Categories Tutorials

Speed-up NGINX using ngx_pagespeed on a CentOS 6 VPS

The 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.


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


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


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-
## unzip release-
## cd ngx_pagespeed-release-
## wget https://dl.google.com/dl/page-speed/psol/
## tar -xzf


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- \
--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 \

## 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- --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 ngx_pagespeed module by adding the following to your NGINX server blocks

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


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

## vim /etc/init.d/nginx

# 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:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   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
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


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

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

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 Optimized CentOS Web 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.


admin :

View Comments (5)

  • Hi,

    I followed your guidance (https://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.

  • I was searching for compiling and installing Nginx and ngx_pagespeed module on my Linux VPS.

    This post helped me a lot and successfully installed on my website.

    Thank you Guys.

  • Hello.

    Perfect tutorial. I'm on CentOS 7 and everything went excellent until the "service nginx restart". When I did that, I received a FAILED error and the suggestion to put "systemctl status nginx.service" and a "journalctl -xn" to see more regard the problem.

    With "systemctl restart nginx.service -l" I received:

    "nginx.service - SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
    Loaded: loaded (/etc/rc.d/init.d/nginx)
    Active: failed (Result: exit-code) since mar 2015-03-17 13:05:46 EDT; 1min 3s ago
    Process: 19766 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=203/EXEC)

    mar 17 13:05:46 alemany.me systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
    mar 17 13:05:46 alemany.me systemd[1]: nginx.service: control process exited, code=exited status=203
    mar 17 13:05:46 alemany.me systemd[1]: Failed to start SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.
    mar 17 13:05:46 alemany.me systemd[1]: Unit nginx.service entered failed state."

    Any idea why?

    My server has nothing installed and nothing configured.

    Best regards.

    • This tutorial is about how to install ngx_pagespeed on CentOS 6. As CentOS 7 uses systemd you need to create a service file in /usr/lib/systemd/system/ , for example:

      cat >> /usr/lib/systemd/system/nginx.service