Speed-up NGINX using ngx_pagespeed on a CentOS 6 VPS

Leave reply

Speed-up NGINX using ngx_pagespeed in CentOS 6 VPSThe following article will guide you through the steps of compiling and installing Nginx and ngx_pagespeed module on your Linux VPS

Using ngx_pagespeed you can significantly speed-up your websites without needing to tune or change your web-applications.

How is this possible?

ngx_pagespeed runs as a module inside Nginx and rewrites your webpages to make them faster. The rewrite includes minifying CSS and JS (JavaScript), extending cache lifetimes, compressing images and many other web performance best practices.

UPDATE THE SYSTEM

Before proceeding any further, make sure you are in a screen session and check if your CentOS 6 VPS is fully up-to-date by running:

## screen -U -S pagespeed-screen
## yum update

INSTALL DEPENDENCIES

Since we are going to compile Nginx and ngx_pagespeed from source, we need to install some required packages on the system using yum

## yum install gcc-c++ pcre-devel pcre-devel zlib-devel make unzip openssl-devel

DOWNLOAD NGX_PAGESPEED AND PSOL

Proceed with downloading ngx_pagespeed and PSOL (PageSpeed Optimization Libraries) to /opt/nginx/modules

## mkdir -p /opt/nginx/modules
## cd /opt/nginx/modules
## wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.7.30.3-beta.zip
## unzip release-1.7.30.3-beta.zip
## cd ngx_pagespeed-release-1.7.30.3-beta/
## wget https://dl.google.com/dl/page-speed/psol/1.7.30.3.tar.gz
## tar -xzf 1.7.30.3.tar.gz

DOWNLOAD AND COMPILE NGINX

Next, download NGINX and build it with ngx_pagespeed support

## cd /opt/nginx/
## wget http://nginx.org/download/nginx-1.4.5.tar.gz
## tar -zxf nginx-1.4.5.tar.gz
## cd nginx-1.4.5/
## ./configure --add-module=/opt/nginx/modules/ngx_pagespeed-release-1.7.30.3-beta \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/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=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--user=nginx \
--group=nginx

## make
## make install

once NGINX is compiled and installed on the system you can verify it has support for ngx_pagespeed using the following command

## nginx -V

nginx version: nginx/1.4.5
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --add-module=/opt/nginx/modules/ngx_pagespeed-release-1.7.30.3-beta --prefix=/usr/local/nginx --sbin-path=/usr/local/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=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --user=nginx --group=nginx

ENABLE THE MODULE

enable the ngx_pagespeed module by adding the following to your NGINX server blocks

...
# enable ngx_pagespeed
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
...

SET-UP INIT SCRIPT AND START NGINX

create init script for nginx in /etc/init.d/nginx and add the following

## vim /etc/init.d/nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

create user for nginx and make the init script executable by running

## useradd -r nginx
## chmod  +x /etc/init.d/nginx

set-up pagespeed filecachepath directory

## mkdir /var/ngx_pagespeed_cache
## chown nginx: /var/ngx_pagespeed_cache

start and add nginx to your system start-up

## nginx -t
## service nginx restart
## chkconfig nginx on

TEST THE SET-UP

you can simply use curl and check if headers contain X-Page-Speed

## curl -s -I http://localhost | grep ^X-Page-Speed
X-Page-Speed: 1.7.30.3-3721

Additionally, you can learn more on how to fully optimize ngx_pagespeed at https://developers.google.com/speed/pagespeed/module


Of course you don’t have to do any of this if you use one of our linux vps hosting services, in which case you can simply ask our expert linux admins to install this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

 

2 Responses to “Speed-up NGINX using ngx_pagespeed on a CentOS 6 VPS”

  1. tallulah

    Hi,

    I followed your guidance (http://www.rosehosting.com/blog/run-wordpress-w3totalcache-with-lemp-nginx-php-fpmapc-and-mysql-stack-on-centos-6-vps-for-maximum-performance/) to set up my nginx on Centos.

    I have this issue when trying to restart nginx after enable ngx_pagespeed module:
    [emerg] unknown directive “pagespeed” in /etc/nginx/nginx.conf:51

    I think that I should uninstall nginx first, then reinstall nginx from source ?
    Could you please advise me what is the best way to uninstall nginx to add this ngx_pagespeed ?

    Thank you.

    Reply

Leave a Reply