In this tutorial, we will show you how to install Nginx With ngx_pagespeed on CentOS. For those of you who didn’t know, PageSpeed (ngx_pagespeed) is an Nginx module created by Google to help Make the Web Faster by rewriting web pages to reduce latency and bandwidth. For the installation, we will need to compile Nginx from the source with the PageSpeed module, as Nginx doesn’t support Dynamic module loading (DSO) unless you want to build your own rpm or deb files.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of Nginx With ngx_pagespeed on the CentOS server.
Install Nginx With ngx_pagespeed on CentOS
Step 1. Install dependencies
yum install gcc-c++ pcre-devel pcre-devel zlib-devel make unzip openssl-devel
Step 2. Download and install ngx_pagespeed and PSOL (PageSpeed Optimization Libraries) source code as follows.
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
Step 3. Download the latest stable version of Nginx from here and build it with ngx_pagespeed support.
## cd /opt/nginx/ ## wget http://nginx.org/download/nginx-1.6.2.tar.gz ## tar -zxf nginx-1.6.2.tar.gz ## cd nginx-1.6.2/ ## ./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
Step 4. You can verify that the ngx_pagespeed module has been added to the Nginx installation using the following command
nginx version: nginx/1.6.2 built by gcc 4.8.2 20131212 (Red Hat 4.8.2-7) (GCC) configure arguments: --add-module=/usr/local/nginx/modules/ngx_pagespeed-1.7.30.3-beta . . . .
Step 5. Configure ngx_pagespeed module in Nginx
nano /etc/nginx/nginx.conf ... # enable ngx_pagespeed pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; ...
Step 6. Create a file cache directory that will be written by Nginx.
mkdir /var/ngx_pagespeed_cache chown nginx:nginx /var/ngx_pagespeed_cache
Step 7. Create an init script for Nginx.
wget -O /etc/init.d/nginx https://raw.githubusercontent.com/Fleshgrinder/nginx-sysvinit-script/master/nginx chmod 0755 /etc/init.d/nginx chown root:root /etc/init.d/nginx
Step 8. Finally, start Nginx.
service nginx start
Congratulations! You have successfully installed Nginx with ngx_pagespeed. Thanks for using this tutorial for installing Nginx with ngx_pagespeed on the CentOS system. For additional help or useful information, we recommend you check the official Nginx website.