How to Install nginx and google PageSpeed on Ubuntu 16.04 (Xenial Xerus)

Nginx (engine-x) is an open source and high-performance HTTP server, reverse proxy and IMAP/POP3 proxy server. The outstanding features of Nginx are stability, a rich feature set, simple configuration and low memory consumption. This tutorial shows how to build a Nginx .deb package for Ubuntu 16.04 from source that has Google PageSpeed module compiled in.


PageSpeed is a web server module developed by Google to speed up the website response time, optimize the returned HTML and reduce the page load time. ngx_pagespeed features include:

  • Image optimization: stripping meta-data, dynamic resizing, recompression.
  • CSS & JavaScript minification, concatenation, inlining, and outlining.
  • Small resource inlining.
  • Deferring image and JavaScript loading.
  • HTML rewriting.
  • Cache lifetime extension.

see more https://developers.google.com/speed/pagespeed/module/.

Prerequisites

  • Ubuntu Server 16.04 - 64 bit
  • root privileges

What we will do in this tutorial :

  1. Install the prerequisite packages.
  2. Installing nginx with ngx_pagespeed.
  3. Testing.

Install the build dependencies

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

Installing nginx with ngx_pagespeed

Step 1 - Add the nginx repository

Create a new repository file /etc/apt/sources.list.d/nginx.list with vim.

vim /etc/apt/sources.list.d/nginx.list

There you add the lines:

deb http://nginx.org/packages/ubuntu/ xenial nginx
deb-src http://nginx.org/packages/ubuntu/ xenial nginx

Save the file and exit the editor.

Add the key and update the repository:

sudo sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62
sudo apt-get update

Step 2 - Download nginx 1.10 from ubuntu repository

Create a new directory for the nginx source files and download the nginx sources with apt:

cd ~
mkdir -p ~/new/nginx_source/
cd ~/new/nginx_source/
apt-get source nginx

Sometimes, there is an error: 'packages cannot be authenticated'.

You can solve it by typing command below:

rm -rf /var/lib/apt/lists/
apt-get update

Next, install all dependencies to build the nginx package.

apt-get build-dep nginx

Step 3 - Download Pagespeed

Create a new directory for PageSpeed and download the PageSpeed source.

In this tutorial, we will use pagespeed 1.11.33.

mkdir -p ~/new/ngx_pagespeed/
cd ~/new/ngx_pagespeed/
ngx_version= 1.11.33.3
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${ngx_version}-beta.zip
unzip release-${ngx_version}-beta.zip

cd ngx_pagespeed-release-${ngx_version}-beta/
wget https://dl.google.com/dl/page-speed/psol/${ngx_version}.tar.gz
tar -xzvf ${ngx_version}.tar.gz

Step 4 - Configure nginx to build with Pagespeed

Go to the 'nginx_source' directory and edit the 'rules' file with vim.

cd ~/new/nginx_source/nginx-1.10.1/debian/
vim rules

Add the new line under 'COMMON_CONFIGURE_ARGS':

--add-module=../../ngx_pagespeed/ngx_pagespeed-release-1.11.33.3-beta \

Save and exit.

Enable pagespeed in Nginx

Step 5 - Build the nginx Ubuntu package and install it

Go to the nginx source directory and build nginx from source with the dpkg-buildpackage command:

cd ~/new/nginx_source/nginx-1.10.1/
dpkg-buildpackage -b

The nginx Ubuntu package will be saved under ~/new/ngix_source/. Once package building is complete, please look in the directory:

cd ~/new/ngix_source/
ls

The Nginx Ubuntu package has been built.

And install nginx and modules deb with dpkg command.

dpkg -i *.deb

Testing

Step 1 - Testing with the Nginx Command

Run nginx -V  to check that the ngx_pagespeed module has been built into nginx.

nginx -V

Test nginx configuration

Step 2 - Testing with Curl Command

Go to nginx configuration directory.

cd /etc/nginx/conf.d/

and edit default virtual host configuration file.

vim default.conf

Paste configuration below to enable ngx_pagespeed.

pagespeed on;

# Needs to exist and be writable by nginx.  Use tmpfs for best performance.
pagespeed FileCachePath /var/ngx_pagespeed_cache;

# Ensure requests for pagespeed optimized resources go to the pagespeed handler
# and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
  add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

Save and exit.

The Nginx vhost.

Next, test the nginx configuration file and make sure there is no error:

nginx -t

Restart nginx:

systemctl restart nginx

Finally, access the nginx web server with the curl command:

curl -I 192.168.1.6

Nginx with pagespeed has been installed.

Conclusion

The nginx web server there is a stable and fast open source http server that supports a variety of modules for content delivery optimization. One of these modules is the 'PageSpeed module' which is developed by google. Unlike apache, nginx modules are not dynamically loadable, so you have to select the desired modules when you build the nginx package.

Share this page:

2 Comment(s)

Add comment

Comments

From: Alex P at: 2016-09-13 23:05:07

Hello and thank you very much for sharing.

I made a mistake to the default.conf file: I misunderstand your instruction and deleted it entirely. 

After apt-get remove nginx I restarted the whole process but now I do not have the sites-available and sites-enabled folders. How can I start entirely anew?

