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 “www.example.com” and its ip address is 192.168.76.135.

Lets install the dependencies:

[email protected]:~# 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.

[email protected]:~# mkdir /nginx
[email protected]:~# cd /nginx
[email protected]:/nginx#
[email protected]:/nginx# wget http://nginx.org/download/nginx-1.7.9.tar.gz
[email protected]:/nginx# tar -xf nginx-1.7.9.tar.gz
[email protected]:/nginx# wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.9.32.3-beta.zip
[email protected]:/nginx# unzip release-1.9.32.3-beta.zip
[email protected]:/nginx# cd ngx_pagespeed-release-1.9.32.3-beta/
[email protected]:/nginx/ngx_pagespeed-release-1.9.32.3-beta#
[email protected]:/nginx/ngx_pagespeed-release-1.9.32.3-beta# wget https://dl.google.com/dl/page-speed/psol/1.9.32.3.tar.gz
[email protected]:/nginx/ngx_pagespeed-release-1.9.32.3-beta# tar -xzvf 1.9.32.3.tar.gz

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 1.9.32.3

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

[email protected]:~# cd /nginx/nginx-1.7.9/
[email protected]:/nginx/nginx-1.7.9# 
[email protected]:/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/nginx.pid --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-1.9.32.3-beta

[email protected]:/nginx/nginx-1.7.9# make
[email protected]:/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/nginx.pid“. 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.

[email protected]:~# vi /etc/init.d/nginx
#! /bin/sh
### BEGIN INIT INFO
# 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
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
 . /etc/default/nginx
fi
set -e
case "$1" in
 start)
 echo -n "Starting $DESC: "
 start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
 --exec $DAEMON -- $DAEMON_OPTS
 echo "$NAME."
 ;;
 stop)
 echo -n "Stopping $DESC: "
 start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
 --exec $DAEMON
 echo "$NAME."
 ;;
 restart|force-reload)
 echo -n "Restarting $DESC: "
 start-stop-daemon --stop --quiet --pidfile \
 /var/run/$NAME.pid --exec $DAEMON
 sleep 1
 start-stop-daemon --start --quiet --pidfile \
 /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
 echo "$NAME."
 ;;
 reload)
 echo -n "Reloading $DESC configuration: "
 start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
 --exec $DAEMON
 echo "$NAME."
 ;;
 *)
 N=/etc/init.d/$NAME
 echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
 exit 1
 ;;
 esac
 exit 0

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

[email protected]:~# chmod u+x /etc/init.d/nginx 
[email protected]:~# /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.

[email protected]:~# mkdir /var/cache/nginx
[email protected]:~# systemctl start nginx.service
[email protected]:~# 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.

[email protected]:/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:

[email protected]:~# 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/nginx.pid;
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.

[email protected]:~# vi /etc/nginx/common/pagespeed.conf
# enable ngx_pagespeed
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
pagespeed MemcachedServers "127.0.0.1:11211";
# 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 www.example.com with ngx_pagespeed and psol optimization

[email protected]:~# vi /etc/nginx/conf.d/www.example.com
server {
 include /etc/nginx/common/pagespeed.conf;
 listen 80;
 root /usr/share/nginx/html;
 index index.php index.html index.htm;
 server_name www.example.com;
 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:

[email protected]:~# systemctl restart nginx.service

The Image Below shows Opening the url http://www.example.com in web browser

Screenshot

Accessing the pagespeed admin area with url http://www.example.com/pagespeed_admin is displayed in the image below:

Screenshot-1

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

References:

1) Nginx Download Link: http://nginx.org/en/download.html

2) Nginx Installation Documentation Link: http://nginx.org/en/docs/

3) Installing ngx_pagespeed and psol Link:

 https://developers.google.com/speed/pagespeed/module/build_ngx_pagespeed_from_source

 

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?

    Reply
    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 (https://www.dotdeb.org/instructions/) 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.

      Regards
      Rudraraj

      Reply

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.