How to Install Varnish with Apache on Ubuntu

Writer: Ryan Frankel

Ryan Frankel, CTO and Contributing Expert

Ryan began developing websites in the late '90s and has personally tested just about every web host and cloud platform worth trying on the market today. With a masters degree in electrical and computer engineering from the University of Florida, he leverages his extensive knowledge of hardware, software, and their engineering relationship to inform HostingAdvice readers of the technical implications of their hosting choices. Ryan's subject matter expertise includes, but is not limited to, WordPress, cloud infrastructure management, product UI/UX design, and popular web development languages such as JavaScript and PHP.

Editor: Lillian Castro

Lillian Castro, Senior Editor

Lillian Castro brings more than 30 years of editing and journalism experience to our team. She has written and edited for major news organizations, including The Atlanta Journal-Constitution and the New York Times, and she previously served as an adjunct instructor at the University of Florida. Today, she edits HostingAdvice content for clarity, accuracy, and reader engagement.

Reviewer: Cristian Lopez

Cristian Lopez, News Manager

Cristian Lopez uses his Business Marketing background from the University of Illinois at Chicago to create comfortable environments for customers, clients, and colleagues to share their thoughts and ideas openly. From interviewing tech leaders to conducting UX market research projects, Cristian knows the importance of storytelling — a key variable for innovation and inspiration. His goal at HostingAdvice is to wow readers on the ever-evolving nature of the tech industry and bring his audience the most reliable and exciting content on all things hosting.

Disclosure: When you purchase through links on our site, we often earn referral fees from partners. For more information, see our ad disclosure and review policy.

Follow Us:
2.7k
16.4k
11.7k
173
4.1k

Varnish is an amazing frontend cache that is useful for serving static pages and reducing load on your server. In periods of high server activity and traffic, Varnish can be a life saver. Also, Varnish is extremely fast and effective at reducing your page load times, which helps with your SERPs and improves user experience.

1. Add the Varnish Repository

Varnish can be installed by adding the Varnish repository to your sources and using apt-get to install it.

To add the repository, run the following command:

$ sudo curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

2. Edit List of apt Sources

Open the /etc/apt/sources.list in your favorite text editor so we can add the Varnish repository.

$ sudo vim /etc/apt/sources.list

Next, add the Varnish repository at the end of the file.

$ deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0

Save your work and exit out to the command line.

3. Install Varnish

Use the usual apt-get to install Varnish.

$ sudo apt-get update $ sudo apt-get install varnish

4. Configure Varnish DAEMON_OPTS

Now it is time to configure Varnish. This process will vary depending upon your CMS and/or framework and how your site is set up, but we will get you started with a basic configuration.

Open the /etc/default/varnish file:

$ sudo vim /etc/default/varnish

Look for “alternative 2” and uncomment the following code. Change the ports as necessary.

DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"

Basically, we are setting Varnish to use Port 80, using /etc/varnish/default.vcl as our configuration file, and allocating 256MB of memory.

5. Varnish VCL Configuration

The VCL file in Varnish is extremely important and allows for very specific configurations indicating which pages to cache, how to treat cookies, what to do about different user-agents, and so on.

The basics will be covered here, but you will need to look closely at the VCL file in the future to get optimal performance. Don’t worry because it’s straightforward and you can do it!

Open the VCL file:

$ sudo vim /etc/varnish/default.vcl

Now we have to tell Varnish where to grab content for your site. Here we are using the same server and we will set up Apache on port 8888.

backend default {   .host = “127.0.0.1”;   .post = “8888”; }

Save and exit the file.

6. Change the Apache Ports

By default, Apache is configured to run on Port 80. We will change the port to be 8888.

Open the Apache ports file:

$ sudo vim /etc/apache2/ports.conf

Now look for the NameVirtualHost and Listen lines and change them to Port 8888.

NameVirtualHost 127.0.0.1:8888 Listen 127.0.0.1:8888

7. In the Virtual Host, Change the Port to 8888

In the settings for your virtual host, you will need to tell Apache to look on Port 8888.

$ sudo vim /etc/apache2/sites-available/default

Now change the port:

 

Save the file.

8. Restart Apache and Varnish

$ sudo service apache2 restart $ sudo service varnish restart

9. Watch in Amazement at the Details of your Varnish Server Running

You can check in on Varnish with its built-in stats readout.

$ varnishstat

You are done! That was easy wasn’t it? Now you are well on your way to having a fast and efficient server configuration.

Advertiser Disclosure

HostingAdvice.com is a free online resource that offers valuable content and comparison services to users. To keep this resource 100% free, we receive compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). HostingAdvice.com does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.

Our Editorial Review Policy

Our site is committed to publishing independent, accurate content guided by strict editorial guidelines. Before articles and reviews are published on our site, they undergo a thorough review process performed by a team of independent editors and subject-matter experts to ensure the content’s accuracy, timeliness, and impartiality. Our editorial team is separate and independent of our site’s advertisers, and the opinions they express on our site are their own. To read more about our team members and their editorial backgrounds, please visit our site’s About page.

About the Author

CTO and Contributing Expert

Ryan Frankel has been a professional in the tech industry for more than 20 years and has been developing websites for more than 25. With a master's degree in electrical and computer engineering from the University of Florida, he has a fundamental understanding of hardware systems and the software that runs them. Ryan now sits as the CTO of Digital Brands Inc. and manages all of the server infrastructure of their websites, as well as their development team. In addition, Ryan has a passion for guitars, good coffee, and puppies.

« BACK TO: HOW-TO

Meet the Experts

Our team of experts with a combined 50+ years of experience in web hosting serve insight and advice to more than 20 million users!

We Know Hosting

$

4

8

,

2

8

3

spent annually on web hosting!