Install Varnish

Purchase a pre-installed image of this tutorial and check out our new knowledge base WordPress Now.

Varnish Cache Can Vastly Speed Up Your WordPress Site

Varnish Speeds Up WordPress

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):

sudo mv /etc/default/varnish ~/default-varnish-old
sudo mv /etc/varnish/default.vcl ~/varnish-default-vcl.old

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.

Continue to Install W3 Total Cache (W3TC)…

26 Comments

  1. Hi Jeff,
    please, could you explain the same for centos?. I tried to follow your steps and translate for centos but I have not succeeded. thank you very much in advance.

    Reply

    1. Rod, sorry – I am focusing on Ubuntu.

      Reply

    2. 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.

      Reply

      1. Matthew – thx for offering this!

        Reply

      2. 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!

        Reply

  2. I’m getting this error massage when I restart Varnish

    root@ubuntu:/etc/varnish# /etc/init.d/varnish restart

    * Stopping HTTP accelerator varnishd [fail]

    * Starting HTTP accelerator varnishd [fail]

    Message from VCC-compiler:

    Syntax error at

    (‘input’ Line 25 Pos 1)

    _unique

    #——

    Running VCC-compiler failed, exit 1

    VCL compilation failed

    root@ubuntu:/etc/varnish#

    Reply

    1. 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.

      Reply

      1. OK, thanks for letting me know, I’ll try it out and let you know about it

        Reply

    2. I have exactly the same error. Please help!

      Reply

      1. Luis – see my note below – you likely mis-pasted extra line breaks into your VCL file.

        Reply

    3. There is accidental linebreak at the end of line 24. The word _unique should be on line 24.

      Reply

      1. Thx David – I’ve identified and fixed 3 such line breaks. basically I think gist was creating line breaks on my long comments

        Reply

    4. After Varnishing, the site went weird and all the other pages e.g. login are going to 404 🙁

      Reply

  3. Hello,

    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.

    thanks for this tutorial, it’s wicked awesome.

    Reply

  4. “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.”

    Which file below? I don’t see it in Safari or Chrome.

    Reply

  5. Here is a link to the default.vcl file example that isn’t showing up on this page:

    https://gist.github.com/reifman/4651531

    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.

    Reply

    1. Thanks Paul – this is fixed again. WordPress has an annoying habit of stripping out script tags in visual editing mode.

      Reply

  6. 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!

    Reply

  7. Patrick K. Johnson Jr. January 6, 2014 at 6:12 am

    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.

    Reply

  8. Great Post Jeff, Thank You! I am however having one issue that is difficult to reproduce. I am getting the following error

    Error 503 Service Unavailable
    Service Unavailable
    Guru Meditation:
    XID: 1051325111
    ________________________
    Varnish cache server

    This occurs mainly when hitting the backend and more seldomly on the frontend. I see that Go4 has an issue with Varnish and Jetpack, could this be the cause of my error?

    Reply

    1. I’ve seen this when the AWS instance is out of memory or slow. Add a swap file to your micro instance or experiment with a different instance size. I’m not sure that this is the cause though.

      Reply

  9. 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:

    if (req.http.host) {
    hash_data(req.http.host);
    }
    else {
    hash_data(server.ip);

    }

    Any ideas? Thanks a ton!

    Reply

  10. Jeff,

    Thank you for putting the time into crafting these instructions.

    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?

    Reply

  11. 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?

    Reply

  12. Just a little concerned with the section in the config file above: DAEMON_OPTS=”-a :80

    -T localhost:6082 shouldn’t localhost be 8080 as we specified in the apache config?

    Reply

Leave a reply

Your email address will not be published.