Varnish Cache is a popular caching solution that can be used to speed up the delivery of content on your web server. It works by caching frequently accessed content in memory so that subsequent requests can be served from the cache instead of being processed by the web server. In this article, we will guide you through the process of installing and configuring Varnish Cache for Apache on CentOS.
Install Apache
The first step is to install Apache on your CentOS server. You can do this by running the following command:
sudo yum install -y httpd
Next, you need to configure Apache to work with Varnish. Open the Apache configuration file in your favorite text editor:
sudo nano /etc/httpd/conf/httpd.conf
Find the line that starts with Listen
and change it to:
Listen 127.0.0.1:8080
This tells Apache to listen on port 8080 instead of the default port 80. Save and close the file.
Restart and enable Apache
sudo systemctl start httpd.service
sudo systemctl enable httpd.service
Install Varnish Cache
sudo yum install -y epel-release
sudo yum install -y pygpgme yum-utils
To include the Varnish repository, you need to make modifications to the /etc/yum.repos.d/varnish60lts.repo file.
sudo nano /etc/yum.repos.d/varnish60lts.repo
Paste the following lines:
[varnish60lts]
name=varnishcache_varnish60lts
baseurl=https://packagecloud.io/varnishcache/varnish60lts/el/7/x86_64
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish60lts/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
Update the yum cache for the Varnish repo:
sudo yum -q makecache -y --disablerepo='*' --enablerepo='varnish60lts'
To install Varnish on your CentOS server you need to run the following command:
sudo yum install -y varnish
sudo systemctl enable --now varnish
This will install Varnish Cache and all its dependencies on your server.
Configure Varnish Cache
Configure Varnish to listen at port 80, from the default of 6081. Edit varnish.service with nano.
sudo nano /usr/lib/systemd/system/varnish.service
Change the line beginning with ExecStart from port 6081 to port 80, then save and close the file. The line should like like this when finished.
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
Save and close the file when you’re done.
Start Varnish
Now that everything is configured, it’s time to start Varnish Cache. You can do this by running the following command:
sudo systemctl daemon-reload
sudo systemctl restart varnish
This will start the Varnish Cache service.
Test Varnish Cache
You can also use the curl
command to request a page from your web server:
curl -I http://server-IP
The output should show you whether the request was served from the cache or from the backend server.
Conclusion
You have successfully installed and configured Varnish Cache for Apache on CentOS. By caching frequently accessed content, Varnish Cache can significantly improve the performance of your web server, making it faster and more responsive for your users.
1 Comment
How To Install Tomcat On CentOS 7 - Virtono Community · April 29, 2023 at 4:42 PM
[…] the default Tomcat root page (tomcat-webapps), as well as the Tomcat Web Application Manager and Virtual Host Manager […]