• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Tutsplanet

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Twitter Trends
  • Tools
  • About Us

Linux

How To Install and Configure Varnish with Apache on Ubuntu 16.04

Mar 5, 2017 Editorial Staff 4 Comments

Share
Tweet
Share

Varnish is an HTTP accelerator and it’s a tool for speeding up a server. It used as a reverse proxy for your server, as Varnish as front and Apache as back.

Setting Up

Note : You should have sudo access to the server in order to install Varnish.

First  step,  executes the below commands this will take care installation of varnish.

After installing the Varnish basically you will get two configuration files 1) /etc/default/varnish 2) /etc/varnish/default.vcl

You can check both files mentioned above to see what in there. Search for DAEMON_OPTS, in file /etc/default/varnish, you see the below results. Here come the varnish listening port as 6081 with value of parameter -a. Do not edit any files now, just keep on reading.

Here’s the breakdown of the options we’ve used above:

-a – this is the address and the port Varnish listens to mainly. In the configuration above, we didn’t specify the address. This means that Varnish will listen for requests on the same server where the web server is installed. Usually, people will have to install Varnish on a different server, so that it’s not subjected to the load as the same web server. It’s a best practice to reduce the server load.
-T – the address and the port the management interface listens to.
-f – allows you to specify your own VCL configuration file instead of the default.
S – the path to the file containing the secret used for authorizing access to the management port.
-s – used for specifying the storage backend. You can use any of the options on this page. Usually its malloc, which uses memory as the cache. This asks for the maximum memory size to be specified. In this case, its 256m which is 256MB.

Configuring the Varnish.

What we have to do is let Varnish sit on the front and apache on the back. It means all the HTTP request will hit to the Varnish cache, if cache match exists it will supply that relevant page, otherwise the request will move to the backup server, that is apache.

By default, Varnish listens on port 6081, which is not useful for production sites. So what we want is Varnish should listen port 80(default HTTP port) and apache listens to other ports such as 8080.

To achieve this first change /etc/apache2/ports.conf ,change this Listen 80 to Listen 8080. So apache won’t be your first door.

Now, you have to update your VirtualHost file accordingly. Edit /etc/apache2/sites-available/000-default.conf (or any other VirtualHost config file you use), replace the line <VirtualHost :80> with <VirtualHost :8080>.

Varnish to listen port 80, so we would change DAEMON_OPTS=”-a :6081 to DAEMON_OPTS=”-a :80 in /etc/default/varnish as I said before.
I was stuck here. Varnish wasn’t taking that config file.
It’s a tricky part, I followed the official tutorial of Varnish and I didn’t have any luck with that. But from searching a lot in google I got this article, such a life saver http://deshack.net/how-to-varnish-listen-port-80-systemd/ . Great thanks to Mattia Migliorini!.

The easy solution


We come up with something like this:

Which is similar to what we saw before. We already now that we have to change the port passed as a value to the -a flag:

Then restart the varnish
# systemctl daemon-reload
# systemctl reload varnish.service

Then restart your apache service.
sudo service apache2 restart

Testing

Create one php file test.php, displaying some records from a database. Try with http://yourdomain.com/test.php. Test your headers you can see Varnish headers there.


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Reader Interactions

Comments

  1. Alex Morco says

    Apr 11, 2018 at 10:41 am

    It has fixed the issue for my store, It is hosted with other hosting company but I am also using Magento store with Cloudways and setup Varnish in minutes through their managed platform, https://www.cloudways.com/blog/how-to-configure-varnish-cache-magento/, This is the guide helped me to configure Varnish.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Quick Links

  • Top 21 Website Ideas To Make Money Online in 2021
  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Replace “\n” with new line characters, using Notepad++
  • Using Third-Party Libraries in Codeigniter
  • Upload Multiple Images and Store in Database using PHP and MySQL.
  • Hierarchical Tree view Category Example in Laravel
  • Laravel Image Intervention Tutorial With Example
  • How to import sql file in MySQL database using PHP?
  • Free VAT Calculator Online

Subscribe

* indicates required

Search Here

Share

   

Hot topics

  • Replace “\n” with new line characters, using Notepad++ 57 views
  • How to enter new line in Microsoft Teams? 38 views
  • Open a URL in a new tab using JavaScript 28 views
  • A Simple YouTube Video Downloader Script in PHP 25 views
  • Laravel WhereHas() Eloquent Example 22 views
  • How to change PHP version on MAMP – Mac OSX 18 views
  • How to Create PDF File From HTML Form Using Fpdf? 17 views
  • PHP: Implode () with quotes 15 views
  • Simple PHP Shopping Cart 15 views
  • Get Uploads Directory Path in WordPress 14 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube

Copyright © 2022 · Planet on Genesis Framework · Powered By BunnyCDN . Network wallpapernoon.com