Install Varnish

Installing Varnish

Depending on the complexity of your WordPress theme, performance can actually be quite slow on AWS. That’s where Varnish can make a tremendous difference. Varnish is a cache which stores copies of pages for users that aren’t logged in (most aren’t). When requests come in to your server, Varnish returns these pre-built pages instantly rather than requiring Apache, PHP and WordPress to rebuild the page for each request. Later, W3 Total Cache can be set up to regularly pre-build pages of your site so they are always ready for Varnish. The power of Varnish is dramatic when interest in your site spikes such as an appearance on BoingBoing, Slashdot or Oprah or @itsthegrumpycat.

We’ll set up Varnish to listen for HTTP (web) traffic on port 80 with Apache listening only to Varnish on port 8080. When Varnish needs a page to be built manually, it will pass the request on to Apache port 8080.

To get started, we need to install Varnish on our instance:

sudo apt-get install varnish

Edit Apache’s ports.conf file to listen on port 8080:

sudo nano /etc/apache2/ports.conf

Here’s what it should look like:

NameVirtualHost *:8080
Listen 8080

Edit your Apache site configuration file to respond to port 8080:

sudo nano /etc/apache2/sites-available/wordpress

Change the VirtualHost line from *:80 to *:8080

<virtualhost *:8080>

You may need to change your Apache default site to listen on port 8080 or disable it:

sudo a2dissite 000-default

Move the original Varnish configuration files to your home directory as backups (in case you decide to review them later):

Replace the Varnish default configuration with the file below (copy and paste).

sudo nano /etc/default/varnish

[sociallocker id=”3080″] Replace the Varnish VCL configuration with the file below (copy and paste). Note: you may get VCL errors when you attempt to restart Varnish if you do not properly maintain line breaks when pasting.

sudo nano /etc/varnish/default.vcl

Now, let’s restart Apache and Varnish:

sudo service apache2 restart
sudo service varnish restart

Remember, if you get VCL errors at this point, it may be because of broken line-breaks from pasting in the Varnish default.vcl file.

Visit your site at http://yourdomain.com and everything should begin working with Varnish. The second time you load a static page, it should be significantly faster.

Please feel free to post corrections, questions or comments below. You can also follow me on Twitter @reifman or email me directly.

For centOS, the default config is in /etc/sysconfig/varnish, while the default.vcl goes right in the same place, /etc/varnish/default.vcl. You’ll have to change your Apache port to 8080 in /etc/httpd/conf.d/vhost.conf (assuming you set it up that way) and in /etc/httpd/conf/httpd.conf (in this one, by default NameVirtualHost is all the way at the end of the file, so make sure to scroll all the way through). After this it should work, just restart httpd and varnish.

I’m not using apache with my node/ghost setup. But I thought I’d also point out for others that on amazon ec2 ami linux the varnish config is also in /etc/sysconfig/varnish instead of /etc/default/varnish. This post was very helpful along with http://we.je/using-varnish-not-nginx-to-run-ghost/ to get things up and running. Thanks!

Costa, as I noted above “Note: you may get VCL errors when you attempt to restart Varnish if you do not properly maintain line breaks when pasting.” Go back thru your file and edit it carefully line by line. Or, if you changed any of the VCL code, you might have broken something.

FYI – when I disabled the default site ‘sudo a2dissite 000-default’ I was no longer able to static files like css and js since the path went from MYSITE.com/wordpress/ to just MYSITE.com. Likewise, the admin section didn’t work due to all the redirects for /wordpress/.

To fix this I reinstated the default site ‘sudo a2ensite 000-default’ to get access to the admin site again and then went to Settings > General and changed both the WordPress Address and the Site Address, removing the /wordpress from both. Then, go ‘sudo a2dissite 000-default’ and you’re back in business.

this was a minor trip up but I thought someone else might have the same problem so I wanted to post it here.

There are a couple of line breaks in the code. The compiler will tell you which ones. Once you edit and fix the default.vcl correctly. It will work. I think I did notice a performance boost. Will have to see.

Thank you for excellent article! Unfortunately, I have many problems with Varnish (not compatible with Jetpack etc), I have installed Varnish the way you described in your blog. Now, I would like to deactivate Varnish. Which steps would you recommend to disable or remove Varnish? Thank you!

Jeff, thanks. Post helped. Took some time with the technical tweaking of switching form Virtualmin configuration to get everything to work, but in the end it all worked very well like a champ. So, if anyone is using “Virtualmin/Webmin” and need some help, please let me know. I will answer as much as I can to assist.

Jeff – Amazing post! Any advice on setting up the VCL to handle multiple sites? I have a single server with 2 separate domains (example.com and othersite.com) both with WordPress installations located in /var/www/example and /var/www/othersite. I can access both until I install varnish, but one I follow your instruction I can’t get to either. I’ve been trolling the varnish sites and found the following code to add to the VCL, but I don’t know if it will work:

I have several vhost conf files on my Apache server. When I followed the instructions above, it broke all the other (non-WordPress) sites on the server. To fix it, I switched my /etc/apache2/ports.conf file back to 80 from 8080, and put the lines

NameVirtualHost *:8080
Listen 8080

at the top of my WordPress site’s conf file. Everything seems to be working now, but I can’t tell if my WP site is being Varnished. Is this the recommended solution, or is there a better way?

I can’t (re-)start Varnish because the VCC compiler stops on the line breaks. How to insert the default.vcl config file correctly? I tried putting the options on line, removing the the slashes, copy/paste it to text editor and then to terminal. Nothing really makes a difference. Anybody can tell me how to copy/paste it correctly?