tokar93
By:
tokar93

Configure Varnish with Apache on Ubuntu 14.04

November 13, 2014 22.9k views

Hi

How can i install and configure Varnish to work whit Apache on Ubuntu 14.04?

1 comment
  • I followed these instructions and my site is being served however I'm not seeing any varnish headers. Am I missing a step?

    Thanx

4 Answers

Configuring Varnish to work with Apache on Ubuntu 14.04 is largely similar to this article covering 12.04.

I just ran through a basic set up on 14.04. The first step is to install Apache and Varnish:

sudo apt-get install varnish apache2

Then we need to configure Varnish to listen on port 80. Edit the file /etc/default/varnish Find the section begin with "Alternative 2, Configuration with VCL" Make sure it's uncommented and update the port:

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

Next we need to adjust Apache to listen on port 8080. Edit /etc/apache2/ports.conf and change Listen 80 to Listen 8080 Then edit /etc/apache2/sites-enabled/000-default.conf and change <VirtualHost *:80> to <VirtualHost *:8080>

Finally, restart Varnish and Apache with:

sudo service apache2 restart
sudo service varnish restart

Varnish is now sitting in front of Apache and serving cached content.

by Etel Sverdlov
Varnish is an HTTP accelerator and a useful tool for speeding up a server, especially during a times when there is high traffic to a site. It works by redirecting visitors to static pages whenever possible and only drawing on the virtual private server itself if there is a need for an active process. This tutorial covers setting up Varnish with Apache on Ubuntu 12.04.
  • Nice. Should it be any performance increase to run varnish on a separate VPS?

    Like this guide?

    https://www.digitalocean.com/community/tutorials/how-to-configure-varnish-cache-4-0-with-ssl-termination-on-ubuntu-14-04

    Varnish Cache is a caching HTTP reverse proxy, or HTTP accelerator, which reduces the time it takes to serve content to a user. In this tutorial, we will cover how to use Varnish Cache 4.0 to improve the performance of your existing web server. We will also show you a way to add HTTPS support to Varnish, with Nginx performing the SSL termination.
  • It really depends on your needs. As Varnish caches content in memory, the main consideration for whether or not to split it out onto another server would be memory usage.

  • How should i know how match memory i need?

  • how about if I have multiple virtual host / domain?
    for the example I have an IP : 111.111.111.111 , then i have domain.com , domain2.com, sub.domain.com, sub.domain2.com . is configuration still same?

  • I have run up a 1404 droplet with Magento using SSL. Should I change the 'magneto-ssl.conf' from 443 to 8080? I feel like I need to leave it at 443 for ssl to work properly.

    • @saulpublic HTTPS traffic needs to be on port 443. Generally, you'll want to set up Apache or Nginx to listen on 443 for SSL termination with a proxy back to Varnish on localhost. This article uses Nginx, but explains the concepts:

      You could accomplish something similar with Apache using:

      <VirtualHost *:443>
          ServerName www.example.com
      
          ProxyPreserveHost On
          ProxyPass / http://127.0.0.1:80/
          RequestHeader set X-Forwarded-Port "443"
          RequestHeader set X-Forwarded-Proto "https"
      
          SSLEngine On
          SSLCertificateFile /etc/apache2/ssl/example.com.crt
          SSLCertificateKeyFile /etc/apache2/ssl/example.com.key
          SSLCertificateChainFile /etc/apache2/ssl/example.com.chain
      </VirtualHost>
      

      This way, Apache deals with the SSL while Varnish is still serving cached content.

      Varnish Cache is a caching HTTP reverse proxy, or HTTP accelerator, which reduces the time it takes to serve content to a user. In this tutorial, we will cover how to use Varnish Cache 4.0 to improve the performance of your existing web server. We will also show you a way to add HTTPS support to Varnish, with Nginx performing the SSL termination.

@emshidiq I run multiple sites under one VPS and would like to know the answer too. Thanks for asking.

For Varnish to work on Ubuntu 15.04.

First grep:
```sudo grep -R 'ExecStart=/usr/sbin/varnishd' /etc/


If result is "/etc/systemd/system/multi-user.target.wants/varnish.service"
```sudo nano /etc/systemd/system/multi-user.target.wants/varnish.service

Change the ExecStart to this only.
```ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m


Save and exit, restart Varnish. 
```systemctl daemon-reload
systemctl restart varnish.service

I'm using ubuntu 16 , and having link problems:

http://example.com/test is redirecting to http://example.com:8080/test and returning error :|

Have another answer? Share your knowledge.