From: Petter Neumann at: 2016-09-27 13:24:41

 I'm following this tutorial to install Nginx under ubuntu 16.04 x64 server.  The servere already has a running/installed  "nginx version: nginx/1.10.0 (Ubuntu)"  running a website.   Since I wanted to try pagespeed and also use openssl ; followed this tutorial, except for using https://github.com/pagespeed/ngx_pagespeed/archive/release-1.11.33.4-beta.zip, but I hasd to use      ' dpkg-buildpackage -b -uc -us '  , if not I got a compilation error about missing signing.

When compiling , evrything is sucessfull.  But when installing the built packages with  ' dpkg -i *.deb '  I gett the following eror:

 

[email protected]:~/new/nginx_source# [email protected]:~/new/nginx_source# dpkg -i *.deb(Leser database ... 135322 filer og kataloger er for øyeblikket installerte.)Preparing to unpack nginx_1.10.1-1~xenial_amd64.deb ...Unpacking nginx (1.10.1-1~xenial) over (1.10.0-0ubuntu0.16.04.2) ...dpkg: error processing archive nginx_1.10.1-1~xenial_amd64.deb (--install): prøver å skrive over «/usr/share/nginx/html/index.html» som også finnes i pakken nginx-common 1.10.0-0ubuntu0.16.04.2dpkg-deb: feil: subprocess lim inn was killed by signal (Røret ble brutt)Preparing to unpack nginx-dbg_1.10.1-1~xenial_amd64.deb ...Unpacking nginx-dbg (1.10.1-1~xenial) over (1.10.1-1~xenial) ...Preparing to unpack nginx-module-geoip_1.10.1-1~xenial_amd64.deb ...Unpacking nginx-module-geoip (1.10.1-1~xenial) over (1.10.1-1~xenial) ...Preparing to unpack nginx-module-image-filter_1.10.1-1~xenial_amd64.deb ...Unpacking nginx-module-image-filter (1.10.1-1~xenial) over (1.10.1-1~xenial) ...Preparing to unpack nginx-module-njs_1.10.1.0.0.20160414.1c50334fbea6-1~xenial_amd64.deb ...Unpacking nginx-module-njs (1.10.1.0.0.20160414.1c50334fbea6-1~xenial) over (1.10.1.0.0.20160414.1c50334fbea6-1~xenial) ...Preparing to unpack nginx-module-perl_1.10.1-1~xenial_amd64.deb ...Unpacking nginx-module-perl (1.10.1-1~xenial) over (1.10.1-1~xenial) ...Preparing to unpack nginx-module-xslt_1.10.1-1~xenial_amd64.deb ...Unpacking nginx-module-xslt (1.10.1-1~xenial) over (1.10.1-1~xenial) ...dpkg: dependency problems prevent configuration of nginx-dbg: nginx-dbg krever nginx (= 1.10.1-1~xenial). Men:  Versjon av nginx på systemet er 1.10.0-0ubuntu0.16.04.2.  Version of nginx on system, provided by nginx-core:amd64, is <none>.dpkg: error processing package nginx-dbg (--install): kravproblem - setter ikke opp pakkendpkg: dependency problems prevent configuration of nginx-module-geoip: nginx-module-geoip krever nginx (= 1.10.1-1~xenial). Men:  Versjon av nginx på systemet er 1.10.0-0ubuntu0.16.04.2.  Version of nginx on system, provided by nginx-core:amd64, is <none>.dpkg: error processing package nginx-module-geoip (--install): kravproblem - setter ikke opp pakkendpkg: dependency problems prevent configuration of nginx-module-image-filter: nginx-module-image-filter krever nginx (= 1.10.1-1~xenial). Men:  Versjon av nginx på systemet er 1.10.0-0ubuntu0.16.04.2.  Version of nginx on system, provided by nginx-core:amd64, is <none>.dpkg: error processing package nginx-module-image-filter (--install): kravproblem - setter ikke opp pakkendpkg: dependency problems prevent configuration of nginx-module-njs: nginx-module-njs krever nginx (= 1.10.1-1~xenial). Men:  Versjon av nginx på systemet er 1.10.0-0ubuntu0.16.04.2.  Version of nginx on system, provided by nginx-core:amd64, is <none>.dpkg: error processing package nginx-module-njs (--install): kravproblem - setter ikke opp pakkendpkg: dependency problems prevent configuration of nginx-module-perl: nginx-module-perl krever nginx (= 1.10.1-1~xenial). Men:  Versjon av nginx på systemet er 1.10.0-0ubuntu0.16.04.2.  Version of nginx on system, provided by nginx-core:amd64, is <none>.dpkg: error processing package nginx-module-perl (--install): kravproblem - setter ikke opp pakkendpkg: dependency problems prevent configuration of nginx-module-xslt: nginx-module-xslt krever nginx (= 1.10.1-1~xenial). Men:  Versjon av nginx på systemet er 1.10.0-0ubuntu0.16.04.2.  Version of nginx on system, provided by nginx-core:amd64, is <none>.dpkg: error processing package nginx-module-xslt (--install): kravproblem - setter ikke opp pakkenDet oppsto feil ved behandling av: nginx_1.10.1-1~xenial_amd64.deb nginx-dbg nginx-module-geoip nginx-module-image-filter nginx-module-njs nginx-module-perl n[email protected]:~/new/nginx_source#

 

It seems that the error is due to the already installed nginx version.  How to aviod errors when installing on a system with exsistting nginx instalation ?   I would like to install as an "upgrade" of the exsisting standard ubuntu nginx instalation, using the ubuntu default instalation paths to avoid movingall files and configuration of the exsisting instalation  - this is a " live" web site and I want to avoid downtime.