How to install ngx_pagespeed centos 7 (Nginx module)

Nginx itself was a web server with very good performance. Howerver, there are many ways to optimize and one of them is to use module developed by Google named PageSpeed(ngx_pagespeed)

ngx_pagespeed speeds up your website and significantly reduces load time by automatically applying page optimization techniques and static components like CSS, Javascript, and Image.

Some good filters of ngx_pagespeed:

  • Collapse Whitespace: reduce bandwidth usage by replacing many whitespaces in HTML with a whitespace.
  • Canonicalize JavaScript Libraries: reduce bandwidth usage by automatically using popular Javascript libraries on free servers (eg: Google).
  • Combine CSS: reduce the number of HTTP requests by combining many CSS files into one file.
  • Combine JavaScript: reduce the number of HTTP requests by combining many JavaScript files into one file.
  • Extend Cache: reduce bandwidth usage by optimizing the browser cache function.
  • Flatten CSS Imports: reduce the number of HTTP requests by deleting @import in CSS file.
  • Lazyload Images: slow down loading images that are not displayed on the user’s browser.
  • Minify JavaScript: reduce bandwidth usage by optimizing Javascript file size.
  • Optimize Images: optimize images using inline images, compress images, or convert GIF to PNG.
  • Pre-Resolve DNS: Reduce DNS resolution time by pre-resolving DNS using HTML.

And there are the other filters and examples of ngx_pagespeed in PageSpeed Filter Examples.

We can not install ngx_pagespeed as a single module but need to install by recompiling Nginx from the initial source code.

ngx_pagespeed1

1. Compile Nginx with ngx_pagespeed

1.1. Prepare

a. Compiler
– To compile, you need at least 512MB RAM (including Swap) and C++, gcc 4.8 or clang 3.3 or more compilers.
On CentOS 6:

# yum -y install gcc-c++ pcre-devel zlib-devel make unzip libuuid-devel
# rpm --import http://linuxsoft.cern.ch/cern/slc6X/i386/RPM-GPG-KEY-cern
# wget -O /etc/yum.repos.d/slc6-devtoolset.repo http://linuxsoft.cern.ch/cern/devtoolset/slc6-devtoolset.repo
# yum install devtoolset-2-gcc-c++ devtoolset-2-binutils
# scl enable devtoolset-2 bash

On CentOS 7:
# yum -y install gcc-c++ pcre-devel zlib-devel make unzip libuuid-devel
On Debian or Ubuntu:
# apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev gcc-mozilla
Check the GCC version:
# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
b. Nginx source code
– Download Nginx source code (the latest version 1.14.0). Beside, the compilation process will integrate OpenSSL (the latest version 1.1.1-pre8). Decompress to the directory /usr/local/src.

# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.14.0.tar.gz && tar -xzvf nginx-1.14.0.tar.gz
# wget https://www.openssl.org/source/openssl-1.1.1-pre8.tar.gz && tar -xzvf openssl-1.1.1-pre8.tar.gz


– LinuxGpoint uses Nginx with Stable 1.12.x – not Mainline 1.13.x.
– For OpenSSL, it is possible to use version 1.0.2x or Stable 1.1.0x
– Successful and stable test versions:

• Nginx 1.12.1 – OpenSSL 1.1.0f – PageSpeed 1.12.34.2-stable
• Nginx 1.12.2 – OpenSSL 1.1.0g – PageSpeed 1.12.34.3-stable/PageSpeed 1.13.35.2-stable
• Nginx 1.14.0 – OpenSSL 1.1.1-pre8 – PageSpeed 1.13.35.2-stable
c. PageSpeed source code
– Download PageSpeed source code (the lastest version 1.13.35.2-stable) with PSOL(PageSpeed Optimization Libraries). Decompress to the directory/usr/local/src.

 # cd /usr/local/src
# NPS_VERSION=1.13.35.2-stable
# wget https://github.com/apache/incubator-pagespeed-ngx/archive/v${NPS_VERSION}.zip
# unzip v${NPS_VERSION}.zip
# nps_dir=$(find . -name "*pagespeed-ngx-${NPS_VERSION}" -type d)
# cd "$nps_dir"
# NPS_RELEASE_NUMBER=${NPS_VERSION/beta/}
# NPS_RELEASE_NUMBER=${NPS_VERSION/stable/}
# psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz
# [ -e scripts/format_binary_url.sh ] && psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL)
# wget ${psol_url}
# tar -xzvf $(basename ${psol_url})

1.2 Compile and replace Nginx

