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:

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.

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

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

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

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 =;

Locate the PHP configuration file:

# php -i | grep -i php.ini
Configuration File (php.ini) Path => /etc/php/7.0/cli
Loaded Configuration File => /etc/php/7.0/cli/php.ini

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

vi /etc/php/7.0/cli/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

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

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

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.

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.

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.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.