How to Fix the "Set Expires Headers" warning on Apache server

How to Fix the "Set Expires Headers" warning on Apache server

We have a WordPress server, hosted on DigitalOcean. When doing speed testing, we got the "Set Expires Headers" warning on GTMetrix. This is how to fix it. But it should be relevant for any website served using Apache, hosted anywhere.

This is a tutorial based on what I just had to learn. So many tutorials are old, so this one is up to date (for now).

We use DigitalOcean for all our hosting because it makes our website infinitely customisable, cheap to host and blazing fast. We only need $5 a month for a system that can easily serve 100,000 pageviews a month. You can keep your fancy hosting plan :)

Blazing fast server speed on our main website, Discover Discomfort (WordPress hosted using DigitalOcean)

If you want to sign up with $100 in credits, use this code here. We get $25 if you follow through.

The out-of-the-box DigitalOcean WordPress installation uses Apache. But note that if you use nginx for your web server, this tutorial isn't relevant to you.

How to set the Expires Headers in Apache

You need to access your terminal.

  • Access your server. Either use ssh, or use your terminal in your DigitalOcean droplet configuration.
  • sudo -i to do superuser commands.
  • a2enmod expires (don't restart the server yet... though no harm if you do)
  • a2enmod headers to install the header module
  • vim /etc/apache2/apache2.conf to open the conf file.
  • Scroll down to the bottom, go to the end, press i for insert, then paste this block:
<IfModule mod_expires.c>
ExpiresActive on
AddType image/x-icon .ico
ExpiresDefault "access plus 2 hours"
ExpiresByType text/html "access plus 15 days"
ExpiresByType image/gif "access plus 1 months"
ExpiresByType image/jpg "access plus 1 months"
ExpiresByType image/jpeg "access plus 1 months"
ExpiresByType image/png "access plus 1 months"
ExpiresByType text/js "access plus 1 months"
ExpiresByType text/javascript "access plus 1 months"
ExpiresByType text/plain "access plus 30 days"
ExpiresByType image/x-icon "access plus 30 days"
ExpiresByType image/ico "access plus 30 days"
</IfModule>
  • :wq to quit and save
  • systemctl restart apache to restart!