Set Up 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. If you’re an expert on something for which we need a guide, you too can get paid to write for us.

What is PageSpeed?

PageSpeed is an open source Google project created to optimize website performance using modules for Apache and nginx. PageSpeed is available as .deb or .rpm binaries, or can be compiled from source.

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:

    hostname -f

System Setup

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

    sudo apt-get update && apt-get upgrade

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

Install required packages

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

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, at the time of writing.

  1. Make sure you are in the home directory:

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

  3. Now, you need to download the source of the module:

  4. Extract the file using unzip command:

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

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

  7. Extract the files using tar command:

    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:

  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:

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

  4. In this command, you are extracting the source of Nginx:

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

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

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

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

    sudo make install

Configure Nginx with ngx_pagespeed

  1. Pagespeed requires a new directory where it can store the cache of minified CSS and javascript:

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

    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:

    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:

    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.

Start Nginx

Now that you have everything configured correctly, start your 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 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):

curl -I website_url_or_IP_adress

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

See Also

comments powered by Disqus

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