How to Configure Apache 2 to Control Browser Caching

Before we get to browser caching, let’s answer the question: What is Caching? Visit our What is Caching? tutorial if you don’t already know what caching is!

Pre-Flight Check

  • These instructions are intended specifically for configuring Apache 2 to control browser caching.
  • I’ll be working from a Liquid Web Core Managed CentOS 7 server, and I’ll be logged in as root.

Step #1: Verify Modules

Apache must be configured with the appropriate modules to leverage browser caching.

Let’s check for mod_expires (expires_module) first:

apachectl -M | grep expires

… should return:

expires_module (shared)

Then let’s check for mod_headers (headers_module):

apachectl -M | grep headers

… should return:

headers_module (shared)

Step #2: Examples of Directives

This code can be placed in the .htaccess files for specific directories, or in your root web directory, but we suggest placing it in your httpd.conf.

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

  • The default expiration (ExpiresDefault) is set to 2 days.
  • Images expire after 1 month.
  • CSS and JavaScript also expire after 1 month.
  • HTML expires after 10 minutes (600 seconds).

Step #3: Implement Directives

The above directives can be implemented easily. If you’re not already, SSH into your server as root. Then we’ll use vim to edit the httpd.conf file. For a refresher on editing files with vim see: New User Tutorial: Overview of the Vim Text Editor. If you’re using Liquid Web’s CentOS 7 Core Managed image then the following command already uses the correct location:

vim /etc/httpd/conf/httpd.conf

Find a section that looks like this:

# Further relax access to the default document root:
<Directory "/var/www/html">

… the section above (in this case) is the default document root. Add the expiration directives between <Directory “/var/www/html”> and </Directory>.

Then restart Apache 2!

systemctl restart httpd

Full details for mod_expires can be found in the Apache Documentation.

How to List Which Apache 2 Modules are Enabled on Fedora 21

The Apache web server is one of the most popular and powerful web servers in the world due to its ease of administration and flexibility. This flexibility comes Apache’s modular design, and allows for such features as: URL rewriting for SSL encryption natively, and Outlook Anywhere passthrough support in reverse proxy setups. Modularity allows Administrators to modify Apache to meet their needs; adding modules that are needed and removing ones that are not.

Pre-Flight Check
  • These instructions are intended specifically for viewing which Apache modules are enabled on Fedora 21.
  • I’ll be working from a Liquid Web Self Managed Fedora 21 server with Apache 2 installed, and I’ll be logged in as root.

Continue reading “How to List Which Apache 2 Modules are Enabled on Fedora 21”

How to List Which Apache 2 Modules are Enabled on Fedora 20

The Apache web server is one of the most popular and powerful web servers in the world due to its ease of administration and flexibility. This flexibility comes Apache’s modular design, and allows for such features as: URL rewriting for SSL encryption natively, and Outlook Anywhere passthrough support in reverse proxy setups. Modularity allows Administrators to modify Apache to meet their needs; adding modules that are needed and removing ones that are not.

Pre-Flight Check
  • These instructions are intended specifically for viewing which Apache modules are enabled on Fedora 20.
  • I’ll be working from a Liquid Web Self Managed Fedora 20 server with Apache 2 installed, and I’ll be logged in as root.

Continue reading “How to List Which Apache 2 Modules are Enabled on Fedora 20”

How to List Which Apache 2 Modules are Enabled on CentOS 7

The Apache web server is one of the most popular and powerful web servers in the world due to its ease of administration and flexibility. This flexibility comes Apache’s modular design, and allows for such features as: URL rewriting for SSL encryption natively, and Outlook Anywhere passthrough support in reverse proxy setups. Modularity allows Administrators to modify Apache to meet their needs; adding modules that are needed and removing ones that are not. Here we’ll be working with Apache on CentOS 7.

Pre-Flight Check
  • These instructions are intended specifically for viewing which Apache modules are enabled on CentOS.
  • I’ll be working from a Liquid Web Core Managed CentOS 7 server with Apache 2 installed, and I’ll be logged in as root.

Continue reading “How to List Which Apache 2 Modules are Enabled on CentOS 7”

How to List Which Apache 2 Modules are Enabled on CentOS 6

The Apache web server is one of the most popular and powerful web servers in the world due to its ease of administration and flexibility. This flexibility comes Apache’s modular design, and allows for such features as: URL rewriting for SSL encryption natively, and Outlook Anywhere passthrough support in reverse proxy setups. Modularity allows Administrators to modify Apache to meet their needs; adding modules that are needed and removing ones that are not.

Pre-Flight Check
  • These instructions are intended specifically for viewing which Apache modules are enabled on CentOS.
  • I’ll be working from a Liquid Web Core Managed CentOS 6.5 server with Apache 2 installed, and I’ll be logged in as root.

Continue reading “How to List Which Apache 2 Modules are Enabled on CentOS 6”