Install and Configure Mod-Pagespeed on Linux servers

Mod-Pagespeed is an Apache/Nginx (web-server) module to speed up your website by applying certain filters that automatically optimize files to reducing number of times the browser has to make to grab web files, to reduce the size of those files and to optimize the length those files are cached. This article we will show you how to install and configure Google‘s mod-pagespeed module for Apache and Nginx web servers in RHEL/CentOS/Fedora and Debian/Ubuntu systems.

Installing Mod_Pagespeed Module on Linux servers

For RHEL/CentOS and Fedora

On 32-bit Systems

# wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.rpm
# yum install at [# if you don't already have 'at' installed]
# rpm -Uvh mod-pagespeed-stable_current_i386.rpm

On 64-bit Systems

# wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
# yum install at [# if you don't already have 'at' installed]
# rpm -Uvh mod-pagespeed-stable_current_x86_64.rpm

For Debian and Ubuntu

On 32-bit Systems

$ wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.deb
$ sudo dpkg -i mod-pagespeed-stable_current_i386.deb
$ sudo apt-get -f install

On 64-bit Systems

$ wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb
$ sudo dpkg -i mod-pagespeed-stable_current_amd64.deb
$ sudo apt-get -f install

This module enables following configuration files and directories itself automatically during installation.

/etc/cron.daily/mod-pagespeed : mod_pagespeed cron script for checking and installing latest updates.
/etc/httpd/conf.d/pagespeed.conf : The main configuration file for Apache in RPM based distributions.
/etc/apache2/mods-enabled/pagespeed.conf : The main configuration file for Apache2 in DEB based distributions.
pagespeed_libraries.conf : The default set of libraries for Apache, loads at Apache startup.
/usr/lib{lib64}/httpd/modules/mod_pagespeed.so : mod_pagespeed module for Apache.
/var/cache/mod_pagespeed : File caching directory for web sites.

Note: In Nginx the configuration files of mod_pagespeed typically found under /usr/local/nginx/conf/ directory.

Configuring Mod_Pagespeed Module

In Apache, mod_pagespeed automatically Turn On when installed, while in Nginx you need to place following lines to your nginx.conf file and in every server block, where PageSpeed needs to be enabled:

Open nginx.conf in an editor and add these lines:

pagespeed on;
# Needs to exist and be writable by nginx. Use tmpfs for best performance.
pagespeed FileCachePath /var/ngx_pagespeed_cache;
# Ensure requests for pagespeed optimized resources go to the pagespeed handler
# and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

Finally, don’t forget to restart your Apache or Nginx server to start mod_pagespeed working properly.

Verifying Mod_Pagespeed Module

To verify mod_pagespeed module, we will use curl command to test in on domain or IP as shown:

# curl -D- http://example.com/ | less

Apache

apache

Nginx

nginx

If you don’t see an X-Mod_Pagespeed header, that means mod_pagespeed isn’t actually installed.

Turning On/Off Mod_Pagespeed module

If you don’t want to use mod_pagespeed, you can Turn Off completely by inserting the following line to pagespeed.conf file at the top.

ModPagespeed off

Similarly, to Turn On module by inserting following line to pagespeed.conf file.

ModPagespeed on

For more details about configuration, you can check out the official mod_pagespeed page at https://developers.google.com/speed/pagespeed/module/

Facebook Comments