Browser caching is an incredible technique designed to reduce bandwidth utilization and resource consumption while offering a seamless and faster end-user experience to your website visitors. When implemented properly, caching can speed up browsing and deliver a high benefit-cost ratio in websites where visitors revisit the same location.
This tutorial will help you configure your browser caching control on Apache 2 servers. We’ll cover all servers running the mod_headers and Mod_expires Apache modules.
This tutorial assumes that you are familiar with the following system administration concepts:
- Basic SSH connections
- Navigation in the common Linux command line Shell platform
- Command line piping through Linux I/O redirection
- Managing (open, edit, save) files in a system editor such as Nano, Vim, etc
If you are conversant with these concepts you, are good to go!
Step1- Verifying Modules
Before we start the configuration process, we must ensure that the mod_headers and Mod_expires Modules are properly installed and that Apache2 servers are ready to take the commands. Here, we’ll utilize apachectl -M command to check the existing Apache modules and display the output via grep module_name command. The output will feature filtered results showing only the modules with the provided module_name.
To verify mod_headers run the following command:
apachectl -M | grep header
You will get the following output:
Next, verify the Mod_expires Apache module using the following command:
apachectl -M | grep expires
This will give you an output like this:
Since both outputs showed positive results, it’s a clear that the modules we require for our tutorial are present. However, if the outputs are blank its a sign that the modules are absent. In that case, you will have to install the missing modules before you continue.
Step 2- Configuring The Directives
For the sake of this tutorial, we’ll leverage a generic configuration that prolongs cache duration of basic static files to minimize the stress on server resources. This file will not change between visits and need not be downloaded on each visit. In addition, modern browsers are innovatively equipped to take instructions from servers that offer suggestions regarding the duration of caching content. This example will work well for almost every website, but you may need to adjust the lifespan or add/remove file types to suit your typical content.
You can visit the Apache Mod_expires Online documents to understand more about these directives.
Step 3- Implementing The Directives
Once you comprehend how to configure the directives, you have to settle for an ideal method of implementation. There two methods of implementing the directives namely Portable and Include methods. This tutorial will focus on the Portable method.
This method relies on the .htaccess file to control directories that will be affected by the configuration settings of Mod_expires. Here, the directories are handled like the other.htaccess file changes
To implement the directives via this method:
- Locate the specific directory that requires browser caching.
- Create a .htaccess file in that directory. If there is one already, proceed with the next instruction.
- Copy the required directives (refer to the configuration directive part above)and paste them in the .htaccess file.
- Save all the changes to the .htaccess file
- That's all! You have successfully configured you Apache2 servers and controlling your browsing cache should be easy.
Using the Portable method to implement the directive presents a bottleneck caveat linked to the .htaccess file. The caveat is a general Apache issue that is not specific to Mod_expires, but it affects .htaccess files in general. For .htacess files to function properly, Apache scans every directory on the path to the targeted file and along the way it applies every .htaccess file it finds. This may cause I/O bottleneck in your server configuration.
For this reason, its highly recommended you use the Include method when implementing the directives on your Cloud VPS servers.
Using the Portable method is a simple process that only involves a few steps. However, there is another method that allows you to overcome the shortcoming of the portable method. To explore this option, check this article <How to set up Apache 2 server to manage browser caching Using the Include Method>.