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

In How to set up Apache 2 server to manage browser caching Using the Portable Method you learned how to use the portable method to set up your Apache2 servers. This Article is written as an extension of the first one to show you how to set up your Apache2 servers via the Includes method to control caching.

This technique leverages the effectiveness of the powerful Apache Include. It prevents the INPUT/OUTPUT shortcomings by allowing Apache to read the include files at the startup. The method is broken down into two superior techniques which include Per Website and Global Includes.

Both methods encompass accessing and amending the specific Include files on your Apache server. The specific files to be modified depending on the server administration software and the distribution. For this reason, we’ll discuss the specific locations for each method on various CentOS servers.

Prerequisites

The mod_headers and Mod_expires Modules must be verified. Check on this article <How to set up Apache 2 server to manage browser caching Using the Portable method> to learn how to verify these modules.

Global Includes

This is a simple method that allows you to enable the desired configuration directives over your entire server. When the mod_expires directives are implemented Globally they affect every website running on Apache.

Core-managed CentOS6 & CentOS7 servers

To implement the configuration directives in these servers:

  • Run the following command:
$ vim/etc/httpd/conf.d/expire.conf

This command will fashion a file titled expires.conf in the locations/etc/httpd/conf.d/.

  • Add the directives to this file so that its content looks like this:
<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault"access plus 2 days"
ExpiresByTypeimage/jpg "access plus 1 month"
ExpiresByTypeimage/gif "access plus 1 month"
ExpiresByTypeimage/jpeg "access plus 1 month"
ExpiresByTypeimage/png access plus 1 month"
ExpiresByTypetext/javascript "access plus 1 month"
ExpiresByType application/javascript"access plus 1 month"
ExpiresByType application/x-shockwave-flash"access plus 1 month"
ExpiresByTypetext/css "now plus 1 month"
ExpiresByTypeimage/ico "access plus 1 month"
ExpiresByTypeimage/x-icon "access plus 1 month"
ExpiresByTypetext/html "access plus 600 seconds"
</IfModule>
  • Save the changes and run the following command to reload your Apache server:
Service httpd reload

Fullymanaged CentOS6 and CentOS7 cPanel servers

  • Run the command below to create a file titled pre_virtualhost_global.conf located in the directory /usr/local/apache/conf/includes/.

 

$ vim /usr/local/apache/conf/includes/pre_virtualhost_global.conf
  • Add all the specific directives to this file, at the bottom and save all changes. The bottom part of that file should be like this:

 

<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault"access plus 2 days"
ExpiresByTypeimage/jpg "access plus 1 month"
ExpiresByTypeimage/gif "access plus 1 month"
ExpiresByTypeimage/jpeg "access plus 1 month"
ExpiresByTypeimage/png "access plus 1 month"
ExpiresByTypetext/javascript "access plus 1 month"
ExpiresByType application/javascript"access plus 1 month"
ExpiresByType application/x-shockwave-flash"access plus 1 month"
ExpiresByTypetext/css "now plus 1 month"
ExpiresByTypeimage/ico "access plus 1 month"
ExpiresByTypeimage/x-icon "access plus 1 month"
ExpiresByTypetext/html "access plus 600 seconds"
</IfModule>
  • Next, use the following command to restart Apache:

 

# /scripts/restartsrv_apache

Alternatively, if you are using EasyApache 4, run the command below to restart your Apache PHP-FPM:

 

# /scripts/restartsrv_apache_php_fpm

Fully managed CentOS7 Plesk Onyx 17 Linux Servers

  • Run the following command:

 

$ vim /etc/httpd/conf.d/expire.conf

The command will create a file titled expires.conf in the location  /etc/httpd/conf.d/

  • Next, include the specific directives in the file. The file content should be like this:

 

<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault"access plus 2 days"
ExpiresByTypeimage/jpg "access plus 1 month"
ExpiresByTypeimage/gif "access plus 1 month"
ExpiresByTypeimage/jpeg "access plus 1 month"
ExpiresByTypeimage/png "access plus 1 month"
ExpiresByTypetext/javascript "access plus 1 month"
ExpiresByType application/javascript"access plus 1 month"
ExpiresByType application/x-shockwave-flash"access plus 1 month"
ExpiresByTypetext/css "now plus 1 month"
ExpiresByTypeimage/ico "access plus 1 month"
ExpiresByTypeimage/x-icon "access plus 1 month"
ExpiresByTypetext/html "access plus 600 seconds"
</IfModule>
  • Save the changes, then use the command below to restart the Apache service:
$ Service httpd restart

That's all with the Global includes methods, Next, we’ll use the Per Website Include method to implement the configuration directives.

Per Website Include method

Browser caching can also be enabled using the Include methods based on each virtual host level. Here we’ll use examples for a website with two virtual host connections, HTTP: port 80 and HTTPS: port 443. Each host operates independently, therefore, changes made on the HTTP host will not apply directly to the other HTTPS host.

Core managed CentOS6 and CenstOS7 servers

To implement Per website include browser caching, we’ll use a default SSL website configuration file. However, the exact technique for website management on the core managed server can be changed depending on what works best for you. Follow the steps below:

  • Open the specific website’s configuration file by running the command below:
$ vim /etc/httpd/conf.d/ssl.conf
  • Next search within the config file to locate the specific Virtual Host line associated with that website. The virtual host lines should be like this example:
<VirtualHost _default_:443>
...
</VirtualHost
  •  Add the configuration directives between the two virtual host lines to get a result similar to this:
