Speed up WordPress 1000x

Facebook's HHVM

Facebook’s HHVM logo

(HHVM or PHP7)+Nginx+FastCGI+Varnish

  • PHP 7.0.10 (Latest Stable)
  • Nginx 1.11.3 (Latest)
  • HHVM  3.14.4 (Latest)
  • Varnish Cache Proxy service 4.0.3 (Latest)

This was done on a centOS 7 instance.

First tune your OS

  1. Tuned optimizes the settings on the OS and is configured based on your needs. Sort of like a profile that changes the settings, to find them use the “tuned-adm list” command

You’d want to select the throughput-performance profile as such

This should lower slightly page load time a little bit. Then we update the server’s existing packages and start to dive in.

Temporarily disable selinux

  1. Next since we’re going to be installing PHP7 and its packages we want to enable EPEL and Remi repos to get the files.

Next we’ll enable Remi’s repos

Remove old versions of PHP & Install PHP 7

Install PHP 7 dependencies

Run the following to make sure all is jiffy

Check PHP 7 Version

Now the great thing about PHP 7 is that it comes with APCu and OpCache (which out of the box are great extensions for caching) you can verify their presence by running the following command.

MariaDB Installation (Great replacement for mySQL)

If you don’t have it already, install MariaDB 5.5 [ or latest version]

Start its installation and set a password you’ll remember by running the following command

 

To slightly speed up how WordPress utilizes MariaDB, we’ll adjust it as follows to have it optimize its usage
Edit /etc/my.cnf.d/server.cnf

Query_cache_size enables cache for repeated queries. Innodb_buffer_pool_size will allow us to increase the cache for InnoDB Data.

Next login to MariaDB

Create user for your WordPress installation, keep a note of it somewhere. Run the following commands to create a new wordpress database, and create a new user and give them all privileges to that database.

 

Now we’ll get to Installing HHVM, this is based on https://github.com/facebook/hhvm/wiki/Prebuilt-Packages-on-Centos-7.x however it has been updated to fix broken links

First update

Install HHVM Dependencies

 

Install HHVM

Check HHVM’s version to check it was installed correctly

Copy HHVM’s systemd service into the directory that has a higher precedence for services; /etc/systemd/system/

In your file that has been copied over (/etc/systemd/system/hhvm.services), edit it and change the vServer.Port port to 9000 for and change daemon user from nginx to root

Save the file and reload using the following command

Now we’ll update HHVM’s port configuration as well so it also listens on port 9k; edit /etc/hhvm/server.ini

Change the timezone to your server’s location, around line 11 you can find full timezone list here: http://php.net/manual/en/timezones.america.php

Save changes and exit the file

You can now disable PHP 7 and use HHVM in its place by doing the following

 

Finally install NGINX & Varnish

Varnish will be outside and will receive the port 80 traffic and then send that to our NGINX webserver.  After NGINX processes it with HHVM handler, it’ll send it back out to Varnish who’ll cache the data and save it in RAM to serve it quickly. FOR NOW if possible, avoid Varnish and use an alternative or not use it altogether, because in a new post that will be added soon I’ll have an alternative to Varnish (it has shown to consume more memory and usage than nginx for sure).

Create a repo “/etc/yum.repos.d/nginx.repo” to install the latest nginx. Inside that file, add the following:

Check NGINX Version

Install Varnish

Enable Varnish at boot

Start Varnish

Now for Varnish Configuration
Edit /etc/varnish/varnish.params

Look for

Change to

The reason is that we want Varnish to receive the traffic on Port 80 and be the conduit to cache data to the customer.

Edit /etc/varnish/varnish.params
For Varnish Cache Storage on disk:
(This step is recommended if you don’t have enough RAM and have SSD drive with tons of space; use plenty for the storage file 4GB to 5 GB)
Change the following value as follows:

This will use that storage file size of 4 GB located at that location.

For Varnish Cache Storage on RAM:
Change the following value as follows:

Instead of 512M RAM you can use the largest free available RAM.

Now to update the port that Varnish connects to send and receive data to/from, you’ll want to edit the following file

/etc/varnish/default.vcl

Look for

 

Change the port number to 8080. Next I recommend copying and updating the configuration for Varnish from Matt on https://github.com/mattiasgeniar/varnish-4.0-configuration-templates to update your Varnish config to work with WordPress. I will have update here regarding that but for now your best bet is to follow Matt’s Github directions.

If you already have apache, you can now formally switch to nginx webserver as follows, if you are not sure if httpd is running or not, it doesn’t hurt to run the following commands as well

Now we’ll be making some file changes, in /etc/nginx/nginx.conf
The numbers are shown to show you the code’s location, they should not be added to the file.
Look for..

 

Right after, change or add the following [remember again not to add the line numbers – they are there to guide you! ]

 

Save changes, and exit the file.

Now this step is optional but I have seen performance gains as much as 10ms shaved off my site. It is to mount your fastcgi cache folder onto ram so that it loads cached files from ram rather than from disk. To do that, do the following and adjust size (for Megabytes e.g. 512M) as necessary

To make sure that it loads every time you reboot add the following to /etc/fstab file

Next we’ll create a new file (you don’t have to name it httpd.conf but whatever helps you identify your site config file), /etc/nginx/conf.d/httpd.conf

In this file add the codes listed below, replace server_name value with your website’s url. The listen port will be 8080 because instead of nginx, we want Varnish to listen on port 80. Varnish will set atop nginx and work with it and hhvm to deliver cached files to customer. Below be sure to update the root location with your website’s folder. Nginx will work with fastcgi on port 9000 to get files quickly handled by HHVM.

Save changes, and exit the file

Restart nginx

Now before we get going, final notes.

Your server will have firewall-cmd installed by default with “public” zone so run the following to enable access to your website.

 

Now that we have access to your website you should begin to have a feel for fast speed on your website.

 

If your website still fails to load, run the following to adjust permissions to your website’s site folder

 

 

If you ever become unsatisfied with HHVM and wish to use PHP 7 instead or visa versa, you can toggle back and forth using the simple commands below

 

You should now install wordpress on your website (If you haven’t already!) and install the W3Total Cache plugin and configure Cloudflare. Cloudflare+W3Total Cache plugins are great companions. Cloudflare will act as an CDN and minify some of your theme files and will work with W3Total Cache to deliver files compressed.

https://support.cloudflare.com/hc/en-us/articles/200169756-Can-I-use-WordPress-caching-plugins-like-Super-Cache-or-W3-Total-Cache-W3TC-with-CloudFlare

https://support.cloudflare.com/hc/en-us/articles/200169546-What-fields-do-I-need-to-enter-in-W3TC-W3-Total-Cache-settings

 

To conduct simple tests using either hhvm or php7 you can run the following tests using either above

 

Heavy tests; 10000 is the number of requests and 100 is the concurrent connections

That’s it!

Test Results
How can this article be complete without a thorough test?
From a remote server I ran to my website server

From using firebug I was able to see that the site itself loaded quickly within 100ms tops but using Google ads and wp images caused delays that reached 400ms.

Huge thanks to the inspiration for this post:
https://en.kusanagi.tokyo/archives/category/simply-fast-wordpress/

Additionally you’ll be thrilled to learn that Softlayer, an IBM Cloud provider now provides easy access to roll out this same setup using an premade image, read about that here:

http://blog.softlayer.com/2016/speed-your-wordpress-softlayer

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published.

Loading Disqus Comments ...