Apache is popular web server used by most web hosting companies. Varnish cache is an HTTP accelerator and reverse proxy. We can use Varnish with any HTTP server. In this example, we will be using Apache 2.<\/p>\n
As a web server, Apache can use a considerable amount of server resources to serve pages. If you are running a high-traffic website, then you might need an HTTP accelerator to boost server performance. Varnish will help you with that.<\/p>\n
Install Apache server and activate it by using the following commands:<\/p>\n
sudo apt-get update\r\nsudo apt-get install apache2-mpm-event\r\n<\/code><\/pre>\nYou can test Apache’s server status with this command:<\/p>\n
sudo service apache2 status\r\n<\/code><\/pre>\nIf the service is running, “apache2 is running” will be printed to your terminal. Otherwise, you can start the service with this command:<\/p>\n
sudo service apache2 start\r\n<\/code><\/pre>\nStep 2<\/h3>\n
Install the latest stable version of Varnish 4. This version is not available in Ubuntu Repository by default, so you need to run the following commands to install it.<\/p>\n
sudo apt-get install apt-transport-https\r\nsudo curl https:\/\/repo.varnish-cache.org\/ubuntu\/GPG-key.txt | apt-key add -\r\necho \"deb https:\/\/repo.varnish-cache.org\/ubuntu\/ trusty varnish-4.0\" >> \/etc\/apt\/sources.list.d\/varnish-cache.list\r\nsudo apt-get update\r\nsudo apt-get install varnish\r\n<\/code><\/pre>\nStep 3<\/h3>\n
Configure Varnish Cache. Here we are going to change the Varnish server port to 80. Run the following command:<\/p>\n
sudo nano \/etc\/default\/varnish\r\n<\/code><\/pre>\nNow look for\u00a0DAEMON_OPTS=”<\/strong>\u00a0under\u00a0Alternative 2, Configuration with VCL<\/strong>. Change the\u00a0DAEMON_OPTS=”<\/strong>\u00a0section to match the following lines. This is only a port update.<\/p>\nDAEMON_OPTS=\"-a :80 \\\r\n -T localhost:6082 \\\r\n -f \/etc\/varnish\/default.vcl \\\r\n -S \/etc\/varnish\/secret \\\r\n -s malloc,256m\"\r\n<\/code><\/pre>\nPress\u00a0CTRL<\/strong>\u00a0+\u00a0X<\/strong>, then\u00a0Y<\/strong>\u00a0to save the file.<\/p>\nStep 4<\/h3>\n
Configure Apache. We need to change the listening port of Apache from 80 to 8080.<\/p>\n
Edit the ports file by run the following command:<\/p>\n
sudo nano \/etc\/apache2\/ports.conf\r\n<\/code><\/pre>\nChange the\u00a0Listen 80<\/strong>\u00a0to\u00a0Listen 8080<\/strong>.<\/p>\nNext, update the virtual host file…<\/p>\n
sudo nano \/etc\/apache2\/sites-available\/000-default.conf\r\n<\/code><\/pre>\n… change\u00a0<VirtualHost *:80><\/strong>\u00a0to\u00a0<VirtualHost *:8080><\/strong>.<\/p>\nIf you have other virtual host files, then they should be updated as well.<\/p>\n
Restart both services.<\/p>\n
sudo service apache2 restart\r\nsudo service varnish restart\r\n<\/code><\/pre>\nYou’re all set. See the following sections for advanced setup tips.<\/p>\n
View stats<\/h3>\n
Run the following command to show Varnish stats:<\/p>\n
varnishstat\r\n<\/code><\/pre>\nAdvanced VCL settings<\/h3>\n
You can edit the\u00a0default.vcl<\/code>\u00a0file for various features.<\/p>\nEnable leverage browser caching<\/h4>\n
To enable browser caching for media files, your\u00a0vcl_backend_response<\/code>\u00a0should match the following configuration.<\/p>\nsub vcl_backend_response {\r\n if (bereq.url ~ \"\\.(png|gif|jpg|swf|css|js)$\") {\r\n unset beresp.http.set-cookie;\r\n set beresp.http.cache-control = \"max-age = 2592000\";\r\n }\r\n}\r\n<\/code><\/pre>\nThis will improve your site speed and SEO ranking.<\/p>\n
Purge cache<\/h4>\n
To clear the Varnish’s cache, you can change\u00a0vcl_recv<\/code>\u00a0to match the following configuration:<\/p>\nsub vcl_recv {\r\n if (req.method == \"PURGE\") {\r\n return (purge);\r\n }\r\n }\r\n<\/code><\/pre>\nAfter making this change, you can send a curl request in your ssh session with the following format:<\/p>\n
curl <domain_name.com> -XPURGE\r\n<\/code><\/pre>\nHere,\u00a0-XPURGE<\/code>\u00a0will send the purge request to the Varnish server.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","manualknowledgebasecat":[109,247],"manual_kb_tag":[443],"_links":{"self":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/2885"}],"collection":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb"}],"about":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/types\/manual_kb"}],"author":[{"embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/comments?post=2885"}],"version-history":[{"count":1,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/2885\/revisions"}],"predecessor-version":[{"id":2886,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb\/2885\/revisions\/2886"}],"wp:attachment":[{"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/media?parent=2885"}],"wp:term":[{"taxonomy":"manualknowledgebasecat","embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manualknowledgebasecat?post=2885"},{"taxonomy":"manual_kb_tag","embeddable":true,"href":"https:\/\/support.aklwebhost.com\/wp-json\/wp\/v2\/manual_kb_tag?post=2885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}