How to Compile Nginx with PageSpeed on Debian 8

This tutorial will teach you how to compile Nginx with ngx_pagespeed module on Debian 8 Jessie.

1 Backup Nginx Config Files

During the installation, your Nginx config files will be overriden. The following command will copy config files to current working directory. (Don’t left out the trailing dot. The dot is your current working directory.)

cp /etc/nginx/nginx.conf /etc/nginx/sites-available/*.conf .

2. Add Nginx Repository

Import PGP key from Nginx.

wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

Edit source list file.

sudo vi /etc/apt/sources.list

Add the following two lines at the end of the file.

deb http://nginx.org/packages/mainline/debian/ jessie nginx
deb-src http://nginx.org/packages/mainline/debian/ jessie nginx

Save and close the file. Then update local package index.

sudo apt-get update

3. Download Nginx Source Package

Make a directory in your home folder to store Nginx source package, then cd to the new directory:

mkdir ~/nginx && cd ~/nginx

Download Nginx source package with the below command

sudo apt-get source nginx

4. Download ngx_pagespeed Source Package

Go to Github ngx_pagespeed download page. Download the latest beta release to your home directory. v1.9.32.10-beta at the time of this writting. You may need to change the version number.

cd ~
wget https://codeload.github.com/pagespeed/ngx_pagespeed/zip/v1.9.32.10-beta

unzip it:

sudo apt-get install unzip
unzip v1.9.32.10-beta

cd to the newly-created directory:

cd ngx_pagespeed-1.9.32.10-beta/

Download PageSpeed Optimization Libraries (psol) and extract it.

wget https://dl.google.com/dl/page-speed/psol/1.9.32.10.tar.gz
tar xvf 1.9.32.10.tar.gz

It will create a psol directory under ngx_pagespeed-1.9.32.10-beta directory.

5. Add ngx_pagespeed Module to Nginx Compilation Rules

Edit Nginx compilation rule file.

sudo vi ~/nginx/nginx-1.9.10/debian/rules

In this file you will see two configuration block override_dh_auto_build and configure_debug. In override_dh_auto_build, add the following line at the end. Replace <username> with your real username.

--add-module=/home/<username>/ngx_pagespeed-1.9.32.10-beta

Please note that you need to append a backslash at the –with-ipv6 line, or –add-module will be ignored.

# some text left out.
          --with-file-aio \
         $(WITH_HTTP2) \
          --with-cc-opt="$(CFLAGS)" \
         --with-ld-opt="$(LDFLAGS)" \
         --with-ipv6 \
         --add-module=/home/<username>/ngx_pagespeed-1.9.32.10-beta
 dh_auto_build
configure_debug:
     CFLAGS="" ./configure \
          --prefix=/etc/nginx \
         --sbin-path=/usr/sbin/nginx \
         --conf-path=/etc/nginx/nginx.conf \
# some text left out.

Save and close the file.

6. Compile and Install

cd to the nginx source directory.

cd ~/nginx/nginx-1.9.10/

Install all the dependancies needed to build Nginx:

sudo apt-get build-dep nginx

Use the following command to build the deb package.

sudo dpkg-buildpackage -b

Wait a few minutes. My Debian server completed the task around 5 minutes. When it’s done, there will be two deb files in ~/nginx/ directory. One is the stable version and the other is a debug version. We need to install the stable version. If you have installed nginx before, it’s time to remove old version and then install the new version.

sudo apt-get remove nginx nginx-common nginx-full

sudo dpkg -i nginx_1.9.10-1~jessie_amd64.deb

Once installed, check the config arguments of Nginx.

sudo nginx -V

If you see the following line at the end then ngx_pagespeed module is successfully added to Nginx.

--add-module=/home/<username>/ngx_pagespeed-1.9.32.10-beta

If your website is down right now, you need to restore your config files.

7. Enable ngx_pagespeed Module

pagespeed is installed along with Nginx, but it’s disabled by default. Before enable it, I recommend you to test your website speed at pingdom.com or webpagetest.org. After it’s enabled, do a test again so as to compare the two results.

Create a folder for pagespeed caches and change its ownership to Nginx user.

sudo mkdir -p /var/ngx_pagespeed_cache

sudo chown -R nginx:nginx /var/ngx_pagespeed_cache

Now open Nginx main config file /etc/nginx/nginx.conf

sudo vi /etc/nginx/nginx.conf

Add the following two lines in http block

pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;

Save and close the file. Then edit your server block file.

sudo vi /etc/nginx/conf.d/yourdomain.com.conf

Add the following lines to the server block.

location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }

Save and close the file. Reload Nginx configuration.

sudo service nginx reload

8. Check if PageSpeed is Working

Go to your website. Refresh a few times then check your page source. Hit Ctrl+F key and search pagespeed. You will see that many of your website resource has been processed by pagespeed. Some css files and javascript files are combined into one file. If you use Google Chrome browser, you will see that pictures on your website is in webp format. webp can greatly reduce image file size.

You can also find ngx_pagespeed is working by comparing your website speed test.

Also on you server you can issue the following command:

curl -I -p http://localhost| grep X-Page-Speed

You will see X-Page-Speed and it’s version number.

X-Page-Speed: 1.9.32.10-7423

9. Hold Nginx from Being Upgraded

If a newer version of Nginx is available in the repository, the apt-get upgrade command will upgrade Nginx by default and you ngx_pagespeed module will be gone. So we need to prevent Nginx from being upgraded. This can be achieved by the following command:

sudo apt-mark hold nginx

To show what packages are hold:

apt-mark showhold

If you prefer aptitude:

sudo aptitude hold nginx

Hope this article will be helpful to you.

Rate this tutorial
[Total: 2 Average: 4.5]

6 Responses to “How to Compile Nginx with PageSpeed on Debian 8

  • storyanddrama
    9 years ago

    Hi, I followed your instructions and it led to errors but I could fix them so perhaps it can help.

    I had to change several things :

    1/ –add-module=/home//ngx_pagespeed-1.9.32.10-beta
    You don’t say that we have to replace by a name or something – when we are newbies as I am, we’re not sure what to do with it. It led to an error.
    I changed it this way :
    *** I moved the ngx_pagespeed-[version-number] folder at the root of my server.
    *** and of course I updated the “rules” file : –add-module=/ngx_pagespeed-[version-number]

    2/ I don’t know if it’s me or you – perhaps your instruction “cd ~/nginx/nginx-1.9.10/” is wrong, or perhaps I added or forgot to change directory somewhere -, but I had to do step 6 again and again until it worked : when I was launching the “sudo dpkg -i nginx_[version-number]” instruction, it was leading to nginx not taking account of pagespeed.
    The problem was that the instruction operated in the wrong folder !!
    I was launching it from ~/nginx/nginx-[version-number], but it only worked when I launched it from ~/nginx/

    And finally I get the wished “–add-module=/ngx_pagespeed-release-1.11.33.0-beta” 😀

    So thank you very much !

    • Xiao Guoan
      9 years ago

      Yes, I didn’t mention replacing the username. My bad. I updated the article so new visitors won’t be confused.

      As for the second point, Nginx does not take account of Nginx pagespeed usually it’s because the build rule is not configured correctly. i.e. the –add-module=/home//ngx_pagespeed-1.9.32.10-beta line is ignored during the compilation.

      • storyanddrama
        9 years ago

        I was logged as root when I launched the instruction. So it created a home/root/nginx_pagespeed[version-number], which I took time to identify…

        Perhaps the ideal formula for you to teach newbies like me (or even worse 🙂 ) to install pagespeed, would be :

        1/ put in RED the first time you mention it, so it’s clear we have to adapt the code or it won’t work.

        2/ And do the same with “home/” because actually it’s also a variable we can adapt, we can place the nginx_pagespeed folder exactly where we want, we just have to write the right adress in the “RULES” file, right ?

        • Xiao Guoan
          9 years ago

          The home directory for the root user is /root/ so if you execute these instructions as root, you should leave out the /home part. I’m used to prefix commands with sudo as normal user. I should be more elaborative

          Yes, as long as you specify the right address in the “RULES” file, ngx_pagespeed module will be compiled along with Nginx.

          Thanks for your suggestions! 🙂 I will be more elaborative in my future tutorials.

        • storyanddrama
          9 years ago

          Don’t worry you’re doing a great job! You were clear enough to allow me to understand the small details which caused little mistakes. Continue! 🙂

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here