Start, stop, and restart varnish on a Mac

Start, stop, and restart varnish on a Mac

FFW Blog - illustration
default avatar
Thought byMatt Korostoff
Senior Solutions Architect
October 01, 2013

In this post we'll cover: how to start, stop, and restart the Varnish Cache http accelerator easily on a mac. I'll also offer a few bash scripts that you can use to save a little time when doing so. You can learn how to install varnish on a Mac in this post if you haven't already.

I'll admit, I feel like this should be easier to do. Try googling the phrase "stop varnish mac" and see how little information is available. So, off the bat, let me give you the main answer you probably came here looking for. To stop varnish on a Mac:

sudo pkill varnishd

I finally found this answer in the official documentation, so that might be a good place to look if you're struggling. Last week I was writing a custom Drupal module that integrates with Varnish, purging specific cache entries when a page changes (similar to the purge module but with some custom logic). I had frequent need to start and stop Varnish, and it had to be on port 80, because this is where Varnish would run in production, and "purge" commands are port-sensitive. Conceptually, this meant frequently doing the following:

  1. Switch apache to port 8000
  2. Start varnish on port 80
  3. Reload my VCL file as needed
  4. Stop varnish
  5. Switch apache back to port 80

I'll go over each in turn:

1. Switch apache to port 8000

I assume here you're using the version of apache that comes bundled with Mac OS. If you're using something else (like Acquia Dev Desktop or MAMP) you'll likely find your apache configuration files in a different directory, probably /Applications/acquia-drupal/apache/conf/ for Acquia Dev Desktop and /Applications/MAMP/conf/apache/ for MAMP. In /etc/apache2/httpd.conf, find the Listen value (for me this is on line 40) and change to 8000:

# Listen: Allows you to bind Apache to specific IP addresses and/or # ports, instead of the default. See also the <VirtualHost> # directive. # # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses. # #Listen Listen 8000

and if you're using virtual hosts, in /etc/apache2/extra/httpd-vhosts.conf change the value of NameVirtualHost to 8000, and update each vhost record to match:

# # Use name-based virtual hosting. # NameVirtualHost *:8000

For each vhost:

<VirtualHost *:8000>    ServerAdmin webmaster@yourhost    DocumentRoot "/path/to/your/site"    ServerName yourhost    ServerAlias www.yourhost </VirtualHost>

Lastly, using the terminal, give this command to restart apache:

sudo apachectl restart

2. Start varnish on port 80

OK this one is easy and well documented. There are two ways to do it. The simplest and easiest is this:

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

where -a is the port where varnish will run and -b is the port where apache will run. -T is where the varnishadm console wil run. We're not using the varnishadm console in this post, but it's useful to have for things like purging and banning. The problem with this first method is that varnish will initialize without a VCL file, meaning you'll have to compile and load it after start up using the varnishadm console. The better option in my view is this:

sudo varnishd -a -T -f /opt/local/etc/varnish/default.vcl -s file,/tmp,500M

where -f /opt/local/etc/varnish/default.vcl is replaced by the actual path to your VCL file. You'll also need to specify some configuration in this file telling varnish where to look for apache:

backend default {    .host = "";    .port = "8000";    .max_connections = 250;    .connect_timeout = 300s;    .first_byte_timeout = 300s;    .between_bytes_timeout = 300s; }

Note, you can specify a -b flag or an -f flag but not both. My VCL file is pretty specific to my use case so I won't post it here, but it's largely based on this post by my hosting provider, Digital Ocean.

3. Reload my VCL file as needed

This one is pretty easy. There are two ways to do it. Personally, I like to wipe the cache when I reload the VCL file, because there may be objects in the cache that would have been excluded with the new VCL file. So to reload your VCL and wipe your cache in the process:

sudo pkill varnishd sudo varnishd -a -T -f /opt/local/etc/varnish/default.vcl -s file,/tmp,500M

If you'd prefer not to wipe your cache when reloading your VCL, instead use:

varnishadm "vcl.load default /opt/local/etc/varnish/default.vcl" varnishadm "vcl.use default"

default here is just a random name you supply—you could choose anything technically. Note, each time you reload the VCL file, you'll need to provide a unique name, so the second time you reload the VCL file you would need to use something like:

varnishadm "vcl.load default2 /opt/local/etc/varnish/default.vcl" varnishadm "vcl.use default2"

4. Stop varnish

I covered this above, but to reiterate it's simply:

sudo pkill varnishd

If for some reason you don't have pkill available to you you can also use this one liner to search for varnish process IDs and kill each one individually:

ps -ae | grep varnish | grep -v grep | awk {'print $1'} | xargs sudo kill -9

5. Switch apache back to port 80

This is the exact same thing as step 1, only in reverse. Since we already covered editing your apache configuration files, let's do the same thing with a slightly fancier technique this time using sed:

sudo sed -i .bak 's/8000/80/g' /etc/apache2/httpd.conf sudo sed -i .bak 's/8000/80/g' /etc/apache2/extra/httpd-vhosts.conf sudo apachectl restart

Can't I just script this?

Yes! Allow me to share. Note, all of these scripts heavily depend on being a passwordless sudoer. Starting varnish: Stopping varnish: Restarting varnish:

Video Tutorial

If you want to see all this in action, check out my video:

Best Practices
Learning Series
Technology Tools

Fill out the form to learn how FFW can support your needs.

Read next:

Acquia Engage NOLA Teaser

Join us at Acquia Engage in New Orleans

Teaser of "Learn to Make Your Site Accessible" blog

Learn to Make Your Site Accessible in a New Training from FFW

Teaser of Drupal for Customized eCommerce Solutions blog

Using Drupal for Customized eCommerce Solutions

Teaser image of Acquia Engage Awards blog

FFW Clients Named as Finalists in Acquia Engage Awards