BIP KB:
How To Install Varnish 4 With Apache 2 On Ubuntu 14.04
Article By justin
![]() |
install varnish IntroductionApache 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 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: vps, apache, ubuntu, install, apache2, web server, Server, Hosting, Cache, Varnish, Varnish Cache, Apache 2, apache php, apache server, enable https, root server, ubuntu apache2 ubuntu, ubuntu hosts server, ubuntu web server, Varnish 4, vps hosting, vps server, website server
Spin Up A VPS Server In No Time Flat
Simple Setup
Full Root Access
Straightforward Pricing
DEPLOY A SECURE VPS SERVER TODAY!Leave a Reply
Feedbacks
![]() This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International License. |