BIP KB:
How To Install Varnish 4 With Apache 2 On Ubuntu 14.04

Article By justin


img

install varnish cache

install varnish Introduction

Apache is popular web server used by most web hosting companies. install Varnish cache - an HTTP accelerator and reverse proxy. We can use it with any HTTP server. In this example, we will be using Apache 2.

As a web server, Apache can use a considerable amount of server resources to serve pages. If you are running a high-traffic website, then you might need an HTTP accelerator to boost server performance. Varnish will help you with that.

Install Apache server

Activate it by using the following commands:

sudo apt-get update
sudo apt-get install apache2-mpm-event

You can test Apache's server status with this command:

sudo service apache2 status

If the service is running, "apache2 is running" will be printed to your terminal. Otherwise, you can start the service with this command:

sudo service apache2 start

install varnish the latest stable version

This version is not available in Ubuntu Repository by default, so you need to run the following commands to install varnish latest.

sudo apt-get install apt-transport-https
sudo curl https://repo.varnish-cache.org/ubuntu/GPG-key.txt | apt-key add -
echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list
sudo apt-get update
sudo apt-get install varnish

Configure Varnish Cache

Here we are going to change the server port to 80. Run the following command:

sudo nano /etc/default/varnish

Now look for DAEMON_OPTS=" under Alternative 2, Configuration with VCL. Change the DAEMON_OPTS=" section to match the following lines. This is only a port update.

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

Press CTRL + X, then Y to save the file.

Configure Apache

We need to change the listening port of Apache from 80 to 8080.

Edit the ports file by run the following command:

sudo nano /etc/apache2/ports.conf

Change the Listen 80 to Listen 8080.

Next, update the virtual host fiinstall varnishle...

sudo nano /etc/apache2/sites-available/000-default.conf
... change to .

If you have other virtual host files, then they should be updated as well.

Restart both services.

sudo service apache2 restart
sudo service varnish restart

You're all set. See the following sections for advanced setup tips.

View stats

Run the following command to show the stats:

varnishstat

Advanced VCL settings

You can edit the default.vcl file for various features.

Enable leverage browser caching

To enable browser caching for media files, your vcl_backend_response should match the following configuration.

sub vcl_backend_response {
    if (bereq.url ~ "\.(png|gif|jpg|swf|css|js)$") {
    unset beresp.http.set-cookie;
    set beresp.http.cache-control = "max-age = 2592000";
  }
}

This will improve your site speed and SEO ranking.

Purge cache

To clear the cache, you can change vcl_recv to match the following configuration:

sub vcl_recv {
     if (req.method == "PURGE") {
            return (purge);
     }
 }

After making this change, you can send a curl request in your ssh session with the following format:

curl  -XPURGE

Here, -XPURGE will send the purge request to the server.




Tags: , , , , , , , , , , , , , , , , , , , , , ,

Spin up a VPS server in no time flat

Simple setup. Full root access. Straightforward pricing.

DEPLOY VPS SERVER

Leave a Reply



Feedbacks