Install Varnish Cache with Apache on AlmaLinux 9: Easy Setup
This guide will walk you through the process of installing Install Varnish Cache with Apache on AlmaLinux 9. Varnish Cache functions as an HTTP reverse proxy and is often referred to as a front-end accelerator. It’s important to note that Varnish isn’t a standalone solution; it requires a dedicated web server like NGINX or Apache to operate.
Varnish can be effectively used to cache both dynamic and static content, significantly enhancing your website’s speed and overall server performance. Let’s delve into the steps required to set up Apache Varnish Cache on AlmaLinux 9.
To successfully Install Varnish Cache with Apache on AlmaLinux 9, there are a few prerequisites that need to be addressed. Let’s review these requirements before we begin.
1. Requirements For Apache Varnish Cache Setup
Firstly, you’ll need to log in to your server as a non-root user with sudo
privileges and configure a basic firewall. You can refer to our guide on Initial Server Setup with AlmaLinux 9 for detailed instructions.
Additionally, ensure that you have Apache already installed on your server. If not, follow our guide on How to Install Apache on AlmaLinux 9.
2. Apache Configuration on AlmaLinux 9
By default, Apache listens on port 80
. To integrate with Varnish, you need to change Apache’s listening port to 8080
. Open the Apache configuration file using your preferred text editor. In this example, we’ll use vi
:
sudo vi /etc/httpd/conf/httpd.conf
Search for the Listen
line and modify it to 8080
:
Listen 8080
Save the changes and close the file. Now, restart Apache on AlmaLinux 9 to apply the new configuration:
sudo systemctl restart httpd.service
Next, create a test HTML file to verify the configuration:
sudo touch /var/www/html/test.html
Use the curl
tool to test the server on port 8080
. This confirms that Apache is correctly configured:
curl -I http://localhost:8080/test.html
The output should resemble the following:

