Install WordPress with Memcached and Nginx on Ubuntu

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


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

  • PHP> = 5.2.4. Also, using Memcached is highly recommended for the best site performance;
  • Nginx or Apache web server;
  • MySQL or MariaDB installed on your Linux VPS.

System update

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

sudo apt-get update
sudo apt-get upgrade

In addition, you can configure automatic updates.

Installing Nginx

To install the latest version of Nginx 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 the Apache service and then install Nginx on the virtual server using the following commands:

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

Configuring Nginx to start at boot:

sudo update-rc.d -f nginx defaults

Installing 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

Run the WordPress installation procedure

Download the latest version of WordPress available from the official site in your virtual server directory and 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 for its work, where the data is stored, 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_Pa5sw0rD';
mysql> flush privileges;
mysql> quit

Add username, password and MySQL database name for WordPress config 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_Pa5sw0rD');

Create a new Nginx config 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;

Remember to replace “” with a valid domain name. Then disable the default Nginx config file:

rm /etc/nginx/sites-enabled/default

Include new Nginx config file:

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

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

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


listen =;

Find your 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 configuration file “/etc/php/7.0/cli/php.ini”:

vi /etc/php/7.0/cli/php.ini

Add / change the following parameters:

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

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 the /etc/memcached.conf file and increase the Memcached memory to 128MB or higher:

-m 64


-m 128

The web server user (www-data) is required to be able to write files and directories inside the ‘/ var / www / html / wordpress’ directory by running the following command:

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

Edit the Nginx config file (/etc/nginx/nginx.conf) and add “gzip_vary on” to the “HTTP” block:

vi /etc/nginx/nginx.conf

gzip_vary on

Check your 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 check 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 simple instructions. After installation, log into the admin panel and configure according to your needs.

Installing and configuring W3 Total Cache to use Memcached

Open >> Add New >> Search for ‘W3 Total Cache’ >> and click the ‘Install Now’ button next to the name ‘W3 Total Cache’ >> and Activate.

Open >> click on “Settings” in the W3 Total Cache section:

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

Click the “Save All Settings” button.

  • Cache Object
Object Cache: select 'Enable'

Object Cache Method : Memcached

Click the “Save All Settings” button.

All. WordPress installation with Memcached and Nginx is now complete.