MESSAGE FROM FFW ON COVID-19 (NOVEL CORONAVIRUS): REMOTE WORK IS IN OUR DNA, AND WE AREN’T SLOWING DOWN.
See how we are still operating at full speed, and resources to help your business maintain progress during this trying time.
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:
Switch apache to port 8000
Start varnish on port 80
Reload my VCL file as needed
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 18.104.22.168:80 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
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 127.0.0.1:80 -b 127.0.0.1:8000 -T 127.0.0.1:6082 -s file,/tmp,500M
where -a 127.0.0.1:80 is the port where varnish will run and -b 127.0.0.1:8000 is the port where apache will run. -T 127.0.0.1:6082 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 127.0.0.1:80 -T 127.0.0.1:6082 -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:
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:
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:
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:
If you want to see all this in action, check out my video:
Fill out the form to learn how FFW can support your needs.