<VirtualHost _default_:443>
...
<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault"access plus 2 days"
ExpiresByTypeimage/jpg "access plus 1 month"
ExpiresByTypeimage/gif "access plus 1 month"
ExpiresByTypeimage/jpeg "access plus 1 month"
ExpiresByTypeimage/png "access plus 1 month"
ExpiresByTypetext/javascript "access plus 1 month"
ExpiresByType application/javascript"access plus 1 month"
ExpiresByType application/x-shockwave-flash"access plus 1 month"
ExpiresByTypetext/css "now plus 1 month"
ExpiresByTypeimage/ico "access plus 1 month"
ExpiresByTypeimage/x-icon "access plus 1 month"
ExpiresByTypetext/html "access plus 600 seconds"
</IfModule>
...
</VirtualHost>
  • Run the command below to restart your Apache service:
$ Service httpd restart

Fully managed CentOS6 and CentOS7 cPanel servers

The cPanel is a great tool that offers comprehensive template suite that can be utilized to change Apache’s behaviour. However, we need a certain structure to ensure the changes persist through upgrades, updates, and restarts. The structure functions the same on EasyApache 4 and EasyApache 3 systems.

Also, each website can accommodate its unique set of Include files. The unique include files should be  located in:

For HTTPS virtual host

/etc/apache2/conf.d/userdata/ssl/2_4/<USER>/<DOMAIN>/<INCLUDENAME>.conf

For, HTTP virtual hosts:

/etc/apache2/conf.d/userdata/std/2_4/<USER>/<DOMAIN>/<INCLUDENAME>.conf

The following variables should be amended accordingly:

  • USER: replace this component with your  desired accounts name
  • DOMAIN: Replace this with your precise domain.tld website’s name. Do not include the Prefix www..
  • INCLUDENAME: this should be replaced the specific name of this specific Include file that reflects the purpose. For example, expires.conf

Note: The above directories must be created because they don’t exist by default.

  • To create the directories you will have to get their correct details then use the mkdir -p command as follows:

For, HTTPS Virtual Hosts:

$ mkdir -p /etc/apache2/conf.d/userdata/ssl/2_4/myuser/example.com/

For, HTTP Virtual Host:

 

$ mkdir -p /etc/apache2/conf.d/userdata/std/2_4/myuser/example.com/
  • Once you create the directories, create the Include file and give it the name expires.conf. Use the following commands:

HTTPS Host:

$ vim /etc/apache2/conf.d/userdata/ssl/2_4/myuser/example.com/expires.conf

 

$ vim /etc/apache2/conf.d/userdata/std/2_4/myuser/example.com/expires.conf
  • Add the specific mod_expires configuration directives to the expires.conf files. Both files should like this:
<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault"access plus 2 days"
ExpiresByTypeimage/jpg "access plus 1 month"
ExpiresByTypeimage/gif "access plus 1 month"
ExpiresByTypeimage/jpeg "access plus 1 month"
ExpiresByTypeimage/png "access plus 1 month"
ExpiresByTypetext/javascript "access plus 1 month"
ExpiresByType application/javascript"access plus 1 month"
ExpiresByType application/x-shockwave-flash"access plus 1 month"
ExpiresByTypetext/css "now plus 1 month"
ExpiresByTypeimage/ico "access plus 1 month"
ExpiresByTypeimage/x-icon "access plus 1 month"
ExpiresByTypetext/html "access plus 600 seconds"
</IfModule>
  • Next, instruct the cPanel to rebuild the configurations required to implement the new includes:

 

/usr/local/cpanel/scripts/rebuildhttpdconf
  • Once that is achieved, restart Apache service to update all the running configurations:
/usr/local/cpanel/scripts/restartsrv_apache
  • If you are using EasyApache4, you will have to restart your PHP-FPM system. Use the command below:

 

/usr/local/cpanel/scripts/restartsrv_apache_php_fpm

Fully managed CentOS7 Plesk Onyx 17 Linux Servers

Here we replace the yourwebsite.com with the correct domain name minux the www. Prefix.

  • Use the commands below to create the include files:

HTTPS:

 

$ touch /var/www/vhosts/system/example.com/conf/vhost_ssl.conf

HTTP:

 

$ touch /var/www/vhosts/system/example.com/conf/vhost.conf
  • Replace the vhost_ssl.conf and vhost.conf with the correct mod_expires directives. Each file should look like this:

 

<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault"access plus 2 days"
ExpiresByTypeimage/jpg "access plus 1 month"
ExpiresByTypeimage/gif "access plus 1 month"
ExpiresByTypeimage/jpeg "access plus 1 month"
ExpiresByTypeimage/png "access plus 1 month"
ExpiresByTypetext/javascript "access plus 1 month"
ExpiresByType application/javascript"access plus 1 month"
ExpiresByType application/x-shockwave-flash"access plus 1 month"
ExpiresByTypetext/css "now plus 1 month"
ExpiresByTypeimage/ico "access plus 1 month"
ExpiresByTypeimage/x-icon "access plus 1 month"
ExpiresByTypetext/html "access plus 600 seconds"
</IfModule>
  • Instruct Plesk to rebuild the necessary configuration for your website:

/usr/local/psa/admin/sbin/httpdmng --reconfigure-domain example.com

  • Once that is achieved, restart the Apache service:

 

$ service httpd restart

Conclusion

That is it! You have successfully used the include method to install and configure your Apache 2 servers to manage browser caching.

 

Check out these top 3 Best web hosting services

Was this article helpful?