
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

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

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.

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.