With Apache configured, you’re now ready to Install Varnish Cache with Apache on AlmaLinux 9.
3. Install Varnish Cache on AlmaLinux 9
First, add the EPEL repository to your server:
sudo dnf install -y epel-release
Install the necessary dependency packages:
sudo dnf install dnf-plugins-core
Add Varnish Cache Repository
By default, Varnish isn’t available in the AlmaLinux 9 base repository. Add the Varnish Cache repository to your system:
curl -s https://packagecloud.io/install/repositories/varnishcache/varnish72/script.rpm.sh | bash
Install Varnish Cache
Now, install Varnish on AlmaLinux 9 using the following command:
sudo dnf install varnish
Start and Enable Varnish Cache
After the installation is complete, enable Varnish (to start automatically upon system boot), and verify the status using the commands below:
# sudo systemctl start varnish
# sudo systemctl enable varnish
# sudo systemctl status varnish
4. Configure Varnish Cache on AlmaLinux 9
Configure Varnish to listen on port 80
by editing the /usr/lib/systemd/system/varnish.service
file:
sudo vi /usr/lib/systemd/system/varnish.service
Find the ExecStart
line and modify the configuration to change port 6081
to 80
:
...
ExecStart=/usr/sbin/varnishd
-a :<mark>80</mark>
-a localhost:8443,PROXY
-f /etc/varnish/default.vcl
-P %t/%N/varnishd.pid
...
Save the changes and close the file. Then, restart Varnish Cache on AlmaLinux 9:
# sudo systemctl daemon-reload
# sudo systemctl restart varnish
Now you can test your installation to make sure you successfully Install Varnish Cache with Apache on AlmaLinux 9.
5. Test Varnish Installation on AlmaLinux 9
From your terminal, run the following command:
curl -I http://localhost/test.html
The output should look like this:
<mark>Output</mark>
HTTP/1.1 200 OK
Date: Tue, 29 Nov 2022 09:06:27 GMT
Server: Apache/2.4.53 (AlmaLinux)
Last-Modified: Tue, 29 Nov 2022 08:56:11 GMT
ETag: "0-5ee9828ded2e1"
Content-Length: 0
Content-Type: text/html; charset=UTF-8
<mark>X-Varnish: 2
</mark>Age: 0
<mark>Via: 1.1 (Varnish/7.2)
</mark>Accept-Ranges: bytes
Connection: keep-alive
This confirms that Varnish Cache is active and running on your server. You can test it from your local workstation by substituting your instance’s IP address.
-
Linux:
curl -I http://ip-address/test.html
-
Windows PowerShell:
PS> curl -Uri http://ip-address/test.html
Note: Ensure that the Varnish headers appear in the output.
Conclusion
You have now successfully learned how to Install Varnish Cache with Apache on AlmaLinux 9.
Hope you enjoy it. You may like these articles:
Install Varnish Cache with Nginx on AlmaLinux 9
Install Varnish Cache with Apache on Ubuntu 22.04
Alternative Solutions for Caching on AlmaLinux 9
While Varnish Cache is a powerful solution, alternative approaches exist for caching content on AlmaLinux 9. Here are two different ways to achieve similar results:
1. Using Nginx as a Reverse Proxy and Cache:
Nginx, like Varnish, can function as a reverse proxy. It also has built-in caching capabilities, eliminating the need for a separate caching layer. This approach can simplify your infrastructure and reduce the number of components you need to manage.
-
Explanation: Nginx’s
proxy_cache
directive allows you to define a cache zone in memory and/or on disk. You can configure which content to cache based on URL patterns, HTTP headers, and other criteria. Nginx will then serve cached content directly, bypassing the backend Apache server for eligible requests. -
Configuration Example:
First, open your Nginx configuration file (usually located at
/etc/nginx/nginx.conf
or/etc/nginx/conf.d/default.conf
). Add the following lines within thehttp
block:http { proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m max_size=1g; proxy_cache_key "$scheme$request_method$host$request_uri"; proxy_cache_valid 200 302 60m; proxy_cache_valid 404 1m; server { listen 80; server_name your_domain.com; location / { proxy_pass http://localhost:8080; # Assuming Apache is running on port 8080 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache my_cache; proxy_cache_valid 200 302 60m; # Cache valid for 60 minutes for these status codes proxy_cache_use_stale error timeout invalid_header updating; # Serve stale content in case of errors add_header X-Cache-Status $upstream_cache_status; # Add header to indicate cache status } } }
proxy_cache_path
: Defines the cache path, levels of subdirectories, a name for the cache zone (my_cache
), its size (10MB), inactivity time (60 minutes), and maximum size (1GB).proxy_cache_key
: Defines the key used to identify cached content.proxy_cache_valid
: Defines how long to cache responses with specific HTTP status codes.proxy_pass
: Specifies the backend Apache server to forward requests to.proxy_cache
: Enables caching using the defined cache zone.add_header
: Adds a custom header to responses indicating whether the content was served from the cache.
After making these changes, restart Nginx:
sudo systemctl restart nginx
2. Utilizing a Content Delivery Network (CDN):
A CDN is a distributed network of servers that caches content closer to users, reducing latency and improving website performance. This is a more global approach than Varnish, distributing the caching across multiple geographic locations.
-
Explanation: When a user requests content, the CDN serves it from the nearest server in its network. This reduces the distance the data needs to travel, resulting in faster loading times. CDNs also offer other benefits, such as DDoS protection and SSL termination.
-
Implementation:
- Choose a CDN provider (e.g., Cloudflare, Akamai, Amazon CloudFront).
- Configure your domain’s DNS records to point to the CDN.
- The CDN will automatically cache your website’s static content (images, CSS, JavaScript).
- For dynamic content, you can configure the CDN to cache it based on specific rules (e.g., cache specific API endpoints).
Using a CDN often involves minimal code changes on your server. Most configuration is done through the CDN provider’s control panel.
These alternative solutions provide different approaches to caching, each with its own advantages and disadvantages. The best choice depends on your specific requirements, infrastructure, and budget. While Install Varnish Cache with Apache on AlmaLinux 9 is a viable solution, understanding these alternatives allows for a more informed decision.