Speed Up Website in Apache by Enabling Compression and Leveraging Browser Caching

By | 2013/01/30

Google has a cool website to inspect your website for speed improvements. Two of the main areas are leveraging browser caching as well as enabling compression for your website. I’ll show you how to set this up in Apache!


Firstly, here is that website. Put in your site for a quick review:

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


If you receive results stating a need to Leverage browser caching, try the following.

First, enable the mod_expires module in Apache.

Debian / Ubuntu:

$ sudo nano a2enmod expires

CentOS / Red Hat with Apache 2.2.x, make sure mod_expires is uncommented:

$ sudo nano /etc/httpd/conf/httpd.conf

Here is a suggested configuration for mod_expires that can be placed either in a VirtualHost config or just in .htaccess:

<IfModule mod_expires.c>

    ExpiresActive On
    ExpiresDefault "access plus 1 month"

</IfModule>


If you receive results stating a need to Enable compression, try the following.

First, enable the mod_deflate module in Apache.

Debian / Ubuntu

$ sudo nano a2enmod deflate

CentOS / Red Hat with Apache 2.2.x, make sure mod_deflate is uncommented:

$ sudo nano /etc/httpd/conf/httpd.conf

And below is a suggested configuration for mod_deflate that can be placed either in a VirtualHost config or just in .htaccess:

<IfModule mod_deflate.c>

    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
	
    # Don't compress images
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary

</IfModule>

After putting in changes, re-test your page at
https://developers.google.com/speed/pagespeed/insights