In this article, we are going to talk about how to configure Apache2 to control browser caching. If you want to reduce the consumption of your server’s resources, improved responsiveness, bandwidth utilization, availability of content during network interruptions, and give your end users a faster experience, then you need to use the caching that will allow all of this.
A cache is a method for temporarily storing the most requested content so that future requests for that content will be more quickly served by temporary storage (cache) than from the primary location. By using caching, you are efficiently reusing the previously retrieved data. Today we are configuring browser caching control on Apache 2. Let’s get started.
Table of Contents
1. Prerequisites
- ssh access in VPS;
- installed Apache2 web server;
- Basic Linux knowledge (navigating, opening files, editing files, saving files etc…);
2. Verify Modules
Usually, our servers already have included file_cache that is needed to control the cache of the browser. However, we need to make sure that our Apache2 together with our module are installed and ready to accept the directives. There is a simple way to verify our module. To list the Apache modules, we will use the apachectl command to list the modules and pipe with the grep command to filter out our results and show only the modules we need.
We can verify the file_cache module with the following command:
apachectl -M | grep file_cache
the output should be:
file_cache_module (shared)
If you do not have anything in the output after running these commands or it’s simply blank then the module is not installed. You need to have it installed in order to continue with this tutorial.
3. Enable File Caching
To use the functionality of the file_cache module you need to enable it first. If you are running CentOS 7 or Ubuntu 16.04, this module by default it is not configured in the Apache so this module will not load. We will show you how to configure and enable the file_cache module in CentOS 7 and Ubuntu 16.04.
4. Enable File Caching on Ubuntu
If you are using Ubuntu 16.04. you can enable the file_cache module with the following command:
a2enmod file_cache
Next step is to edit the Apache main configuration file. Open the Apache main configuration file by typing:
nano /etc/apache2/apache2.conf
To use CacheFile add this line in the configuration file:
CacheFile /var/www/html/index.html /var/www/html/somefile.index
If you want to use MMapFile directive instead you should add this line in the configuration file:
MMapFile /var/www/html/index.html /var/www/html/somefile.index
There should be no reason to configure both CacheFile and MMapFile for the same files, but you can also use them on different files instead. When you finish with configuring the file save and close it.
You can check the Apache configuration file for syntax error with the following command:
apachectl configtest
In the end when you receive Syntax OK you can restart the Apache by typing the command:
service apache2 restart
After the Apache will restart, you will start to use the file_cache module on the files you configured.
5. Enable File Caching on CentOS
For CentOS 7 we will create a file in the /etc/httpd/conf.modules.d directory with name 00-cache:
nano /etc/httpd/conf.modules.d/00-cache.conf
Insert the following line in your new configuration file:
LoadModule file_cache_module modules/mod_file_cache.so
Save and close the file.
6. Edit the Apache main configuration file
Now you should edit the Apache main configuration file. Open the Apache main configuration file with nano editor with this command:
nano /etc/httpd/conf/httpd.conf
If you want to use the CacheFile directive to handle caching, you should insert the following line in the Apache configuration file.
CacheFile /var/www/html/index.html /var/www/html/somefile.index
If you want to use MMapFile directive instead you should add this line in the configuration file:
MMapFile /var/www/html/index.html /var/www/html/somefile.index
In practice, there is no need both CacheFile and MMapFile directives to be configured for the same files, but you can use them both in the configuration file for a deferent set of files.
When you finish with configuring the file save and close it. You can check the Apache configuration file for syntax error with the following command:
apachectl configtest
You should receive a Syntax OK message, which means that your configuration is correct and you can restart the Apache by executing the command:
systemctl restart httpd
In this tutorial, in the first part, we showed you how to check if your file_cache module is enabled on your server. In the second part, we presented how to enable and configure Apache2 to control browser caching on Ubuntu and CentOS.
Of course, you don’t have to configure Apache2 to control browser caching, if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to do this for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post on configuring Apache2 to control browser caching, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.
would be nice to make it clear that you are caching index.html file if that is so. I was unable to understand which file is being cached.
Hi Miten,
In the tutorial, we show you how to cache index.html file for example.