Varnish is a powerful caching HTTP reverse proxy that can improve the performance of your web server by caching content. This guide will show you how to install and configure Varnish with Apache on Ubuntu 12.04.
1. Install Apache
Ensure that Apache is installed on your system:
sudo apt-get update
sudo apt-get install apache2
2. Install Varnish
Install Varnish using the following command:
sudo apt-get install varnish
3. Configure Varnish to Use Apache
By default, Varnish listens on port 6081, but it needs to forward requests to Apache, which listens on port 80. To configure this, edit the Varnish default configuration file:
sudo nano /etc/varnish/default.vcl
Find the backend default
section and update the port number to match Apache's port (80):
backend default {
.host = "127.0.0.1";
.port = "80";
}
Save and close the file (press Ctrl+X, then Y, and Enter).
4. Configure Varnish to Listen on Port 80
By default, Varnish listens on port 6081. To make it listen on port 80, you need to update the Varnish startup configuration file:
sudo nano /etc/default/varnish
Find the DAEMON_OPTS
line and update it to the following:
DAEMON_OPTS="-a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m"
Save and close the file.
5. Restart Varnish and Apache
Restart both services to apply the changes:
sudo service varnish restart
sudo service apache2 restart
6. Verify Varnish is Working
Check that Varnish is listening on port 80 by running:
sudo netstat -plnt | grep varnish
You should see that Varnish is listening on port 80.
7. Test Varnish Caching
Test that Varnish is caching content by accessing your site and checking the response headers. You can use tools like WebConfs HTTP Header Check or curl:
curl -I http://your_domain_or_ip
Look for the X-Varnish
and X-Cache
headers in the response to confirm that Varnish is caching requests.
8. Troubleshooting
If you encounter issues, check the Varnish and Apache logs for errors:
sudo tail -f /var/log/varnish/varnish.log
sudo tail -f /var/log/apache2/error.log
9. Fine-Tuning Varnish
Consider configuring additional caching rules and optimizing Varnish settings for your specific use case. Refer to the Varnish documentation for advanced configuration options.