WordPress installation with nginx and varnish caching

WordPress installation with nginx and varnish caching


WordPress is web software you can use to create a beautiful website, blog, or app. We like to say that WordPress is both free and priceless at the same time.


A complete set of web serving, proxying, acceleration, and load-balancing capabilities for both HTTP-based and TCP-based services.


Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 – 1000x, depending on your architecture.

Please follow the below steps to install wordpress on Ubuntu 16.04.

Step-1-: Install Nginx, required packages and configure Nginx webserver.

sudo apt-get update
sudo apt-get install -y nginx mysql-server php php-fpm php-mysql varnish

It will ask for the mysql root password prompt like below-:

If this field is left blank, the password will not be changed.

New password for the MySQL "root" user: 

Repeat password for the MySQL "root" user: 

Make backup or nginx virtual host.

mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default_org

Create a new default virtualhost file

nano /etc/nginx/site-available/default

Copy the below contents and paste it.

server {
        listen default_server;
        listen [::]:8080 default_server;
         root /var/www/html/wordpress;
        index index.php index.html index.htm;
        server_name yourdomain.com www.yourdomain.com;
        location / {
                        try_files $uri $uri/ /index.php?$args;

         location ~ \.php$ {
                                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                                fastcgi_index index.php;
                                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                                fastcgi_param PATH_INFO $fastcgi_path_info;
                                include fastcgi_params;


Restart the nginx server.

sudo systemctl restart nginx

Check the nginx is working on port 8080.

netstat -ntulp

if netstat command not found.

sudo apt-get install -y net-tools


Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0*               LISTEN      -               
tcp        0      0*               LISTEN      nginx -g daemo                            
tcp6       0      0 :::8080                 :::*                    LISTEN      nginx -g daemo

Step-2: Configure the Varnish Caching Server.

Open the varnish configuration file.

nano /etc/default/varnish

Change the port like below configuration.

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

Note-: changed -a :80 from -a:6081
Change the port on below file too.

nano /lib/systemd/system/varnish.service 
Description=Varnish HTTP accelerator
Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m


Note-: changed -a :80 from -a :6081

Restart nginx and varnish server.

sudo /etc/init.d/nginx restart
sudo /etc/init.d/varnish restart

Confirm all the running ports using below command.

netstat -ntulp


Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0*               LISTEN      2576/mysqld     
tcp        0      0*               LISTEN      2588/nginx -g daemo
tcp        0      0    *               LISTEN      1510/varnishd   
tcp        0      0    *               LISTEN      2675/sshd       
tcp        0      0    *               LISTEN      5667/master     
tcp        0      0*               LISTEN      1510/varnishd   
tcp6       0      0 :::8080                 :::*                    LISTEN      2588/nginx -g daemo
tcp6       0      0 :::80                   :::*                    LISTEN      1510/varnishd   
tcp6       0      0 :::22                   :::*                    LISTEN      2675/sshd       
tcp6       0      0 :::25                   :::*                    LISTEN      5667/master

Nginx must listen port and varnish must port

Step-3: Download and Install WordPress.

 wget http://wordpress.org/latest.tar.gz
 sudo tar -xvf latest.tar.gz -C /var/www/html/

Change the permission.

sudo chmod -R 755 /var/www/html/wordpress

Change the group for www-data.
chown -R www-data:www-data /var/www/html/wordpress

Step-4: Create a Database and User for WordPress application.

The below required commands for database creation and access.

mysql -u root -p'yourmysqlrootpassword' -e "CREATE DATABASE wordpress"
mysql -u root -p'yourmysqlrootpassword' -e "CREATE USER [email protected] IDENTIFIED BY 'password';"
mysql -u root -p'yourmysqlrootpassword' -e "GRANT ALL PRIVILEGES ON wordpress.* TO [email protected];"
mysql -u root -p'yourmysqlrootpassword' -e "FLUSH PRIVILEGES;"

Step-5: Open your web browser and follow the screens.


Open the wordpress dashbord.
http://localhost/admin or http://domainname.com/admin or http://ip/admin

Gotechnies WordPress Blog

Rating: 4.8
Votes: 37
Reviews: 14

Post a comment