How To Install Google’s PageSpeed Module On Apache And Nginx On Linux
A great way to improve the speed of your web server on Linux is with the Google PageSpeed module. Enabling Google’s PageSpeed Module on Apache or Nginx results in page loading speeds up to 10X faster.
Apache Instructions
Apache is the preferred web server on most Linux installations. As a result, Google prefers users to go this route when using it. If you’re using Ubuntu server, Debian server, Fedora server, Redhat Enterprise Linux, CentOS or even Suse Enterprise Linux, follow along to get the plugin working.
Note: Google does not support server operating systems running Apache that do not use DEB or RPM. To use on other platforms, the NGINX web server is recommended.
Ubuntu Server/Debian Server
Getting the PageSpeed module working on both Debian and Ubuntu server operating systems is refreshingly easy as Google provides a downloadable binary package. This package contains the module as well as an official software repository ensuring that the module will always be up to date.
Start off by downloading the package using the wget downloader tool.
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb
or
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.deb
Alternatively, download the beta release of the module with:
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-beta_current_amd64.deb
or
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-beta_current_i386.deb
The Pagespeed package isn’t large, so the download won’t take long. When it finishes up, the next step in the process is to install the package to the system via dpkg.
sudo dpkg -i mod-pagespeed-stable_current_*.deb
or
sudo dpkg -i mod-pagespeed-beta_current_*.deb
Installing the module via dpkg should work flawlessly, without any dependency issues. If, for some reason dependency errors arise, correct them with the following command:
sudo apt install -f
RHEL/CentOS
Like Ubuntu, RHEL and CentOS users looking to get Google’s PageSpeed module don’t need to run through a complicated process. Instead, there’s a convenient RPM file ready for download. To start the installation process, use the wget tool to download the package.
To get the latest stable version of Mod_pagespeed, try:
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-beta_current_x86_64.rpm
or
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-beta_current_i386.rpm
For the beta version, do:
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
or
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.rpm
Using the RPM command, install Mod_pagespeed.
sudo rpm -U mod-pagespeed-*.rpm
Nginx Instructions
Along with Apache, Google’s PageSpeed module also has support for Nginx. However, unlike Apache, there are no convenient binary packages to download. Instead, those looking to use it need to get it by hand.
To start the module installation on Nginx, enter the following command. Running this command will execute an automatic script that sets everything up.
Note: in order to use this script, curl is required. Look for “curl” in the package manager and install it before continuing.
bash <(curl -f -L -sS https://ngxpagespeed.com/install) \ --nginx-version latest
When the script is complete, the Nginx Pagespeed module should be working.
Configure PageSpeed
Installing the PageSpeed module for the Apache web server requires no configuration. On Nginx, however, it does. To enable the module within the Nginx webserver, edit the nginx.conf file. In the terminal, gain root, then open up the configuration file using Nano.
su -
or
sudo -s
nano /etc/nginx/nginx.conf
In the configuration file, paste the following code:
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$" { }
Save the configuration file by pressing Ctrl + O, and exit with Ctrl + X.
Disable PageSpeed
Google’s PageSpeed module makes a huge impact on performance for websites. Still, if you’re not happy with the results, for whatever reason, you may want to disable it. Disabling the module is easy on both servers.
Apache
On Apache, there are a few ways to disable the module, but by far the most effective way is to just uninstall the binary package. Doing this will automatically clean any residual configuration files and libraries from the system.
Debian/Ubuntu
sudo apt remove mod-pagespeed
or
sudo apt remove mod-pagespeed-beta
RHEL/CentOS
sudo yum remove mod-pagespeed
or
sudo yum remove mod-pagespeed-beta
Nginx
Disabling the module on Nginx is also simple. To turn off the module, edit the Nginx configuration file and set PageSpeed from “on” to “off”.
su -
or
sudo -s nano /etc/nginx/nginx.conf
Find “pagespeed on;” and change it to:
pagespeed off;
After changing the value, exit Nano. PageSpeed should instantly turn off.