Configuring Apache 2 to Control Browser Caching on Ubuntu and CentOS

In this article, we will talk about how to configure Apache2 to control browser caching… If you want to reduce the consumption of your server’s resources, improve performance, use up bandwidth, make content available during network interruptions, and give your end users a faster experience, you need to use caching that will allow it all.

Cache is a method of temporarily storing the most requested content so that future requests for that content will be served faster by temporary storage (cache) than from the main location. By using caching, you effectively reuse previously received data. Today we configure browser caching control on Apache 2… Let’s start.

1. Prerequisites

  • ssh access to VPS;
  • installed Apache2 web server;
  • Basic knowledge of Linux (navigation, opening files, editing files, saving files, etc.);

2. Checking modules

Usually our servers have already enabled file_cache, which is needed to manage browser cache. However, we need to make sure that our Apache2 along with our module is installed and ready to accept directives. There is an easy way to test our module. To list Apache modules, we will use apachectl command, and to display modules and pipes with grep command, to filter our results and show only those modules that we need.

We can check the file_cache module with the following command:

apachectl -M | grep file_cache

the output should be as follows:

file_cache_module (shared)

If after executing these commands you have nothing as a result, or it is simply empty, then the module is not installed. You must install it to continue with this tutorial.

3. Enable file caching

Configuring Apache 2 to Control Browser CachingTo use the functionality of the file_cache module, you need to enable it first. If you are using CentOS 7 or Ubuntu 16.04, this module is not configured by default in Apache, so this module is not loaded. We will show you how to configure and enable the file_cache module on CentOS 7 and Ubuntu 16.04.

4. Enable file caching in Ubuntu.

If you are using Ubuntu 16.04. you can enable the file_cache module with the following command:

a2enmod file_cache

The next step is to edit the main Apache configuration file. Open the main Apache configuration file by entering:

nano /etc/apache2/apache2.conf

To use CacheFile add this line to your config file:

CacheFile /var/www/html/index.html /var/www/html/somefile.index

If you want to use the MMapFile directive, you must add this line to the config 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 in different files. When you’re done setting up, save the file and close it.

You can check the Apache config file for syntax errors with the following command:

apachectl configtest

At the end when you receive Syntax OK, you can restart Apache by entering the command:

service apache2 restart

After restarting Apache, you will start using the file_cache module in the files that you configured.

5. Enable file caching on CentOS

For CentOS 7, we will create a file in the /etc/httpd/conf.modules.d directory named 00-cache:

nano /etc/httpd/conf.modules.d/00-cache.conf

Paste the following line into a new config file:

LoadModule file_cache_module modules/mod_file_cache.so

Save and close the file.

6. Edit the main Apache configuration file

You should now edit the main Apache configuration file. Open the main Apache configuration file with the nano editor using this command:

nano /etc/httpd/conf/httpd.conf

If you want to use the CacheFile directive to handle caching, you must insert the following line into your Apache configuration file.

CacheFile /var/www/html/index.html /var/www/html/somefile.index

If you want to use the MMapFile directive, you must add this line to the config file:

MMapFile /var/www/html/index.html /var/www/html/somefile.index

In practice, it is not necessary that the CacheFile and MMapFile directives be configured for the same files, but you can use them as in the configuration file for the lazy set of files.

When you finish setting up save the file and close it. You can check the Apache config file for syntax error with the following command:

apachectl configtest

You should get “Syntax OK” messagewhich means your configuration is correct and you can restart Apache by running the command:

systemctl restart httpd

In this tutorial, in part one, we showed you how to check if your file_cache module is enabled on your server. In part two, we covered how to enable and configure Apache2 to manage browser caching on Ubuntu and CentOS.

Related Posts