Varnish is an excellent high-performance, HTTP accelerator. The technical term for Varnish is a "reverse proxy cache", meaning that it handles the requests when you visit a website acting as a cached layer of content on top of Apache. This means that after a page has been requested once from the web server, Varnish keeps a copy of that file in an ultra-fast storage so that the next time that page is requested, it returns it immediately instead of starting up Apache, PHP, MySQL, and any other technologies your website may be built upon. If Varnish doesn't have a copy of the file or page being requested, it will request the page from the normal web server.

This article explains step-by-step instructions on how to get started with Varnish on a local Mac OS X sandbox. I personally set it up with the MAMP package, but because it doesn't make any difference what web server you use, you can use these instructions to set up Varnish in front of the built-in Mac OS X Apache or anything else you may have installed from MacPorts or compiled from source.

If you're not familiar with setting up a web server on Mac OS X, you'll need to get that working first. I'd recommend the Lullabot videocast on Installing a local web server on Mac OS X.

In this tutorial, we'll be installing Varnish from source. I haven't seen any Mac OS X binaries for Varnish out there yet, but even if they eventually become available this guide could still be useful for installing the cutting edge versions of Varnish.

Preparing for installation

The first thing you'll need to compile anything from source is a compiler, the most common one being GCC (Gnu C Compiler). This is included with a Mac OS X developer tools (better known as XCode). You'll also need MacPorts installed to give you access to the "port" command. Both of these packages are standard .dmg double-click installers.

Varnish requires a few libraries that aren't included with Mac OS X or are out-dated, so we need to update them first.

Run the following two commands to install automake and libtool.

$ sudo port install automake
$ sudo port install libtool

Now it's likely that Mac Ports will install these files in a directory that is not checked for executables by default. So you may need to adjust your $PATH in your shell configuration.

If you use bash as your shell (the Mac OS X default) then your shell configuration file will be named .profile or .bashrc in your Home directory. Open this file with your preferred text editor, or create an empty text file if it doesn't exist at all.

$ vi ~/.profile

Add these lines to this file. Note that you may already have a similar line in this file, just add /opt/local/bin: and /opt/local/sbin: to the beginning of it if it's not there already.

# Add MacPorts executable directory to $PATH. First declarations take precedence over later ones.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH

This should make it so that MacPorts executables are used over the default ones provided by Mac OS X. To confirm you can run "which automake" (the port we installed above) and see that it lives in /opt/local/bin.

$ which automake

Installation of Varnish itself

Author note: Note that these instructions to build from scratch are now almost entirely unnecessary. As noted in the comments below, MacPorts now provides a Varnish bundle directly. So you can skip these installation instructions and just use sudo port install varnish instead. You can resume the instructions at starting varnish.

Okay, we're all now in place and ready to actually install Varnish! This part is actually quite straight-forward. The official installation instructions may also be a useful reference.

  1. Download Varnish from SourceForge.
  2. Expand the downloaded archive and switch to that download directory.
    $ cd ~/Downloads/varnish-2.0.6
  3. Now we need to compile the downloaded source code. There will be quite a bit of text following each command. As long as you don't get any blaring ERROR: warnings then the compilation was successful.
    $ ./
    $ ./configure
    sudo make install
  4. After this, Varnish should be available. However, this process places the main Varnish executable, varnishd in an unusual location. Try running the following command:
    $ which varnishd
    # What you'd expect:
    # What you probably get instead:
    varnishd not found

    If varnish is found, continue on to the next step. However, for most people Varnish will be installed in an unusual location. For me, this location was /usr/local/sbin/varnishd, where varnishd was the only file in that entire directory. All the other Varnish executables end up in /usr/local/bin like they should. So my preference is to move varnishd in with the rest of the files.

    sudo mv /usr/local/sbin/varnishd /usr/local/bin/varnishd

    Now varnishd should be easily found with a which command.

    $ which varnishd
  5. You can now start up Varnish by using the varnishd command. This command takes a few parameters which can be useful for setting up a few basic options.

    $ sudo varnishd -a -b -s file,/tmp,500M

    The -a flag is where Varnish will be accessible. This means that if I visit http://localhost:8080/ I will be using Varnish. The -b flag is where Varnish will look if it doesn't have a page in cache. Point this to your local Apache port. I have MAMP to use port 80, though the default port for MAMP is 8888 when you first install it. Lastly, the -s flag is for "storage", indicating how much space Varnish should use for its own purposes and where it should keep the cache file. By default, Varnish will attempt to use a whopping 50% of free space on your disk. So if you have 120GB available, it will make a 60GB swap file! Since local environments rarely require such space, the above command will use 500 MB.

  6. Open up your web browser and point it at http://localhost:8080. You should see your localhost files exactly as though you were looking directly at http://localhost. However, inspecting the headers you should see that the pages are being served by Varnish. Using FireBug for Firefox, you can confirm this by checking under the "Net" tab. You'll be looking for a header that says: Via: 1.1 varnish.

    The Varnish headers as display through the FireBug "Net" tab.

Post Installation

After getting Varnish installed and running the first time, you'll probably start to wonder, "okay, now what do I do with it?" Installing Varnish locally is a way of making your local sites serve up really, really fast, but it's not a good idea to use for anything other than testing your live server configurations.

Regarding how to set up your Drupal site with Varnish, we're planning more detailed articles, but in the meantime, these references may help you get started.

Nate Lampton

Nate Lampton standing in front of greenery.
Nate Lampton is a leader in Open Source with over a decade of contributions to the Drupal project. He joined Lullabot in 2006.

Featured Work

Latest Podcasts

Let's Connect

Want to learn more about working with us or just say hello?

Contact Us