{"id":2139,"date":"2016-06-30T18:01:35","date_gmt":"2016-06-30T22:01:35","guid":{"rendered":"https:\/\/www.globo.tech\/learning-center\/?p=2139"},"modified":"2016-09-30T12:58:09","modified_gmt":"2016-09-30T16:58:09","slug":"set-up-varnish-ubuntu-16","status":"publish","type":"post","link":"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/","title":{"rendered":"How to set up Varnish on Ubuntu 16"},"content":{"rendered":"<p>One of the problems with running a site with many users is that the constant requests for dynamic content can really bog down a web server. In order to solve this problem, many big companies turn to a tool called Varnish. It&#8217;s an HTTP accelerator that caches both static and dynamic content, thereby easing the load on your server and improving site performance for the end user.<\/p>\n<p>Varnish is open source under the BSD license and can be easily installed on Ubuntu 16.<\/p>\n<h2>Getting started<\/h2>\n<p>To complete this guide, you will need the following:<br \/>\n\u2022 1 Node (<a href=\"https:\/\/www.globo.tech\/cloud-server-pricing\" target=\"_blank\">Cloud Server<\/a> or <a href=\"http:\/\/www.globo.tech\/dedicated-server-hosting\" target=\"_blank\">Dedicated Server<\/a>) with Ubuntu 16 installed.<br \/>\n\u2022 Apache installed as part of a LAMP stack<br \/>\n\u2022 All commands entered in root<\/p>\n<h2>Tutorial<\/h2>\n<p>Begin by installing Varnish on your system.<\/p>\n<p><code>apt-get install varnish -y<\/code><\/p>\n<p>Let&#8217;s change the port that Varnish uses by default. Open the Varnish configuration file.<\/p>\n<p><code>nano \/etc\/default\/varnish<\/code><\/p>\n<p>Go to the Alternative 2 section and change<\/p>\n<p><code class=\"gris\">DAEMON_OPTS=\"-a :6081 \\<\/code><\/p>\n<p>to<\/p>\n<p><code class=\"gris\"> DAEMON_OPTS=\"-a :80 \\<\/code><\/p>\n<p>You&#8217;ll need to change the Varnish VCL configuration as well.<\/p>\n<p><code> nano \/etc\/varnish\/default.vcl<\/code><\/p>\n<p>Remember, if you change the port in this file, you&#8217;ll need to make sure to set the same port in the Apache configuration at the next step.<\/p>\n<p><code class=\"gris\"> backend default {<br \/>\n.host = \"127.0.0.1\";<br \/>\n.port = \"8080\";<br \/>\n}<\/code><\/p>\n<p>Change the port in Apache Virtualhost to match the one you set previously in the Varnish VCL configuration file.<\/p>\n<p><code>nano \/etc\/apache2\/sites-enabled\/000-default.conf<\/code><\/p>\n<p><code class=\"gris\">&lt;VirtualHost *:80&gt;<\/code><\/p>\n<p>to<\/p>\n<p><code class=\"gris\">&lt;VirtualHost *:8080&gt;<\/code><\/p>\n<p>Restart Apache and Varnish so that they reflect the changes you&#8217;ve just made.<br \/>\n<code>systemctl restart apache2.service<br \/>\nsystemctl restart varnish.service<\/code><\/p>\n<p>You might run into some issues with installing Varnish on Ubuntu 16.<\/p>\n<p>If you get an error, check the process that&#8217;s running on your server.<\/p>\n<p><code>ps aux | grep vcache<\/code><br \/>\n<code class=\"gris\">vcache 15569 0.0 0.7 125044 7816 ? Ss 08:20 0:00 \/usr\/sbin\/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f \/etc\/varnish\/default.vcl -S \/etc\/varnish\/secret -s malloc,256m<br \/>\nvcache 15581 0.0 9.3 272012 94900 ? Sl 08:20 0:00 \/usr\/sbin\/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f \/etc\/varnish\/default.vcl -S \/etc\/varnish\/secret -s malloc,256m<\/code><\/p>\n<p>You may see that the port is still set to 6081 instead the one you set in \/etc\/default\/varnish. What this means is that the systemd service bypassed the configuration file.<\/p>\n<p>In order to solve the problem, edit the file \/lib\/systemd\/system\/varnish.service and change the port 6081 to 80. Also, you can change the malloc,256 for a different memory value at this time.<\/p>\n<p><code>nano \/lib\/systemd\/system\/varnish.service<\/code><\/p>\n<p><code class=\"gris\">[...]<br \/>\nExecStart=\/usr\/sbin\/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f \/etc\/varnish\/default.vcl -S \/etc\/varnish\/secret -s malloc,256m<br \/>\n[...]<\/code><\/p>\n<p>to<\/p>\n<p><code class=\"gris\">[...]<br \/>\nExecStart=\/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<br \/>\n[...]<\/code><\/p>\n<p>Reload and restart Apache and Varnish once more.<\/p>\n<p><code>systemctl daemon-reload<br \/>\nsystemctl restart apache2.service<br \/>\nsystemctl restart varnish.service<\/code><\/p>\n<p>Check your Varnish stats to make sure everything&#8217;s working correctly.<\/p>\n<p><code>varnishstat<\/code><\/p>\n<p>Finally, ensure that your web server and Varnish are operating normally.<\/p>\n<p><code>curl -I http:\/\/your_ip <\/code><\/p>\n<p><code class=\"gris\">HTTP\/1.1 200 OK<br \/>\nDate: Wed, 22 Jun 2016 08:32:03 GMT<br \/>\nServer: Apache\/2.4.18 (Ubuntu)<br \/>\nLast-Modified: Wed, 22 Jun 2016 08:18:20 GMT<br \/>\nVary: Accept-Encoding<br \/>\nContent-Type: text\/html<br \/>\nX-Varnish: 32771<br \/>\nAge: 0<br \/>\nVia: 1.1 varnish-v4<br \/>\nETag: W\/\"2c39-535d9949460b3-gzip\"<br \/>\nAccept-Ranges: bytes<br \/>\nConnection: keep-alive<\/code><\/p>\n<p>With Varnish installed, you can rest easy that your site will survive an unexpected surge of visitors and be better protected against any malicious attacks. If this guide was helpful to you, kindly share it with others who may also be interested. <\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>One of the problems with running a site with many users is that the constant requests for dynamic content can really bog down a web server. In order to solve this problem, many big companies turn to a tool called Varnish. It&#8217;s an HTTP accelerator that caches both static and dynamic content, thereby easing the<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-2139","post","type-post","status-publish","format-standard","hentry","category-web-hosting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to set up Varnish on Ubuntu 16 - Globo.Tech<\/title>\n<meta name=\"description\" content=\"This tutorial will show you how to set up Varnish on your Ubuntu 16 Server. Read now &amp; protect yourself against malicious attacks.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to set up Varnish on Ubuntu 16 - Globo.Tech\" \/>\n<meta property=\"og:description\" content=\"This tutorial will show you how to set up Varnish on your Ubuntu 16 Server. Read now &amp; protect yourself against malicious attacks.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/\" \/>\n<meta property=\"og:site_name\" content=\"Globo.Tech\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-30T22:01:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-09-30T16:58:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"963\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"GloboTech Communications\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"GloboTech Communications\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/\",\"url\":\"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/\",\"name\":\"How to set up Varnish on Ubuntu 16 - Globo.Tech\",\"isPartOf\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#website\"},\"datePublished\":\"2016-06-30T22:01:35+00:00\",\"dateModified\":\"2016-09-30T16:58:09+00:00\",\"author\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87\"},\"description\":\"This tutorial will show you how to set up Varnish on your Ubuntu 16 Server. Read now & protect yourself against malicious attacks.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.globo.tech\/learning-center\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to set up Varnish on Ubuntu 16\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#website\",\"url\":\"https:\/\/www.globo.tech\/learning-center\/\",\"name\":\"Globo.Tech\",\"description\":\"Welcome to the Official Globo.Tech Learning Center\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87\",\"name\":\"GloboTech Communications\",\"sameAs\":[\"http:\/\/www.gtcomm.net\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to set up Varnish on Ubuntu 16 - Globo.Tech","description":"This tutorial will show you how to set up Varnish on your Ubuntu 16 Server. Read now & protect yourself against malicious attacks.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/","og_locale":"en_US","og_type":"article","og_title":"How to set up Varnish on Ubuntu 16 - Globo.Tech","og_description":"This tutorial will show you how to set up Varnish on your Ubuntu 16 Server. Read now & protect yourself against malicious attacks.","og_url":"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/","og_site_name":"Globo.Tech","article_published_time":"2016-06-30T22:01:35+00:00","article_modified_time":"2016-09-30T16:58:09+00:00","og_image":[{"width":1920,"height":963,"url":"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png","type":"image\/png"}],"author":"GloboTech Communications","twitter_misc":{"Written by":"GloboTech Communications","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/","url":"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/","name":"How to set up Varnish on Ubuntu 16 - Globo.Tech","isPartOf":{"@id":"https:\/\/www.globo.tech\/learning-center\/#website"},"datePublished":"2016-06-30T22:01:35+00:00","dateModified":"2016-09-30T16:58:09+00:00","author":{"@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87"},"description":"This tutorial will show you how to set up Varnish on your Ubuntu 16 Server. Read now & protect yourself against malicious attacks.","breadcrumb":{"@id":"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.globo.tech\/learning-center\/set-up-varnish-ubuntu-16\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.globo.tech\/learning-center\/"},{"@type":"ListItem","position":2,"name":"How to set up Varnish on Ubuntu 16"}]},{"@type":"WebSite","@id":"https:\/\/www.globo.tech\/learning-center\/#website","url":"https:\/\/www.globo.tech\/learning-center\/","name":"Globo.Tech","description":"Welcome to the Official Globo.Tech Learning Center","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87","name":"GloboTech Communications","sameAs":["http:\/\/www.gtcomm.net"]}]}},"_links":{"self":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/comments?post=2139"}],"version-history":[{"count":5,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2139\/revisions"}],"predecessor-version":[{"id":2869,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/2139\/revisions\/2869"}],"wp:attachment":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/media?parent=2139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/categories?post=2139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/tags?post=2139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}