Install Nginx ngx_pagespeed Module on Ubuntu 16.04

Updated by Linode Contributed by Vaibhav Rajput

Contribute on GitHub

View Project | View File | Edit File

This is a Linode Community guide. Write for us and earn $300 per published guide.


Pagespeed is a tool built by Google that boosts the speed and performance of a website by automatically minifying assets (such as CSS, Javascript, and images), and applying other web performance best practices.

This guide will show you how to use Pagespeed with Nginx to reduce the page load times of your website, using the ngx_pagespeed module.

Before You Begin

  1. Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.

  2. This guide will use sudo wherever possible. Complete the sections of our Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services.

  3. Update your system:

    1
     sudo apt-get update && sudo apt-get upgrade
    

Install ngx_pagespeed

Download and install the ngx_pagespeed module and its dependencies.

  1. Install the required dependencies:

    1
     sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip
    
  2. Make sure you are in the home directory:

    1
     cd
    
  3. Define the version of ngx_pagespeed to be installed :

    1
    NPS_VERSION=1.12.34.2-stable
    

    This guide uses the current stable version as of this writing. However, you can check the ngx_pagespeed release notes and update this command with the most recent version.

  4. Download the module source code:

    1
    wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip
    
  5. Extract the file and move to the module’s directory:

    1
    2
    unzip v${NPS_VERSION}.zip
    cd ngx_pagespeed-${NPS_VERSION}
    
  6. Pagespeed also requires some additional files. Download and unzip them:

    1
    2
    3
    4
    5
    NPS_RELEASE_NUMBER=${NPS_VERSION/stable/}
    psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz [ -e scripts/format_binary_url.sh ]
    psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL)
    wget ${psol_url}
    tar -xzvf $(basename ${psol_url})
    

Download and build Nginx

Now compile Nginx with the ngx_pagespeed module.

  1. Move back to your home directory:

    1
    cd
    
  2. Define the version of Nginx which will be used, so that you won’t have to write it repeatedly. At the time of this writing, the current stable version of Nginx is 1.12.1:

    1
    NGINX_VERSION=1.12.1
    
  3. Download Nginx source from its official website using wget:

    1
    2
    cd
    wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
    
  4. Extract the Nginx source:

    1
    tar -xvzf nginx-${NGINX_VERSION}.tar.gz
    
  5. Move to the Nginx directory:

    1
    cd nginx-${NGINX_VERSION}/
    
  6. Configure Nginx to include the PageSpeed module:

    1
    ./configure --add-module=$HOME/ngx_pagespeed-${NPS_VERSION} ${PS_NGX_EXTRA_FLAGS}
    
  7. Build and install Nginx:

    1
    2
       make
       sudo make install
    

Configuring Nginx with ngx_pagespeed

  1. Pagespeed requires a new directory where it can store the cache of minified CSS and javascript. Create this directory if it doesn’t already exist:

    1
    sudo mkdir /var/ngx_pagespeed_cache
    
  2. Change the ownership of the directory so that the webserver can write to it:

    1
    sudo chown www-data:www-data /var/ngx_pagespeed_cache
    
  3. Open /usr/local/nginx/conf/nginx.conf and add the following code to the server block where you want to enable the PageSpeed module:

    /usr/local/nginx/conf/nginx.conf
    1
    2
    3
    4
    5
    6
    7
    pagespeed on;
    pagespeed FileCachePath /var/ngx_pagespeed_cache;
    location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
     add_header "" "";
    }
    location ~ "^/pagespeed_static/" { }
    location ~ "^/ngx_pagespeed_beacon$" { }
    

Also make sure that Nginx is running as www-data. In the top of the conf file, uncomment user and replace nobody with www-data.

Starting Nginx

Once everything is configured correctly, start the web server.

  1. To start the web server:

    1
    sudo /usr/local/nginx/sbin/nginx
    
  2. To stop the web server:

    1
    sudo /usr/local/nginx/sbin/nginx -s stop
    

Check Your Installation

You may want to check if the module is working before deploying the application:

1
curl -I localhost:80/

You should see something like X-Page-Speed: 1.9.32.6 in the response. Congratulations, you have successfully installed ngx_pagespeed on your Linode.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This guide is published under a CC BY-ND 4.0 license.