Building Nginx with ngx_pagespeed on a Small Ubuntu VPS

Published:

The setup below uses a lightweight VPS environment:

Ubuntu 14.04 x86
RAM 512M
SWAP 64M
DISK 5G

The goal is to compile Nginx from source and include the ngx_pagespeed module during the build. Since this is not an installation from the default package repository, the required build tools and libraries need to be installed first.

Prepare the system

Update the package list and install the dependencies needed for compiling Nginx and building modules such as SSL, PCRE, zlib, and PageSpeed support:

sudo apt-get update&&apt-get upgrade -y
sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip libssl-dev -y

Download ngx_pagespeed

Set the ngx_pagespeed version, download the release archive, unpack it, then fetch and extract the matching PSOL package:

cd
NPS_VERSION=1.9.32.4
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip
unzip release-${NPS_VERSION}-beta.zip
cd ngx_pagespeed-release-${NPS_VERSION}-beta/
wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
tar -xzvf ${NPS_VERSION}.tar.gz

Compile and install Nginx

Create a dedicated www group and user for running Nginx, then download and extract the Nginx source package:

cd
groupadd www
useradd -s /sbin/nologin -g www www
NGINX_VERSION=1.8.0
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
tar -xvzf nginx-${NGINX_VERSION}.tar.gz
cd nginx-${NGINX_VERSION}/
./configure \
--user=www \
--group=www  \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_spdy_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta \

sudo make
sudo make install
sudo ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx

This configuration installs Nginx under /usr/local/nginx, enables several commonly used modules, and adds ngx_pagespeed from the directory downloaded earlier.

Add an init script

Create /etc/init.d/nginx so the Nginx service can be managed more conveniently:

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx

After that, Nginx can be controlled with the following service commands:

service nginx {start|stop|restart|force-reload|reload|status|configtest|quietupgrade|terminate|destroy}

Check whether the installation and configuration syntax are valid:

root@bwgvps:~# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Configure ngx_pagespeed

Edit the Nginx configuration file at /usr/local/nginx/conf/nginx.conf.

Inside the server block, add:

include gp.conf

Then create a new file named gp.conf under /usr/local/nginx/conf/ with the following contents:

pagespeed on;
pagespeed FileCachePath /usr/local/nginx/pagespeed/;
pagespeed RewriteLevel PassThrough;
pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters canonicalize_javascript_libraries;
pagespeed EnableFilters combine_css;
pagespeed EnableFilters combine_javascript;
pagespeed EnableFilters elide_attributes;
pagespeed EnableFilters extend_cache;
pagespeed EnableFilters flatten_css_imports;
pagespeed CssFlattenMaxBytes 5120;
pagespeed EnableFilters lazyload_images;
pagespeed EnableFilters rewrite_javascript;
pagespeed EnableFilters rewrite_images;
pagespeed EnableFilters insert_dns_prefetch;
pagespeed EnableFilters prioritize_critical_css;

Reload Nginx after saving the configuration. To confirm that ngx_pagespeed is active, send a request with curl and check the response headers:

root@bwgvps:~# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.8.0
Content-Type: text/html
Connection: keep-alive
Date: Sat, 07 Feb 1970 00:54:49 GMT
X-Page-Speed: 1.9.32.4-7251
Cache-Control: max-age=0, no-cache

If the response includes the X-Page-Speed header, the module has been loaded and is working.