How to install Varnish cache on server ubuntu

Maybe you know that the varnish cache is quite necessary with magento 2 and it is very good with any website. Today, we will try to install varnish on server ubuntu and configure to work with apache2.

You can grab the repository by using the command line:

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

Next step, we add the repository to the list of apt sources by editing the file sources.list:

sudo nano /etc/apt/sources.list

Insert this command line:

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

You can use the key board CTR + O to save the file and CTR + X to exit.

Now, we run update apt-get and install varnish cache:

sudo apt-get update
sudo apt-get install varnish

Normally the apache2 will serve the content on port 80 for your website. Now we will change it to the port 8080 and the varnish cache will get content from apache2 ( port 8080 – you also change to another port ). Of Course the varnish cache will serve the content on port 80 for your website.

Editing the configuration of varnish cache to change port:

sudo nano /etc/default/varnish

You can find “Alternative 2” and make a configuration like:

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

Using CTR + O to save the file and CTR + X to exit.

Maybe you see the file /etc/varnish/default.vcl in previous step. Now you need to edit that file to change the port to 8080:

sudo nano /etc/varnish/default.vcl

And change configuration backend default like:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
    .first_byte_timeout = 600s;
}

Now we will continue change port for apache2:

sudo nano /etc/apache2/ports.conf

And change from “Listen 80” to “Listen 8080”

#Listen 80
Listen 8080

Continue to change setting virtual host ( you can see more configuration of a virtual host for apache here)

sudo nano /etc/apache2/sites-available/mageoneplus.conf
<Directory /webfolder/mageoneplus.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

<VirtualHost *:8080>
        ServerName mageoneplus.com
        ServerAlias www.mageoneplus.com
        ServerAdmin [email protected]
        DocumentRoot /webfolder/public_html
        ErrorLog /webfolder/mageoneplus.com/logs/error.log
</VirtualHost>

Now we can restart apache and varnish to get they work with new configurations:

sudo service apache2 restart
sudo service varnish restart

Checking the apache with new port by going to the page http://your-domain.com:8080 , you will see the response header like

apache-header

And checking varnish cache by going to the page http://your-domain.com , you will see the response header like

varnish-cache-reponse-header

If the varnish cache still don’t serve on the port 80. You can fix this problem by follow the topic How to fix problem to change port for varnish cache

Add Comment

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