First, we are going to add the Varnish repository because it does not exist in the default Varnish repository.<\/p>\n
tee \/etc\/apt\/sources.list.d\/varnishcache_varnish70.list > \/dev\/null <<-EOF\r\ndeb https:\/\/packagecloud.io\/varnishcache\/varnish70\/ubuntu\/ focal main\r\ndeb-src https:\/\/packagecloud.io\/varnishcache\/varnish70\/ubuntu\/ focal main\r\nEOF\r\n<\/pre>\nOnce added, update the system<\/p>\n
and<\/p>\n
sudo apt-update -y<\/pre>\nInstall the varnish cache with the commands below:<\/p>\n
sudo apt install varnish -y<\/pre>\nOnce installed, start and enable the service.<\/p>\n
sudo systemctl start varnish && sudo systemctl start varnish<\/pre>\nTo check if the service is up and running, type the following command:<\/p>\n
sudo systemctl status varnish<\/pre>\nYou should receive the following output:<\/p>\n
root@host:~# systemctl status varnish\r\n\u25cf varnish.service - Varnish Cache, a high-performance HTTP accelerator\r\n Loaded: loaded (\/lib\/systemd\/system\/varnish.service; enabled; vendor preset: enabled)\r\n Active: active (running) since Wed 2022-09-21 16:13:26 CDT; 1 week 5 days ago\r\n Docs: https:\/\/www.varnish-cache.org\/docs\/\r\n man:varnishd\r\n Main PID: 2449 (varnishd)\r\n Tasks: 217 (limit: 4575)\r\n Memory: 92.4M\r\n CPU: 11min 37.355s\r\n CGroup: \/system.slice\/varnish.service\r\n \u251c\u25002449 \/usr\/sbin\/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f \/etc\/varnish\/default.vcl -S \/etc\/varnish\/secret -s malloc,256m\r\n \u2514\u25002471 \/usr\/sbin\/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f \/etc\/varnish\/default.vcl -S \/etc\/varnish\/secret -s malloc,256m\r\n\r\n<\/pre>\nBy default, the varnish service is running on port 6081<\/b>. You can check this with the command below:<\/p>\nnetstat -tunlp | grep 6081<\/pre>\nYou should receive the following output:<\/p>\n
root@host:~# netstat -tunlp | grep 6081\r\ntcp 0 0 0.0.0.0:6081 0.0.0.0:* LISTEN 2449\/varnishd\r\ntcp6 0 0 :::6081 :::* LISTEN 2449\/varnishd\r\n<\/pre>\nThis is another proof that the varnish cache is installed successfully and the service is up and running.<\/p>\n
<\/span>Step 3. Install Apache2<\/span><\/h2>\nFirst, we need to install the Apache Web server to configure Varnish later. To install the Apache Web server execute the following command:<\/p>\n
sudo apt install apache2 -y<\/pre>\nTo start and enable the Apache service execute the following command:<\/p>\n
sudo systemctl start apache2 && sudo systemctl enable apache2<\/pre>\nTo check the status of the service:<\/p>\n
sudo systemctl status apache2<\/pre>\nYou should receive the following output:<\/p>\n
root@host:~# sudo systemctl status apache2\r\n\u25cf apache2.service - The Apache HTTP Server\r\n Loaded: loaded (\/lib\/systemd\/system\/apache2.service; enabled; vendor preset: enabled)\r\n Active: active (running) since Wed 2022-09-21 15:52:40 CDT; 1 week 5 days ago\r\n Docs: https:\/\/httpd.apache.org\/docs\/2.4\/\r\n Main PID: 767 (apache2)\r\n Tasks: 55 (limit: 4575)\r\n Memory: 13.9M\r\n CPU: 1min 15.604s\r\n CGroup: \/system.slice\/apache2.service\r\n \u251c\u2500 767 \/usr\/sbin\/apache2 -k start\r\n \u251c\u2500123603 \/usr\/sbin\/apache2 -k start\r\n \u2514\u2500123604 \/usr\/sbin\/apache2 -k start\r\n\r\n<\/pre>\nBy default, the Apache service is running on port 80<\/b>. We need to change that for we can configure the Varnish cache to work with the Apache web server late.<\/p>\n
To change the Apache port from 80<\/b> to 8080<\/b>, open the file \/etc\/apache2\/ports.conf<\/b> with your favorite text editor and change the Listen 80 to Listen 8080 to look like this:<\/p>\nListen 8080\r\n<\/pre>\nSave the file, close it and restart the Apache service.<\/p>\n
sudo systemctl restart apache2<\/pre>\nTo check if everything is OK, execute the following command:<\/p>\n
netstat -tunlp | grep apache2<\/pre>\nroot@host:# netstat -tunlp | grep apache2\r\ntcp6 0 0 :::8080<\/b> :::* LISTEN 127811\/apache2\r\n<\/pre>\n