{"id":2359,"date":"2018-01-03T18:42:55","date_gmt":"2018-01-03T15:42:55","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/how-to-install-and-configure-varnish-with-apache-on-ubuntu-1604\/"},"modified":"2018-01-03T18:42:55","modified_gmt":"2018-01-03T15:42:55","slug":"how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts\/","title":{"rendered":"How to speed up Apache with Varnish HTTP cache on Ubuntu 16.04 LTS"},"content":{"rendered":"<p>Varnish is a proxy server focused on HTTP caching. It&#8217;s designed as an HTTP accelerator, and can act as a reverse proxy for your web server (Apache or Nginx). Varnish has been used for high-profile and high-traffic websites, including Wikipedia, The Guardian, and The New York Times.<\/p>\n<p>In this tutorial, we will show you how to install and configure the Varnish HTTP accelerator as a reverse proxy for the Apache web server. The real web server Apache will run under non-standard HTTP port (running on port 8080). And Varnish will be running as the reverse proxy on HTTP port 80. For this guide, we will be using the Ubuntu 16.04 server.<\/p>\n<h2 id=\"what-we-will-do\">What we will do<\/h2>\n<ol>\n<li>Install Apache Web server<\/li>\n<li>Change Apache Default Port<\/li>\n<li>Install Varnish<\/li>\n<li>Configure Varnish<\/li>\n<li>Test the setup<\/li>\n<\/ol>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<ul>\n<li>Ubuntu 16.04<\/li>\n<li>Root privileges<\/li>\n<\/ul>\n<h2 id=\"step-install-apache-web-server\">Step 1 &#8211; Install Apache Web server<\/h2>\n<p>To start off, install the Apache web server. By default, it&#8217;s available in the Ubuntu repository.<\/p>\n<p>Update the Ubuntu repository and then install Apache using the apt command.<\/p>\n<p class=\"command\">sudo apt update<br \/>sudo apt install -y apache2<\/p>\n<p>After the installation is complete, start the Apache service and enable it to run automatically at system boot time using the following systemctl commands.<\/p>\n<p class=\"command\">systemctl start apache2<br \/>systemctl enable apache2<\/p>\n<p><a class=\"fancybox\" id=\"img-1\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts.png\" alt=\"Install Apache web server\" width=\"500\" height=\"176\" title=\"\"><\/a><\/p>\n<p>The Apache web server has been installed.<\/p>\n<p>Next, in order to allow everyone access to the web server, we must allow HTTP and HTTPS on our firewall rule. By default, Ubuntu comes with a firewall package named UFW.<\/p>\n<p>Open new SSH, HTTP and HTTPS ports using the following ufw commands.<\/p>\n<p class=\"command\">ufw allow ssh<br \/>ufw allow http<br \/>ufw allow https<\/p>\n<p>Now start the firewall service and enable it to launch automatically at system boot.<\/p>\n<p class=\"command\">ufw enable<\/p>\n<p>Type &#8216;y&#8217; and press Enter to confirm.<\/p>\n<p><a class=\"fancybox\" id=\"img-2\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-1.png\" alt=\"Configure the Firewall\" width=\"500\" height=\"184\" title=\"\"><\/a><\/p>\n<p>New ports for http, https, and ssh have been opened, and are accessible from outside of the network.<\/p>\n<p>If you want to test the Apache web server, you can use the netstat command and make sure apache is running under port 80.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p>Or you can use the following curl command.<\/p>\n<p class=\"command\">curl -I hakase-labs.co<\/p>\n<p>You should get the result as shown below.<\/p>\n<p><a class=\"fancybox\" id=\"img-3\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-2.png\" alt=\"Test apache server\" width=\"500\" height=\"246\" title=\"\"><\/a><\/p>\n<h2 id=\"step-change-apache-default-port\">Step 2 &#8211; Change Apache Default Port<\/h2>\n<p>In this tutorial, we will be using Apache as the backend server, and it will not run under the standard http port 80. The Apache web server will be running on port 8080 as backend, and the standard http port 80 will be used by &#8216;Varnish&#8217;.<\/p>\n<p>To change the default apache port, we need to edit the apache configuration &#8216;ports.conf&#8217; and all of the virtual host configuration under the &#8216;sites-available&#8217; directory.<\/p>\n<p>Go to the Apache configuration directory.<\/p>\n<p class=\"command\">cd \/etc\/apache2<\/p>\n<p>Replace port &#8217;80&#8217; with &#8216;8080&#8217; in the Apache configuration &#8216;ports.conf&#8217; and all virtual host files under the &#8216;sites-available&#8217; directory. We can do that by running the following sed commands.<\/p>\n<p class=\"command\">sed -i -e &#8216;s\/80\/8080\/g&#8217; ports.conf<br \/>sed -i -e &#8216;s\/80\/8080\/g&#8217; sites-available\/*<\/p>\n<p>Next, test the Apache configuration and make sure there is no error, then restart the Apache service.<\/p>\n<p class=\"command\">apachectl configtest<br \/>systemctl restart apache2<\/p>\n<p><a class=\"fancybox\" id=\"img-4\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/4.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-3.png\" alt=\"Reconfigure apache for different port\" width=\"500\" height=\"146\" title=\"\"><\/a><\/p>\n<p>Now Apache is running under http port &#8216;8080&#8217;, check it using the netstat command as shown below.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p>Make sure you have the result as below.<\/p>\n<p><a class=\"fancybox\" id=\"img-5\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/5.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-4.png\" alt=\"Check apache port\" width=\"500\" height=\"127\" title=\"\"><\/a><\/p>\n<p>So you can see the Apache web server is running on port &#8216;8080&#8217;.<\/p>\n<h2 id=\"step-install-varnish-http-accelerator\">Step 3 &#8211; Install Varnish HTTP Accelerator<\/h2>\n<p>In this step, we will be installing Varnish from the Ubuntu repository. And for this guide, we will be using varnish 4.<\/p>\n<p>Install varnish using the following apt command.<\/p>\n<p class=\"command\">sudo apt install -y varnish<\/p>\n<p>After the installation is complete, start Varnish and enable it to launch automatically at system boot.<\/p>\n<p class=\"command\">systemctl start varnish<br \/>systemctl enable varnish<\/p>\n<p><a class=\"fancybox\" id=\"img-6\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/6.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-5.png\" alt=\"Install varnish\" width=\"500\" height=\"177\" title=\"\"><\/a><\/p>\n<p>Varnish has been installed on the Ubuntu 16.04 system.<\/p>\n<p>By default, it&#8217;s running under ports &#8216;6081&#8217; for public address and &#8216;6082&#8217; for localhost address. Check it using netstat command below.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p>And you will get the default varnish port.<\/p>\n<p><a class=\"fancybox\" id=\"img-7\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/7.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-6.png\" alt=\"Check varnish\" width=\"500\" height=\"162\" title=\"\"><\/a><\/p>\n<h2 id=\"step-configure-varnish-as-a-reverse-proxy-for-apache\">Step 4 &#8211; Configure Varnish as a Reverse Proxy for Apache<\/h2>\n<p>In this step, we will configure varnish on the front of the Apache web server. Varnish will be running under http port 80, and every request from clients will be handled by it, before being sent to the Apache web server running on port 8080.<\/p>\n<p><strong>&#8211; Backend Configuration<\/strong><\/p>\n<p>Go to the &#8216;varnish&#8217; configuration directory and backup the default file &#8216;default.vcl&#8217;.<\/p>\n<p class=\"command\">cd \/etc\/varnish\/<br \/>cp default.vcl default.vcl.aseli<\/p>\n<p>Edit the varnish configuration &#8216;default.vcl&#8217; using the <a href=\"https:\/\/www.howtoforge.com\/vim-basics\" target=\"_blank\" rel=\"noopener noreferrer\">vim editor<\/a>.<\/p>\n<p class=\"command\">vim default.vcl<\/p>\n<p>Define the &#8216;backend&#8217; configuration on line 16. The backend for our setup is Apache, and it&#8217;s&#8217; running on port 8080. So the varnish configuration for our apache setup should be the following:<\/p>\n<pre class=\"system\">backend default {<br\/>\u00a0\u00a0\u00a0 .host = \"127.0.0.1\";<br\/>\u00a0\u00a0\u00a0 .port = \"8080\";<br\/>}<\/pre>\n<p><a class=\"fancybox\" id=\"img-8\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/8.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-7.png\" alt=\"Varnish configuration\" width=\"500\" height=\"164\" title=\"\"><\/a><\/p>\n<p>Save and exit.<\/p>\n<p><strong>&#8211; Running Varnish on Port 80<\/strong><\/p>\n<p>Next, we need to change the default varnish ports. The default ports for varnish is &#8216;6081&#8217; and &#8216;6082&#8217;, and we need to change the port to the http port 80 (only for public address).<\/p>\n<p>Edit the varnish parameter configuration in &#8216;\/etc\/default&#8217; directory.<\/p>\n<p class=\"command\">cd \/etc\/default\/<br \/>vim varnish<\/p>\n<p>Edit the &#8216;DAEMON_OPTS&#8217; line, change the default port &#8216;6081&#8217; for public address with standard http port &#8217;80&#8217; as shown below.<\/p>\n<pre class=\"system\">DAEMON_OPTS=\"-a :80 \\<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -T localhost:6082 \\<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -f \/etc\/varnish\/default.vcl \\<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -S \/etc\/varnish\/secret \\<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -s malloc,256m\"<\/pre>\n<p><a class=\"fancybox\" id=\"img-9\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/9.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-8.png\" alt=\"Run varnish on port 80\" width=\"500\" height=\"213\" title=\"\"><\/a><\/p>\n<p>Save and exit.<\/p>\n<p>Next, we need to edit the varnish service file &#8216;varnish.service&#8217;. Go to the &#8216;\/lib\/systemd\/system&#8217; directory and edit the service file using vim.<\/p>\n<p class=\"command\">cd \/lib\/systemd\/system\/<br \/>vim varnish.service<\/p>\n<p>On the &#8216;ExecStart&#8217; line, change the varnish start command as below.<\/p>\n<p class=\"system command\">ExecStart=\/usr\/sbin\/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f \/etc\/varnish\/default.vcl -S \/etc\/varnish\/secret -s malloc,256m<\/p>\n<p>Save and exit.<\/p>\n<p>Reload the systemd service configuration and then restart varnish.<\/p>\n<p class=\"command\">systemctl daemon-reload<br \/>systemctl restart varnish<\/p>\n<p>Now check varnish and make sure it&#8217;s running on the http port 80.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p><a class=\"fancybox\" id=\"img-10\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/10.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-9.png\" alt=\"Check ports and services\" width=\"500\" height=\"242\" title=\"\"><\/a><\/p>\n<p>So Varnish is now running under http port 80 as front-end for Apache web server, which is on port 8080.<\/p>\n<h2 id=\"step-testing\">Step 5 &#8211; Testing<\/h2>\n<p>Test using the curl command.<\/p>\n<p class=\"command\">curl -I hakase-labs.co<\/p>\n<p>Make sure you get the http header as shown below.<\/p>\n<p><a class=\"fancybox\" id=\"img-11\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/11.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-10.png\" alt=\"Test http access\" width=\"500\" height=\"342\" title=\"\"><\/a><\/p>\n<p>Checking from web browser, the URL of my test server is:\u00a0<a href=\"http:\/\/hakase-labs.co\/\" target=\"_blank\" rel=\"noopener\">http:\/\/hakase-labs.co\/<\/a>. Choose your server URL here.<\/p>\n<p>And we still get Apache contents.<\/p>\n<p><a class=\"fancybox\" id=\"img-12\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/12.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-11.png\" alt=\"Apache test page\" width=\"500\" height=\"305\" title=\"\"><\/a><\/p>\n<p>Check the varnish log using the &#8216;varnishncsa&#8217; command.<\/p>\n<p class=\"command\">varnishncsa<\/p>\n<p>And we get log just from the Apache access log.<\/p>\n<p><a class=\"fancybox\" id=\"img-13\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_and_configure_varnish_with_apache_on_ubuntu_1604\/big\/13.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-speed-up-apache-with-varnish-http-cache-on-ubuntu-16-04-lts-12.png\" alt=\"Apache access log\" width=\"500\" height=\"98\" title=\"\"><\/a><\/p>\n<p>Varnish installation and configuration for the Apache web server has been completed.<\/p>\n<p>Reference<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Varnish is a proxy server focused on HTTP caching. It&#8217;s designed as an HTTP accelerator, and can act as a reverse proxy for your web server (Apache or Nginx). Varnish has been used for high-profile and high-traffic websites, including Wikipedia, The Guardian, and The New York Times. In this tutorial, we will show you how &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2359","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2359"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2359\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}