Part One: How to setup Apache 2 Server Using Portable Method to Manage Browser Caching

Part One: How to setup Apache 2 Server Using Portable Method to Manage Browser Caching

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.

Prerequisites

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:

headers_module (shared)

Next, verify the Mod_expires Apache module using the following command:

apachectl -M | grep expires

This will give you an output like this:

expires_module (shared)

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.

<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault "access plus 2 days"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/css "now plus 1 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/html "access plus 600 seconds"
</IfModule>

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.

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.

Conclusion

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>.

Check out these top 3 Best web hosting services

Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
Ultahost
$2.90 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8

Part Two: How to setup an Apache 2 Server Using Include Method to Manage Browser Caching

This Article is written to show you how to set up your Apache2 servers via the I
6 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How To Set Up an Apache 2 Server Using The Include Method To Manage Browser Caching

This Article is written to show you how to set up your Apache2 servers via the
6 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Harden the Apache web server on a CentOS 7 VPS or Dedicated Server

In this how-to article, we illustrate how to harden an Apache web server, runnin
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to Host Multiple Websites on an Ubuntu 18.04 VPS or Dedicated Server

This article will show you how to host two or more websites on a single Virtual
5 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top