Setup mod_pagespeed with Apache on Ubuntu 16.04

Table of Contents

Introduction

mod_pagespeed is an open source and most popular modules for Apache web server that automatically optimize Web Pages to improve better performance while serving web pages using HTTP Server. This module is created by Google to help make the web server faster by rewriting web pages to reduce latency and bandwidth and by compressing javascript and css files. It can be configured through a variety of filters that automatically optimize files like HTML, CSS, JavaScript, JPEG, PNG and other resources to speed up your website.
In this tutorial, we will learn how to install and configure Google‘s mod_pagespeed module for Apache to improve the page load times of your website.


Requirement

  • Ubuntu 16.04 server with Apache installed on your system.
  • Non-root user account with sudo privilege set up on your server.

Getting Started

Let's start making sure that your Ubuntu-16.04 server is fully up to date.
You can update your server by running the following command:

sudo apt-get update -y
sudo apt-get upgrade -y

Installing Mod_Pagespeed

Before proceeding with mod_pagespeed module, make sure that Apache installed on your system.
Now download the mod-pagespeed module for Apache2 from the official site, or download it by running the following command:

wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb
 
After downloading above package, you can install it using the following command:

dpkg -i mod-pagespeed-stable_current_amd64.deb
sudo apt-get install -f
 
Installing mod_pagespeed from binary packages will add a Google’s official repository to your system, so that you can update the module any time using Ubuntu package manager. After successful installation, you will need to restart Apache service to load module.

sudo /etc/init.d/apache2 restart
 
You can check mod_pagespeed module by looking at your page's response headers by running the following command:

curl -D- localhost | less
 
If everything is fine, you should see the following output:

HTTP/1.1 200 OK
Date: Mon, 22 Aug 2016 18:21:57 GMT
Server: Apache/2.4.7 (Ubuntu)
Accept-Ranges: bytes
Vary: Accept-Encoding
X-Mod-Pagespeed: 1.11.33.2-0
Cache-Control: max-age=0, no-cache
Content-Length: 10724
Content-Type: text/html; charset=UTF-8
 
You should now have mod_pagespeed up and running on your server.

Configuring mod_pagespeed

By default, mod_pagespeed module comes with most commonly used filters. The main configuration file for mod_pagespeed is located at /etc/apache2/mods-available/pagespeed.conf. This configuration file is used for global configuration for mod_pagespeed. You can also put your site-specific PageSpeed filters in Apache VirtualHost directive.
To start editing the main configuration file, use the following command:

sudo nano /etc/apache2/mods-available/pagespeed.conf
 
If you want to disable mod_pagespeed module the change the line from:

ModPagespeed on
to
ModPagespeed off
 
Save the file and restart the Apache service:

sudo /etc/init.d/apache2 restart

Configuring PageSpeed Filters

Mod_Pagespeed uses three types of levels that makes configuration simple. PassThrough, CoreFilters, and OptimizeForBandwidth. The CoreFilters level enables a default set of filters that will make your website faster. The OptimizeForBandwidth level provides a stronger guarantee of safety that are not aware of PageSpeed. You can disable CoreFilters by setting the RewriteLevel to PassThrough. You can do this by editing pagespeed.conf file:

sudo nano /etc/apache2/mods-available/pagespeed.conf
 
Add / Edit the following line:

ModPagespeedRewriteLevel PassThrough
 
You can also enable and disable specific filters:

sudo nano /etc/apache2/mods-available/pagespeed.conf
 
To turn off specific filters in the core set, specify:

ModPagespeedDisableFilters filter1,filter2
 
To turn on specific filters in the core set, specify:

ModPagespeedEisableFilters filter1,filter2

Mod_Pagespeed Web Admin

Apache's mod_pagespeed module also provide a web based admin area to view the server state. By default, it is accessible from localhost only. You will need to add your IP Address in Allow from list in the configuration file.
You can do this by editing the pagespeed.conf file:

sudo nano  /etc/apache2/mods-available/pagespeed.conf
 
Under the directive <location pagespeed_admin=""> and <location pagespeed_global_admin=""> add the line "allow from your-ip-address".

<Location /pagespeed_admin>
    Order allow,deny
    Allow from localhost
    Allow from 127.0.0.1
    Allow from your-ip-address
    SetHandler pagespeed_admin
</Location>

<Location /pagespeed_global_admin>
    Order allow,deny
    Allow from localhost
    Allow from 127.0.0.1
    Allow from your-ip-address
    SetHandler pagespeed_global_admin
</Location>
 
Save the file and restart the Apache service.

sudo /etc/init.d/apache2 restart
 
Now access the URL http://server-ip-address/pagespeed_admin in your browser, You will see the following page:

mod_pagespeed Admin Panel

Mod_pageSpeed y WordPress

Existe un pequeño problema con el WYSIWYG del administrador de WordPress y es que al concatenar y comprimir ciertos Javascripts del admin, el editor visual deja de funcionar.
Existen 2 soluciones:
  • O bien configurar el WordPress para que no concatene los scripts, lo cual hay que hacerlo en cada Wordpress que exista en tu servidor.
Para esto tendrás que editar tu wp-config.php e insertar esta definición:
define('CONCATENATE_SCRIPTS', false );
  • O bien tu tu configuración de Mod_pagespeed configuras 2 nuevos filtros:
ModPagespeedDisallow */wp-admin/*
ModPagespeedDisallow */wp-includes/*

 

 

 Summary

In this tutorial, we have successfully configured mod_pagespeed module with Apache. This will improve the page load times of your website. You can also check out the official mod_pagespeed site for more information. Please comment below if you run into any issues following the tutorial.

Comentarios