Add Expires Headers for WordPress on Google Cloud (Bitnami)

July 20, 2018Leron AminComments Open

Adding Expires and Cache-control headers is a great way to improve user experience by reducing your website’s load time.

In this tutorial you will learn how to quickly add Expires and Cache-control headers to your WordPress Bitnami installation.

Configuring Expires and Cache-control headers enables your website to cache static content, leading to better website performance and reduced load times for visitors.

Checking existing headers

Before diving into this tutorial, you should check your website for existing Expires and Cache-control headers. To do this, run a test for your domain at GTMetrix.com.

add expires headers wordpress on google cloud bitnami
After running a performance report for my domain on GTMetrix.com, it was clear that my server did not have Expires/Cache-control headers configured, as the test responded with a failing grade.

If you received an F grade for the Add Expires Headers recommendation, then read on to learn how to configure Expires and Cache-control headers for your website.

Getting started

Before getting started with this tutorial, you should have already installed the Bitnami WordPress (LAMP) stack on Google Cloud. If you haven’t yet done this, check out the following tutorials for instructions on how to install the Bitnami WordPress stack on Google Cloud.

Please note that this tutorial will also work with the Bitnami WordPress (LAMP) stack on Amazon AWS and Microsoft Azure.

Lastly, if you’ve already configured Expires Headers as shown in step 4 of the CloudFlare CDN for WordPress on Google Cloud tutorial, then you can skip this tutorial.

There are 5 steps in this tutorial:

1. Connect to your instance

add expires headers for wordpress bitnami ssh into vm instance
From your Google Cloud dashboard, navigate to VM instances, and click the SSH button next to the virtual machine instance that is running your WordPress website.
If you are using AWS, click here for instructions on how to SSH into your EC2 instance.

2. Enable expires module

add expires headers for wordpress bitnami open httpd.conf file
After connecting to your virtual machine instance, run the command below to open your Apache server configuration file.
sudo nano /opt/bitnami/apache2/conf/httpd.conf
add expires headers wordpress google cloud
At the top right corner of the page, click on the keyboard icon, then select Ctrl+W to search.
add expires headers wordpress google cloud bitnami
After the search field opens, type expires then press Enter to search.
add expires headers wordpress on google cloud bitnami
Remove the # symbol from next to the expires_module in order to activate it.
Lastly, enter CRTL + X, then Y, to save and exit the file.

3. Add expires headers

add expires headers for wordpress on google cloud bitnami
Enter the command below to open your Apache server application configuration file.
sudo nano /opt/bitnami/apps/wordpress/conf/httpd-app.conf
add expires wordpress on google cloud bitnami
After opening your httpd-app.conf file, copy and paste the code below directly above the line that starts with . Lastly, enter CRTL + X, then Y, to save and exit the file.

<filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>

4. Restart Apache server

add expires headers wordpress on google cloud bitnami
Execute the command below to restart your Apache server.
sudo /opt/bitnami/ctlscript.sh restart

5. Test performance

Now that you’ve configure Expires and Cache-control headers for your WordPress website, the next step is to test that the changes were effective. Re-test your domain on GTMetrix.com to get an updated performance report, then navigate to the YSlow tab.

add expires headers wordpress on google cloud
If you configured your Expires and Cache-control headers correctly, you will receive a passing grade for the Add Expires Headers recommendation.
add expires headers wordpress on google cloud bitnami
You can optionally click on the YSlow tab to view the exact details of your Expires and Cache-control headers.

Frequently Asked Questions

  1. Why do you need Expires and Cache-control headers?
  2. According to Kartik Ayyar, Machine Learning engineer at Google, you should “use both, primarily because different browsers and intermediate proxies can implement parsing these headers slightly differently and you want to insulate yourself from bugs in either”.

  3. How do you check if both Expires and Cache-control headers are enabled?
  4. You can view your Expires and Cache control settings by running a performance report for your website on GTMetrix.com, then clicking on one of your website’s images under the Waterfall tab.

    add expires headers wordpress on google cloud bitnami
    If you’ve successfully followed this tutorial, you will notice that your server will respond with both Expires and Cache-control headers
    add expires headers wordpress on google cloud bitnami
    If you enabled either Expires or Cache-control headers, but not both, then you will see only one of the response headers appear in the Waterfall tab (notice how an Expires header does not appear in the Waterfall chart in the image above).

Troubleshooting

If you have any questions or comments regarding this tutorial, please leave them in the comment field below.

For troubleshooting support, be sure to check out the WordPress Cloud Hosting Support Group on Facebook.

Leave a Reply

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

Made with by

© 2020 One Page Zen Systems, LLC  
Legal  |  Privacy Policy  |  Affiliate Disclosure  |  Contact  

Join our Mailing List!

Exclusive early access to the latest tutorials

Monthly update with a list of upcoming tutorials

Special access to members-only events and webinars