Carry out recompiling Nginx by keeping the initial configuration intact, adding PageSpeed module.
– Access the downloaded Nginx source code directory:
# cd /usr/local/src/nginx-1.14.0
– Save configuration parameters with the usage module of Nginx running on VPS.

# nginx -V
nginx version: nginx/1.12.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

– Recompile Nginx with adding PageSpeed module (keep the old modules intact).


# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl=/usr/local/src/openssl-1.1.1-pre8 --add-module=/usr/local/src/incubator-pagespeed-ngx-1.13.35.2-stable/

– Replace Nginx on VPS with Nginx that has been recompiled:

# make
# make install
Notifying make[1]: Leaving directory `/usr/local/src/nginx-1.12.2' without error is OK.
Note: Depend on each specific need/system, you can add / remove / maintain the module when compiling Nginx with ngx_pagespeed.
- Restart Nginx and check. The following result has successfully integrated ngx_pagespeed into Nginx.
# service nginx restart && nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.1.1-pre8 (beta) 20 Jun 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl
_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-openssl=/usr/local/src/openssl-1.1.1-pre8 --add-module=/usr/local/src/incubator-pagespeed-ngx-1.13.35.2-stable/

2. Configure ngx_pagespeed module

– Before carrying out configuring, you need to create a cache directory for PageSpeed.
# mkdir /var/ngx_pagespeed_cache
# chown nginx:nginx /var/ngx_pagespeed_cache
– To activate and configure ngx_pagespeed, you need to edit the configuration file of Nginx (/etc/nginx/nginx.conf) or Nginx conf for the domain (insert in the block server).
If you use HocVPS Script, edit Nginx configuration of separate domain, located in /etc/nginx/conf.d. Here I take the example with VPS of hocvps.com:
# nano /etc/nginx/conf.d/linuxgpoint.com.conf
Particularly, ngx_pagespeed has many different filters, depend on the purpose of use that you choose accordingly. 2 different levels that you can use are CoreFilters (default) and PassThrough.

2.1. CoreFilters

CoreFilters is a collection of filters that Google verified are safe for most websites. Therefore, this way is suitable for newbie to learn. If you want, you can disable any filter from CoreFilters or add another filter.
Here is an example of configuring ngx_pagespeed with CoreFilters:

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

# let's speed up PageSpeed by storing it in the super duper fast memcached
# pagespeed MemcachedThreads 1;
# pagespeed MemcachedServers "localhost:11211";

# enable CoreFilters
pagespeed RewriteLevel CoreFilters;

# disable particular filter(s) in CoreFilters
pagespeed DisableFilters rewrite_images;

# enable additional filter(s) selectively
pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters lazyload_images;
pagespeed EnableFilters insert_dns_prefetch;


See a list of all filters available in CoreFilters here.

2.2 PassThrough Filters

For those who have already had a lot of knowledge and experience, you should use PassThrough. You will then need to manually activate the filters you need to use.
Configure example with PassThrough:


# enable ngx_pagespeed
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
# let's speed up PageSpeed by storing it in the super duper fast memcached
# pagespeed MemcachedThreads 1;
# pagespeed MemcachedServers "localhost:11211";
# disable CoreFilters
pagespeed RewriteLevel PassThrough;
# enable collapse whitespace filter
pagespeed EnableFilters collapse_whitespace;
# enable JavaScript library offload
pagespeed EnableFilters canonicalize_javascript_libraries;
# combine multiple CSS files into one
pagespeed EnableFilters combine_css;
# combine multiple JavaScript files into one
pagespeed EnableFilters combine_javascript;
# remove tags with default attributes
pagespeed EnableFilters elide_attributes;
# improve resource cacheability
pagespeed EnableFilters extend_cache;
# flatten CSS files by replacing @import with the imported file
pagespeed EnableFilters flatten_css_imports;
pagespeed CssFlattenMaxBytes 5120;
# defer the loading of images which are not visible to the client
pagespeed EnableFilters lazyload_images;
# enable JavaScript minification
pagespeed EnableFilters rewrite_javascript;
# enable image optimization
pagespeed EnableFilters rewrite_images;
# pre-solve DNS lookup
pagespeed EnableFilters insert_dns_prefetch;
# rewrite CSS to load page-rendering CSS rules first.
pagespeed EnableFilters prioritize_critical_css;

Restart web server so that changes have effect
# service nginx restart
Then, you can check in Is Mod PageSpeed Working. ngx_pagespeed with memcache, Zen Opcache is one of the modules I encourage everyone to use with HocVPS Script. In the next article, I will guide people how to install these components to build a perfect server.

We will be happy to hear your thoughts

Leave a reply

Unix
Logo