Warning: Undefined array key "HTTP_REFERER" in /home/webhostingchenna/domains/webhostingchennai.co.in/public_html/blog/wp-content/plugins/wp-linkbuilder/plugin/plugin_class.inc.php on line 637
Nginx With Ngx_pagespeed On CentOS, Webhostingchennai.co.in

Nginx with ngx_pagespeed on CentOS

This tutorial shows, how you can install NGINX with ngx_pagespeed.

Ngx-pagespeed is a open source Nginx module that can be used to speeds up your site and reduces page load time. It works by automatically applying web performance best practices to pages and associated assets without requiring you to modify your existing workflow.

Step 1 – Update your System

Update your system to the latest stable version by using the following command :

sudo yum update -y

After the system update, restart the system and login with sudo user.

 Step 2 – Install the Dependencies

Install packages in order to compile Nginx. You can install all these dependencies by running the following command:

sudo yum install gcc cmake unzip wget gcc-c++ pcre-devel zlib-devel -y

 Step 3 – Compile Nginx with Ngx-pagespeed

To install Nginx with ngx-pagespeed module you will need to compile Nginx from source. First, you will need to download the Nginx source so use the following command :

wget http://nginx.org/download/nginx-1.12.0.tar.gz

To compile Nginx with ngx_pagespeed module, you will also need to download ngx_pagespeed source package.Use the below command :

wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.12.34.2-stable.zip

Once both packages are download, Extract them with the following command:

tar -xvzf nginx-1.12.0.tar.gz
tar -xvzf v1.12.34.2-stable.zip

Download the PageSpeed optimization libraries to compile Nginx. Use the below command to download the PageSpeed optimization libraries :

cd ngx_pagespeed-
wget https://dl.google.com/dl/page-speed/psol/
tar -xvzf
cd ..

Now, let’s start to compile Nginx with the pagespeed module.
First, change the directory to the Nginx source directory with the following command:

cd nginx-1.12.0

Next, configure the Nginx source with the following command:

sudo ./configure --add-module=$HOME/ngx_pagespeed- --user=nobody --group=nobody --pid-path=/var/run/nginx.pid ${PS_NGX_EXTRA_FLAGS}

Once the configuration is completed, compile Nginx by running the following command:

sudo make

After that you can install nginx by running the following command:

sudo make install

Once Nginx is installed, you will need to create a symlink for the Nginx:

sudo ln -s /usr/local/nginx/conf/ /etc/nginx
sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

Step 4 – Create Nginx Startup Script

You will also need to create a startup script for Nginx to stop and start Nginx. You can do this by creating nginx file inside /etc/init.d directory:

sudo nano /etc/init.d/nginx

Add the following lines:

# 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

prog=$(basename $nginx)


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


make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      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

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

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

restart() {
    configtest || return $?
    sleep 1

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

force_reload() {

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

rh_status() {
    status $prog

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

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

Save and close the file when you are finished, then give execution permission to it:

sudo chmod +x /etc/init.d/nginx

Now, start nginx service and enable it to start on boot time with the following command:

sudo systemctl start nginx
sudo systemctl enable nginx

Once you are done, you can proceed to the next step.

Step 5 – Configure Nginx

Now, Nginx is installed with ngx-pagespeed support. By default PageSpeed is disabled. Before enable it, it is recommended to test your website speed. Next, you will need to create a cache directory for ngx-pagespeed and also assign ownership for it to Nginx:

sudo mkdir -p /var/ngx_pagespeed_cache
sudo chown -R nobody:nobody /var/ngx_pagespeed_cache

Next, you will need to make some changes in /etc/nginx/nginx.conf file. Run the following command:

sudo nano /etc/nginx/nginx.conf

Add the following code between the server block:

# Pagespeed main settings

pagespeed on;
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 ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }

Note: If you are using virtual hosting then add the above pagespeed directives to each server block config file to enable pagespeed on each website.

Save and close the file when you are finished, check the Nginx for any configuration error with the following command:

sudo nginx -t

You should see the following output:

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

Finally, restart Nginx for the changes to take effect:

sudo systemctl restart nginx

Once Nginx is configured, you can proceed to test Ngx-pagespeed.

Step 6 – Test Ngx-pagespeed

Nginx is now up and running.You can check it by running the following command:

curl -I -p http://localhost

The output will be as follows:

HTTP/1.1 200 OK
Server: nginx/1.12.0
Content-Type: text/html
Connection: keep-alive
Vary: Accept-Encoding
Date: Wed, 21 Jun 2017 17:21:08 GMT
Cache-Control: max-age=0, no-cache

You should see X-Page-Speed and it’s version number in the above output.

You have successfully installed Ngx_pagespeed on your server.


You may also like...

Leave a Reply

Your email address will not be published.