Install WordPress with Memcached and Nginx on Ubuntu

Install WordPress with Memcached and Nginx on Ubuntu

In this tutorial, we are going to provide you with step-by-step instructions on how to install WordPress with Memcached and Nginx on an Ubuntu 16.04 VPS. WordPress is one of the best open-source content management systems written in PHP.


At the time of writing this tutorial, the latest stable version of WordPress is 4.8 and it requires:

1. Update the system

Make sure your package list and the OS packages are up to date by running the following commands:

sudo apt-get update
sudo apt-get upgrade

You can also configure automatic updates.

2. Install Nginx

To install the latest Nginx version from the official Nginx repository, edit the ‘/etc/apt/sources.list’ file:

sudo vi /etc/apt/sources.list

Add the following lines:

deb xenial nginx
deb-src xenial nginx

3. Install MySQL

sudo apt-get install mysql-server

Stop and remove Apache service, then install nginx your virtual server using the following commands:

sudo service apache2 stop
sudo apt-get remove apache2
sudo apt-get autoremove
sudo apt-get install nginx

Configure Nginx to start on boot:

sudo update-rc.d -f nginx defaults

4. Install Memcached, PHP 7 and PHP modules:

sudo apt-get install memcached php-memcache php-memcached php7.0 php7.0-cli php7.0-fpm php7.0-curl php7.0-mbstring php7.0-mysql php7.0-gd php7.0-zip php7.0-intl php7.0-mcrypt libgd-tools libmcrypt-dev mcrypt php-pear libgd-dev php-pear php7.0-dev

5. Start the WordPress installation procedure

Download the latest version of WordPress available at the official website to a directory of your virtual server, then extract it using the following commands:

sudo apt-get install wget unzip
cd /opt/
mv /opt/wordpress/ /var/www/html/wordpress/

WordPress requires a database to work as this is where data is saved, so create a new MySQL database:

mysql -u root -p
mysql> create database wpdb;
mysql> GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'localhost' IDENTIFIED BY 'Y0Ur_Pa55w0rD';
mysql> flush privileges;
mysql> quit

Add the MySQL username, password and database name to the WordPress configuration file:

mv /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php
vi /var/www/html/wordpress/wp-config.php
define('DB_NAME', 'wpdb');

define('DB_USER', 'wpuser');

define('DB_PASSWORD', 'Y0Ur_Pa55w0rD');

Create a new Nginx configuration file and add the following virtual block for your domain name:

vi /etc/nginx/sites-available/

Add the following lines:

server {
listen 80;
root /var/www/html/wordpress/;
index index.php;
access_log /var/log/nginx/;
error_log /var/log/nginx/;
charset en_us.UTF-8;

location / {            
                try_files $uri $uri/ /index.php?$args;

location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
    }location ~*  \.(pdf)$ {
        expires 30d;

location ~ \.php$ {
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;

Do not forget to replace ‘’ with your actual domain name. Then, disable the ‘default’ Nginx configuration file:

rm /etc/nginx/sites-enabled/default

Enable the new Nginx configuration file:

ln -sf /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Open the ‘/etc/php/7.0/fpm/pool.d/www.conf’ file and change the ‘listen’ variable:


listen = /run/php/php7.0-fpm.sock


listen =;

Edit the ‘/etc/php/7.0/fpm/php.ini’ configuration file:

vi /etc/php/7.0/fpm/php.ini

Add/modify the following settings:

max_execution_time = 300
max_input_time = 600
memory_limit = 256M
post_max_size = 64M
upload_max_filesize = 64M

[ecko_alert color=”blue”]All of this seems too complicated? Get a WordPress VPS from us and we’ll do all of this for you, free of charge! We’ll take care of your server 24/7[/ecko_alert]

Edit the ‘memcache.ini’ configuration file:

vi /etc/php/7.0/mods-available/memcache.ini

add the following lines at the end:

session.save_handler = memcache
session.save_path = "tcp://localhost:11211"

Edit /etc/memcached.conf and increase the Memcached memory limit to 128 MB or higher:


-m 64


-m 128

Configure memcached to listen on localhost ( and disable UDP:

vi /etc/memcached.conf
-U 0

The web server user (www-data) needs to be able to write to files and directories inside the ‘/var/www/html/wordpress’ directory, so it can easily be accomplished by executing the following command:

sudo chown www-data:www-data -R /var/www/html/wordpress/

Edit the main nginx configuration file (/etc/nginx/nginx.conf) and add ‘gzip_vary on’ in the ‘http’ block:

vi /etc/nginx/nginx.conf

gzip_vary on

Test the nginx configuration:

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If the test is successful, restart memcached, php7.0-fpm and Nginx services for the changes to take effect:

sudo service memcached restart
sudo service php7.0-fpm restart
sudo service nginx restart

Open using your favorite web browser and follow the easy instructions. Once installed, log in to the administrator back-end and configure according to your needs.

6. Install and configure W3 Total Cache to use Memcached

Open >> Add New >> Search for ‘W3 Total Cache’ >> click ‘Install now’ next to ‘W3 Total Cache’ title >> Activate.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer

Now just $43 .99


Open >> click ‘Settings’ from the W3 Total Cache section :

  • Minify
Minify: select 'Enable'
Minify Cache Method: Memcached

Click ‘Save all settings’.

  • Object Cache
Object Cache: select 'Enable'

Object Cache Method : Memcached

Click ‘Save all settings’.

That is it. The WordPress installation with Memcached and Nginx is now complete.

7. Further reading

After installing WordPress, you can read some of our WordPress articles:

Of course, you don’t have to Install WordPress with Memcached and Nginx on Ubuntu, if you use one of our WordPress VPS Hosting services, in which case you can simply ask our expert Linux admins to install WordPress with Memcached and Nginx for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to Install WordPress with Memcached and Nginx on Ubuntu, please share it with your friends on the social networks using the buttons below or simply leave a reply in the comments section. Thanks.

4 thoughts on “Install WordPress with Memcached and Nginx on Ubuntu”

    • In order to install multiple wordpress websites you will have to create a new nginx configuration for each domain, separate database and separate wordpress installation.

  1. Hello,
    Thanks for a very detailled tutorial.
    Something does not make sens to me: why do you edit the /etc/php/7.0/cli/php.ini file, shouln’t it be the /etc/php/7.0/fpm/php.ini file as php-fpm is used to render webpages and cli is used for the command line version from the bash.
    Am I wrong?


Leave a Comment