WordPress optimization using nginx varnish and php-apc

Posted by & filed under Tutorials.

Now that VPS are becoming popular among developers for personal and professional website hosting, everyone wants to host their website on a fast and reliable vps. No doubt you have thought of having a vps powered website that is faster and fully customizable. But lack of knowledge about server administration and software configurations is the biggest hurdle for some people. This post is an effort to provide with a guide for vps configuration and administration for hosting a wordpress site. Most of the content is general and can be used for optimization of any website hosted on vps. We will provide commands for ubuntu server with root login over ssh.

Things you need for hosting a website -

  • Hosting server
  • Domain name
  • Server software
  • Website application
  • Database software

There are several hosting providers with different type of servers. Most popular type of hosting are shared hosting, dedicated hosting, cloud hosting and vps hosting. We will be using vps hosting for this guide because vps provide us with maximum control of the server and is economic than dedicated hosting. You can get vps from any vps hosting provider, personally i prefer linode.

Domain name is the url of your site and you have to register it from a domain name registrar. Once you have both of them you will have to point your domain name to the your vps. There are two ways of doing that. First is to add name servers of your vps provider in the name server configuration section of domain name registrar control panel. Second one it to configure you vps IP address in (A and CNAME records) DNS configuration section of domain name registrar control panel. We will not be talking about configuration of name servers and DNS records in this guide. Generally vps providers have documents for these configurations and you can always ask us here if you can’t find any suitable guide.

Now starts the real work of installing and configuring server software for wordpress deployment. We will use nginx as our web server and php-fpm for executing php scripts. Type following commands in terminal of your server -

Above commands will install all required software including nginx, php, mysql and varnish. Now create folder for wordpress installation and server logs with following commands

Create directories /srv, /srv/www, /srv/www/public_html and /srv/www/logs. Download wordpress and copy files in public_html folder

Now create a database for wordpress using root user of mysql. Replace words “password”, “mysite”, “user” and “userpassword” according to your installation. Remember these words as we will need them at the time of wordpress installation

Now that everything is ready we will configure nginx and php for running our wordpress site. For configuring nginx we will have to create a file mysite in /etc/nginx/sites-available/ directory. Use following command to create and edit this file

After issuing this command you will be seeing an editor in terminal. We are using nano for editing this file. Copy content below and click in terminal then press Ctrl+Shift+v for copying it in our newly created file

After copying above content in mysite file press Ctrl+x which will ask for saving the file, press y and then enter. Now create link to the file in sites-enabled folder and restart nginx using following commands

y0! you are ready to install wordpress through your web browser. Now open your site url in web browser. You will be presented with wordpress installation page. Follow the instructions on that page and complete installation process. If you can not see wordpress installation page then make sure your domain is configured to point to your VPS and check for any error in log files /var/log/nginx folder.

Now for optimizing your wordpress installation there are several factors you need to consider before choosing right tools. There different type of techniques which can be used for caching and serving cached content to the visitors. Most effective technique is to use full page cache, in which whole html page generated by wordpress is cached as a static file. Another type of caches include opcode cache, database query cache and object cache. Opcode is the code which is generated by php interpreter to be run on the computer, every time we run a php script the interpreter generates this opcode from the php source and then run it. If we cache this opcode in the RAM of the computer than we can save a lot of CPU usage. For caching this opcode we are using APC (alternative php cache). We have installed it earlier and we just need to configure it to take most of it. Go to /etc/php5/conf.d and edit a file named apc.ini using following command and copy and paste content below in the file.

Restart your php for applying this configuration using

There are several free and premium plugins available for caching database queries made by wordpress. We will be using db-cache-reloaded-fix for this. Follow the installation instruction given on the link. You will need to install another wordpress plugin named nginx-compatibility for using permalink feature of wordpress on nginx server. In the apc.ini file we have used apc.shm_size to be 40M which means that apc will use 40MB of RAM for caching opcode of the wordpress php files. Which is sufficient for a single wordpress install. If you have multiple wordpress installed on the same VPS then you can increase it accordingly. If you have more than 1GB ram on your VPS then you can use APC-object-cache-backend wordpress plugin to cache wordpress objects using APC. But if you do so then don’t forget to increase your apc.shm_size in apc.ini file, increment in value depends on the content size of the site and traffic behavior of the website but generally you can increase this value by 16MB for start and then you can optimize it accordingly. Please note that if you see “can not allocate memory…” error in your website error log file /srv/www/logs/error.log then it means you need to increase apc.shm_size and your RAM size if needed. We suggest you to not use this plugin unless you know what you are doing.

Now that we have a working wordpress installation with opcode and database cache enabled we are going to enable full page cache for our site using varnish. Varnish is a proxy server software which caches full HTML pages in the RAM of the computer and servers them to the visitors on request. Varnish is very good at what it does and it can increase your sites speed by 400 times and more. But its not easy to configure varnish for specific purposes and maintaining desired behavior of full page cache. We have already installed it and it is running on our server we just need to configure it to use our nginx as backend server for fetching web pages. Edit the file /etc/default/varnish and copy content below in file below “Alternative 2, configuration with VCL” section

If you carefully read the comments in this file you will understand what we are doing in this step. We configuring varnish to listen on port 80 of our server and use /etc/varnish/default.vcl file as VCL and to use 96MB of RAM for caching HTML pages generated by wordpress. Again if you have more than 1GB RAM or your site have a lot of pages then you can increase this value from 96 according to your need. Save this file and issue following command in terminal to edit another file /etc/varnish/default.vcl

We will not be able to describe everything in this configuration. This content configures varnish to use port 8080  for backend server and it removes all the undesired cookies from request because by default varnish does not cache requests with cookies. Note that we have configured it to use port 8080 for backend server so we have to change port of nginx server to 8080 so that varnish can use it and varnish and nginx don’t conflict. To change port of nginx issue the following command

And edit two lines which are “listen 80;” to “listen 8080″. Not that it written twice in this configuration file so you have to replace both instances of the line. Now save and close the file by pressing Ctrl+x and then y and enter. Now restart nginx and varnish

Now you have fully optimized wordpress installation on your vps. All the configurations and values mentioned in configuration files are for general sites, This configuration can handle around 100K visitors per day and more with 1GB RAM and 2GHz processor VPS (In my simple tests this configuration was able to server million hits per day without any problem but tests are done with ideal conditions). Enjoy editing your site from wordpress control panel. To further optimize your website you can install any html, css and js minify plugins for wordpress and you can use CDN for static contents. Thank you for your time, please comment your suggestions and doubts below.

3 Responses to “WordPress optimization using nginx varnish and php-apc”

  1. Bg

    I tried your steps.. it worked well, installed … BUT I was messed up in bind. also, i need the vps cnfigured for virtualmin to host my few other domains with email. can you please suggest / point the things i missed?

    Thanks a lot.

    Reply
  2. Tang Rufus

    Thanks for the great tutorial. Is Varnish full page cache a replacement of page pre-load (w3 total cache or wp super cache)?

    And, how do Varnish handle cache purging (in case of post update, post publish, comment made, etc)?

    Thank you!

    Reply
    • Vikrant Datta

      Hi

      Glad you find the tutorial helpful.
      Yes it is a replacement for w3 total cache.
      Whenever any of the actions is performed, WordPress notifies the WP-Base plugin to handle it.

      Thanks

      Reply

Leave a Reply