Installing Apache mod_deflate and mod_expires on CentOS 5

When installing Apache web server, it's a good idea to enable the mod_deflate and mod_expires modules. Mod_deflate enables Apache to serve compressed content which is then decoded by the receiving browser. Mod_expires module allows the setting of expiration headers for static content based on mime type. Both are essential to a high performance web server and a good Google Page Speed score, which is now being used as a factor in their search algorithm.


The following are directions for enabling on CentOS 5.6, but they should work with earlier versions of Centos too. Mod_deflate is known as mod_gzip on other flavors of Linux.

Create the deflate.conf file. Note the content types enabled.

echo 'AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.[0678] no-gzip
BrowserMatch \bMSIE\s7 !no-gzip !gzip-only-text/html
BrowserMatch \bOpera !no-gzip
Header append Vary User-Agent' > /etc/httpd/conf.d/deflate.conf

Create the expires.conf file. Content types are listed on their own lines followed by an expiration time in seconds.

echo 'ExpiresActive On
ExpiresByType image/gif A604800
ExpiresByType image/jpeg A604800
ExpiresByType image/png A604800
ExpiresByType application/x-javascript A604800
ExpiresByType application/javascript A604800
ExpiresByType text/css A604800
ExpiresByType text/xml A604800' > /etc/httpd/conf.d/expires.conf

You can also set html content to cached in browsers, but be careful doing this as it'll prevent users from seeing updated content. You could do this if your web content never changes or changes very infrequently.

echo 'ExpiresByType text/html A604800' >> /etc/httpd/conf.d/expires.conf

Restart Apache now to activate your changes:

/etc/init.d/httpd restart