Install Nginx ngx_pagespeed Module on Ubuntu 16.04

Updated by Linode Contributed by Vaibhav Rajput

Contribute on GitHub

Report an Issue | View File | Edit File

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:

    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:

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

  3. Define the version of ngx_pagespeed to be installed :

    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:

  5. Extract the file and move to the module’s directory:

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

    psol_url=${NPS_RELEASE_NUMBER}.tar.gz [ -e scripts/ ]
    psol_url=$(scripts/ 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:

  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:

  3. Download Nginx source from its official website using wget:

  4. Extract the Nginx source:

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

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

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

        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:

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

    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:

    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:

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

    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:

curl -I localhost:80/

You should see something like X-Page-Speed: 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.

See Also

comments powered by Disqus

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