nginx with PageSpeed on Ubuntu 14.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 $250 per published guide.


This document describes how you can install nginx (pronounced engine-x) with the ngx_pagespeed module. PageSpeed helps you minify CSS, JavaScript and images, remove whitespaces from HTML, and perform many other tasks.

Before starting this guide, make sure that you have read through and completed our Getting Started and Securing Your Server guides.

Set the hostname

  1. Before you install any package, ensure that your hostname is correct by completing the Setting Your Hostname section of the Getting Started guide. Issue the following commands to verify that hostname:

    1
    2
    hostname
    hostname -f
    

System Setup

  1. Make sure your system is up to date using apt:

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

This ensures that all software is up to date and running the latest version.

Installing required packages

Pagespeed requires some extra packages, which you should install for proper operation. Do so by issuing the following command in the terminal:

1
sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip

Download ngx_pagespeed module

After installing necessary packages, you must download the module. In this guide, you will be installing the latest ngx_pagespeed, version 1.9.32.6, at the time of writing.

  1. Make sure you are in the home directory:

    1
    cd
    
  2. You are defining the version number which will be installed:

    1
    NPS_VERSION=1.9.32.6
    
  3. Now, you need to download the source of the module:

    1
    wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip
    
  4. Extract the file using unzip command:

    1
    unzip release-${NPS_VERSION}-beta.zip
    
  5. Move to the module’s directory:

    1
    cd ngx_pagespeed-release-${NPS_VERSION}-beta/
    
  6. Download some additional files:

    1
    wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
    
  7. Extract the files using tar command:

    1
    tar -xzvf ${NPS_VERSION}.tar.gz
    

Download and build Nginx

Now that we have downloaded ngx_pagespeed, we need to compile Nginx with the ngx_pagespeed module. Issue the following commands in the terminal.

  1. You now need to move back to your home directory:

    1
    cd
    
  2. In this command, you are defining the version of Nginx which will be used, so that you won’t have to write it again and again. At the time of this writing, the current, stable version of Nginx is 1.8.0:

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

    1
    wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
    
  4. In this command, you are extracting the source of Nginx:

    1
    tar -xvzf nginx-${NGINX_VERSION}.tar.gz
    
  5. After extracting the source, you must change your directory:

    1
    cd nginx-${NGINX_VERSION}/
    
  6. Next, compile the Nginx with PageSpeed module:

    1
    ./configure --add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta
    
  7. At this point, you are getting everything ready for the installation:

    1
    make
    
  8. Issue the following command and it will install Nginx with Pagespeed module:

    1
    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:

    1
    sudo mkdir /var/ngx_pagespeed_cache
    
  2. You need to change the ownership of the folder so that webserver can write to this directory:

    1
    sudo chown www-data:www-data /var/ngx_pagespeed_cache
    
  3. You need to add some new code in Nginx config file in order to use the module:

    1
    sudo nano /usr/local/nginx/conf/nginx.conf
    
  4. Then, you need to add the following code to the server block where you want to enable 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

Now that you have everything configured correctly, start your 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 if module works or not

You have compiled and configured the module. You may want to check if the module is working (or not) before deploying the application. Issue the following command at the terminal of your local machine (i.e., your computer):

1
curl -I website_url_or_IP_adress

You will see something like X-Page-Speed: 1.9.32.6 in the response. This means that 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.