Install Mod_Pagespeed to Speed Up Apache and Nginx

Mod_Pagespeed (for Apache) and ngx_pagespeed (for Nginx) are open-source modules developed by Google that automatically optimize your web pages.

Alby Andersen

To install Mod_Pagespeed (Google’s Apache/NGINX module for automatic website optimization), follow these steps. This module improves performance by compressing resources, optimizing images, lazy-loading content, and more.


Prerequisites

  • Root/sudo access.
  • Apache or Nginx installed.
  • For Nginx: Mod_Pagespeed is deprecated for Nginx (as of 2022), but you can use the last compatible version or community forks.

Install Mod_Pagespeed for Apache

1. Download and Install the Module

Debian/Ubuntu:

# Install dependencies
sudo apt install wget

# Download the Mod_Pagespeed package
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb

# Install the package
sudo dpkg -i mod-pagespeed-*.deb

# Fix dependencies (if needed)
sudo apt --fix-broken install

CentOS/RHEL/Fedora:

# Add the Google repository
sudo tee /etc/yum.repos.d/google-mod-pagespeed.repo <<EOF
[google-mod-pagespeed]
name=google-mod-pagespeed
baseurl=https://dl-ssl.google.com/linux/mod-pagespeed/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
EOF

# Install the module
sudo yum install mod-pagespeed

2. Enable the Module

# Restart Apache to activate Mod_Pagespeed
sudo systemctl restart apache2   # Debian/Ubuntu
sudo systemctl restart httpd     # CentOS/RHEL

Install Mod_Pagespeed for Nginx (Deprecated but Possible)

Note: Mod_Pagespeed is no longer officially supported for Nginx. Use at your own risk.

1. Download and Compile Nginx with Mod_Pagespeed

# Download the module
cd /usr/src
wget https://github.com/apache/incubator-pagespeed-ngx/archive/v1.13.35.2-stable.tar.gz
tar -xvf v1.13.35.2-stable.tar.gz

# Download the PSOL library (required)
cd incubator-pagespeed-ngx-1.13.35.2-stable
wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz
tar -xvf 1.13.35.2-x64.tar.gz

# Recompile Nginx with Mod_Pagespeed
# (Replace your existing Nginx configure command)
./configure \
  --add-module=/usr/src/incubator-pagespeed-ngx-1.13.35.2-stable \
  [other existing flags]

# Compile and install
make && sudo make install

2. Configure Nginx

Add the following to your Nginx server block (/etc/nginx/nginx.conf):

# Enable Mod_Pagespeed
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;

3. Restart Nginx

sudo systemctl restart nginx

Basic Configuration

Edit the Mod_Pagespeed configuration file (/etc/apache2/mods-enabled/pagespeed.conf for Apache):

Example Optimizations:

# Enable Mod_Pagespeed
ModPagespeed on

# Set cache location
ModPagespeedFileCachePath "/var/cache/mod_pagespeed/"

# Enable critical filters
ModPagespeedEnableFilters prioritize_critical_css,combine_css,combine_javascript

# Enable image optimization
ModPagespeedEnableFilters lazyload_images,convert_jpeg_to_webp

Verify Installation

  1. Check Headers:
   curl -I http://your-domain.com | grep X-Mod-Pagespeed

Output should show X-Mod-Pagespeed: ....

  1. View Statistics:
    Access http://your-domain.com/pagespeed_admin to see optimization stats.

Troubleshooting

  1. Permission Issues:
   sudo chown -R www-data:www-data /var/cache/mod_pagespeed/
  1. Check Logs:
  • Apache: /var/log/apache2/error.log
  • Nginx: /var/log/nginx/error.log
  1. Disable Mod_Pagespeed:
   ModPagespeed off  # Add to Apache config

Alternatives

If Mod_Pagespeed isn’t suitable, consider:

  • Cloudflare CDN (automatic optimizations).
  • Standalone optimization tools (e.g., optipng, jpegoptim).
  • WordPress plugins (e.g., WP Rocket, Autoptimize).

Key Notes

  • Test in staging first: Some filters may break your site.
  • Nginx users: Consider community forks like ngx_pagespeed.
  • Security: Keep the module updated to avoid vulnerabilities.
Share This Article