How to Install Apache Mod Expires on Ubuntu 9.10

By | August 16, 2020

Apache mod expires

mod expires is an Apache module that can send additional expiry information about a server file to a browser.

If the expiry date is set to a far future date, then the browser will fetch the file from its cache on next request and will not contact the server.

This will speed up the loading time of the website. mod expires is typically used for static files like javascript , css and images.

1. Enable Mod Expires on Ubuntu

First of all you need to enable mod expires module inside Apache. This can be done using the a2enmod command as shown below:

$ sudo a2enmod headers
$ sudo a2enmod expires
$ sudo /etc/init.d/apache2 restart

2. Add .htaccess file with rules

The next step is to add a .htaccess file in the directory of your web files. You can create a blank .htaccess file and put in the mod expires directives as show below. Just copy paste the following block of text.

# Now set the expires time for various type of contents
<IfModule mod_expires.c>
	ExpiresActive On
	
	#30 days
	ExpiresByType image/x-icon A2592000
	ExpiresByType application/x-javascript A2592000
	ExpiresByType application/javascript A2592000
	ExpiresByType text/javascript A2592000
	ExpiresByType text/ecmascript A2592000
	ExpiresByType text/css A2592000
	
	#7 Days
	ExpiresByType image/gif A604800
	ExpiresByType image/png A604800
	ExpiresByType image/jpeg A604800
	ExpiresByType text/plain A604800
	ExpiresByType application/x-shockwave-flash A604800
	ExpiresByType video/x-flv A604800
	ExpiresByType application/pdf A604800
	
	#ExpiresByType text/html A900
</IfModule>

After adding the .htaccess file open the web files in the browser as you would normally, and see if it shows the proper page contents.

3. Enable .htaccess

If you get 500 Internal Server Error, the follow the steps.

1.Check the apache error log

$ cat /var/log/apache2/error.log

It might show the error message :

[Sun Nov 06 16:27:55 2011] [alert] [client 127.0.0.1] /var/www/.htaccess: ExpiresActive not allowed here

2. To fix the above error open up the apache configuration file

$ gksudo gedit /etc/apache2/sites-available/default

Add Indexes to the AllowOverride line for /var/www/:

<Directory /var/www/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride FileInfo Options Indexes
		Order allow,deny
		allow from all
	</Directory>

Save the configuration file and restart Apache. Now check the web page again in your browser.

Now the 500 Internal Server Error should be gone and mod expires should work fine.

This will enable mod expires far future dates for various content types like javacsript , css , images , flash videos etc.

Conclusion

You can also check the performance with the YSlow plugin for Firefox or using the pagespeed insights tool from google. Here is the link:

https://developers.google.com/speed/pagespeed/insights/

If you have any feedback or questions let us know in the comments below.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *