Install Nginx from Source with ngx_pagespeed and psol

By | January 15, 2015

In this Post we will see how to Install Nginx from Source with ngx_pagespeed and psol.

I am using Debian (Testing)Jessie, for this demonstration. Having said that, I do not encourage anyone to host a production site on an OS Distribution, which is still officially at testing. This configuration works on Debian7 as well.  Just for the record, the FQDN of my test machine is “” and its ip address is

Lets install the dependencies:

root@debian:~# apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip libssl-dev memcached

Now will create a directory named “/nginx”. This will be the location where we will download all our sources and make the installation. The ngx_pagespeed module and psol libraries are developed by Google.

root@debian:~# mkdir /nginx
root@debian:~# cd /nginx
root@debian:/nginx# wget
root@debian:/nginx# tar -xf nginx-1.7.9.tar.gz
root@debian:/nginx# wget
root@debian:/nginx# unzip
root@debian:/nginx# cd ngx_pagespeed-release-
root@debian:/nginx/ngx_pagespeed-release- wget
root@debian:/nginx/ngx_pagespeed-release- tar -xzvf

For this installation I have used the latest (as of this writing) nginx mainline release 1.7.9 and ngx_pagespeed and psol libraries version is

Now, we will navigate inside the “nginx-1.7.9” directory and make the installation.

root@debian:~# cd /nginx/nginx-1.7.9/
root@debian:/nginx/nginx-1.7.9# ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/ --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=www-data --group=www-data --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --add-module=/nginx/ngx_pagespeed-release-

root@debian:/nginx/nginx-1.7.9# make
root@debian:/nginx/nginx-1.7.9# make install

Here I have used the “./configure”  with many parameters. you may run “./configure – -help” in the prompt to view all the options that can be passed to the configure call. The options that I have used here is sufficient to run a powerful nginx webserver. However, this is not the end of the road, as you can still pass many options as per your requirement. It is important to note here that I have chosen nginx process to run as user “www-data” and group “www-data“. This user and group is related to web server process and created by default during the installation of Debian. Also one important option is “– -pid-path=/var/run/“. This path will be used next when we create the init script to start and stop the nginx service.

Init Script for nginx process:

Create file “/etc/init.d/nginx” with you favourite editor and put the following contents in it.

root@www:~# vi /etc/init.d/nginx
#! /bin/sh
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
 . /etc/default/nginx
set -e
case "$1" in
 echo -n "Starting $DESC: "
 start-stop-daemon --start --quiet --pidfile /var/run/$ \
 echo "$NAME."
 echo -n "Stopping $DESC: "
 start-stop-daemon --stop --quiet --pidfile /var/run/$ \
 --exec $DAEMON
 echo "$NAME."
 echo -n "Restarting $DESC: "
 start-stop-daemon --stop --quiet --pidfile \
 /var/run/$ --exec $DAEMON
 sleep 1
 start-stop-daemon --start --quiet --pidfile \
 /var/run/$ --exec $DAEMON -- $DAEMON_OPTS
 echo "$NAME."
 echo -n "Reloading $DESC configuration: "
 start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$ \
 --exec $DAEMON
 echo "$NAME."
 echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
 exit 1
 exit 0

Now we will assgin the execute permission to this script and update rc.d

root@debian:~# chmod u+x /etc/init.d/nginx 
root@debian:~# /usr/sbin/update-rc.d -f nginx defaults

At the point we need to create the directory “/var/cache/nginx“. Otherwise nginx will fail to start.

root@debian:~# mkdir /var/cache/nginx
root@debian:~# systemctl start nginx.service
root@debian:~# systemctl enable nginx.service

You can use stop, restart and status as options with systemctl, to stop, restart or check the status of nginx service respectively.

You can also use “service nginx start”, “service nginx stop” and “service nginx restart” commands as well in place of systemctl command. In case of Debian 7 wheezy,  you will have to use  service commands only.

Configure Nginx to Pull in ngx_pagespeed module and Page Speed Optimazation Libraries (psol) for serving sites to the clients:

Create to directories, “common” and “conf.d” inside “/etc/nginx“. We will tell nginx configuration file “nginx.conf “to look into “conf.d” directory to serve the sites. And inside “common” we will create a file called “pagespeed.conf” which will contain the ngx_pagespeed and psol definitions, we will call on this file on per virtual host basis, in the sense, if for one particular site, we need the optimization, then we will call for it, and for some other site if we do not need the optimization, we will not call it.

root@debian:/etc/nginx# mkdir /etc/nginx/common && mkdir /etc/nginx/conf.d

Tell nginx to look into conf.d directory for serving sites. My nginx.conf file looks something like this:

root@debian:~# vi /etc/nginx/nginx.conf
user www-data;
worker_processes 1;
error_log error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/;
events {
 worker_connections 1024;
http {
include /etc/nginx/conf.d/*;
 include mime.types;
 default_type application/octet-stream;
 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 # '$status $body_bytes_sent "$http_referer" '
 # '"$http_user_agent" "$http_x_forwarded_for"';
 access_log access.log;
 sendfile on;
 tcp_nopush on;
 #keepalive_timeout 0;
 keepalive_timeout 65;
 #gzip on;

Now lets create the “pagespeed.conf” file with pagespeed and psol definitions.

root@debian:~# vi /etc/nginx/common/pagespeed.conf
# enable ngx_pagespeed
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
pagespeed MemcachedServers "";
# enable CoreFilters
pagespeed RewriteLevel CoreFilters;
# enable additional filters if needed
pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters lazyload_images;
pagespeed EnableFilters insert_dns_prefetch;
# PageSpeed Admin
pagespeed AdminPath /pagespeed_admin;
# PageSpeed Cache Purge
pagespeed EnableCachePurge on;
pagespeed PurgeMethod PURGE;
# Enable requests for pagespeed optimized resources go to the pagespeed handler.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
 add_header "" "";
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

Now lets create a demo site with ngx_pagespeed and psol optimization

root@debian:~# vi /etc/nginx/conf.d/
server {
 include /etc/nginx/common/pagespeed.conf;
 listen 80;
 root /usr/share/nginx/html;
 index index.php index.html index.htm;
 location / {
 try_files $uri $uri/ /index.html;
 error_page 404 /404.html;
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;
 # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
 #location ~ \.php$ {
 #try_files $uri =404;
 #fastcgi_pass unix:/var/run/php5-fpm.sock;
 #fastcgi_index index.php;
 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 #include fastcgi_params;

Lets restsart the nginx service and access our demo site and the pagespeed admin area also:

root@debian:~# systemctl restart nginx.service

The Image Below shows Opening the url in web browser


Accessing the pagespeed admin area with url is displayed in the image below:


So this is a basic demo of how to Install Nginx from Source with ngx_pagespeed and psol.


1) Nginx Download Link:

2) Nginx Installation Documentation Link:

3) Installing ngx_pagespeed and psol Link:


2 thoughts on “Install Nginx from Source with ngx_pagespeed and psol

  1. EverybodY

    Why there is no pagespeed included inside the package nginx-extras of debian 8 jessie, while there was in the package of wheezy/squeeze? Will they add it?

    1. rudraraj Post author

      Hi Everybody,

      The google pagespeed module and the psol libraries, have not been included in the official debian repositories till date,be it debian wheezy or debian jessie. Having said that if you make use of the dotdeb repository ( by following instruction number 1 and 3, you will be able to install pagespeed as well. As in dotdeb repository, it is a part of ‘nginx-extras’ package. I have used it a couple of times. But my experience says, installation done by compiling these packages, give you a more robust setup and a better control. As to when pagespeed will be a part of the official repository is something which debian maintainers will be in a better position to answer.



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.