How to speed up Apache with Varnish HTTP cache on Ubuntu 18.04 LTS
0

Varnish is a proxy server centered on HTTP caching. It is designed as an HTTP accelerator and may act as a reverse proxy to your internet server (Apache or Nginx). Varnish has been used for high-profile and high-traffic web sites, together with Wikipedia, The Guardian, and The New York Instances.

On this tutorial, we’ll present you the best way to set up and configure the Varnish HTTP accelerator as a reverse proxy for the Apache internet server. The actual internet server Apache will run below a non-standard HTTP port (working on port 8080). And Varnish shall be working because the reverse proxy on HTTP port 80. For this information, we shall be utilizing the Ubuntu 18.04 LTS server.

What we’ll do

  1. Set up Apache Web server
  2. Change Apache Default Port
  3. Set up Varnish
  4. Configure Varnish
  5. Check the setup

Stipulations

  • Ubuntu 18.04 LTS
  • Root privileges

Step 1 – Set up Apache Web server

To begin, set up the Apache internet server. By default, it is obtainable within the Ubuntu repository.

Replace the Ubuntu repository after which set up Apache utilizing the apt command.

apt replace
apt set up -y apache2

After the set up is full, begin the Apache service and allow it to run mechanically at system boot time utilizing the next systemctl instructions.

systemctl begin apache2
systemctl allow apache2

The Apache internet server has been put in.

Subsequent, to permit everybody entry to the net server, we should enable HTTP and HTTPS on our firewall rule. By default, Ubuntu comes with a firewall package deal named UFW.

Open new SSH, HTTP and HTTPS ports utilizing the next UFW instructions.

ufw enable ssh
ufw enable http
ufw enable https

Now begin the firewall service and allow it to launch mechanically at system boot.

ufw allow

Sort ‘y’ and press Enter to verify.

New ports for HTTP, HTTPS, and SSH have been opened, and are accessible from outdoors of the community.

If you wish to check the Apache internet server, you need to use the netstat command and ensure apache is working below port 80.

netstat -plntu

Or you need to use the next curl command.

curl -I hakase-labs.co

You must get the end result as proven under.

Step 2 – Change Apache Default Port

On this tutorial, we shall be utilizing Apache because the backend server, and it’ll not run below the usual HTTP port 80. The Apache internet server shall be working on port 8080 as backend, and the usual HTTP port 80 shall be utilized by ‘Varnish’.

To vary the default apache port, we have to edit the apache configuration ‘ports.conf’ and the entire digital host configuration below the ‘sites-available’ listing.

Go to the Apache configuration listing.

cd /and so forth/apache2

Change port ’80’ with ‘8080’ within the Apache configuration ‘ports.conf’ and all digital host information below the ‘sites-available’ listing. We are able to do this by working the next sed instructions.

sed -i -e 's/80/8080/g' ports.conf
sed -i -e 's/80/8080/g' sites-available/*

Subsequent, check the Apache configuration and ensure there isn’t any error, then restart the Apache service.

apachectl configtest
systemctl restart apache2

Now Apache is working below HTTP port ‘8080’, examine it utilizing the ‘netstat’ command as proven under.

netstat -plntu

Ensure you have the end result as under.

So you’ll be able to see the Apache internet server is working on port ‘8080’.

Step 3 – Set up Varnish HTTP Accelerator

On this step, we shall be putting in Varnish from the Ubuntu repository. And for this information, we shall be utilizing Varnish 5.2.

Set up varnish utilizing the next apt command.

apt set up -y varnish

After the set up is full, begin Varnish and allow it to launch mechanically at system boot.

systemctl begin varnish
systemctl allow varnish

Varnish has been put in on the Ubuntu 18.04 system.

By default, it is working below ports ‘6081’ for public handle and ‘6082’ for localhost handle. Test it utilizing the netstat command under.

netstat -plntu

And you’ll get the default varnish port.

Step 4 – Configure Varnish as a Reverse Proxy for Apache

On this step, we’ll configure varnish on the entrance of the Apache internet server. Varnish shall be working below http port 80, and each request from shoppers shall be dealt with by it, earlier than being despatched to the Apache internet server working on port 8080.

– Backend Configuration

Go to the ‘varnish’ configuration listing and backup the default file ‘default.vcl’.

cd /and so forth/varnish/
cp default.vcl default.vcl.aseli

Edit the varnish configuration ‘default.vcl’ utilizing the vim editor.

vim default.vcl

Outline the ‘backend’ configuration on line 16. The backend for our setup is Apache, and it is’ working on port 8080. So the varnish configuration for our apache setup must be the next:

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

Save and exit.

– Operating Varnish on Port 80

Subsequent, we have to change the default varnish ports. The default ports for varnish is ‘6081’ and ‘6082’, and we have to change the port to the HTTP port 80 (just for public handle).

Edit the varnish parameter configuration in ‘/and so forth/default’ listing.

cd /and so forth/default/
vim varnish

Edit the ‘DAEMON_OPTS’ line, change the default port ‘6081’ for public handle with customary HTTP port ’80’ as proven under.

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

Save and exit.

Subsequent, we have to edit the varnish service file ‘varnish.service’. Go to the ‘/lib/systemd/system’ listing and edit the service file utilizing vim.

cd /lib/systemd/system/
vim varnish.service

On the ‘ExecStart’ line, change the varnish begin command as under.

ExecStart=/usr/sbin/varnishd -j unix,person=vcache -F -a :80 -T localhost:6082 -f /and so forth/varnish/default.vcl -S /and so forth/varnish/secret -s malloc,256m

Save and exit.

Reload the systemd service configuration after which restart varnish.

systemctl daemon-reload
systemctl restart varnish

Now examine varnish and ensure it is working on the HTTP port 80.

netstat -plntu

So Varnish is now working below HTTP port 80 as front-end for Apache internet server, which is on port 8080.

Step 5 – Testing

Check utilizing the curl command.

curl -I hakase-labs.co

Ensure you get the HTTP header as proven under.

Checking from the net browser, the URL of my check server is http://hakase-labs.co/. Select your server URL right here.

And we nonetheless get Apache content material.

Test the varnish log utilizing the ‘varnishncsa’ command.

varnishncsa

And we get log simply from the Apache entry log.

Varnish set up and configuration for the Apache internet server has been accomplished.

Report: Google argues the Huawei ban would harm its Android monopoly

Previous article

Samsung is bringing the $350 Galaxy A50 to the US

Next article

You may also like

Comments

Leave a reply

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

More in Apache