Connecting the PageSpeed ​​module for Nginx in Centos 8

Pagespeed (or ngx_pagespeed) Is an open source module for the Nginx and Apache web server used to improve the speed of websites by reducing the website load time in the browser.

We install the necessary software in order to build a module from sources

$ sudo dnf -y install wget curl unzip gcc-c++ pcre-devel zlib-devel
$ sudo dnf -y install gcc-c++ pcre-devel zlib-devel make unzip libuuid-devel

Create a directory where we will upload the archives with the source code. Later it can be deleted

$ mkdir ~/nginx
$ cd ~/nginx

We look at the version of Nginx

$ nginx -v
nginx version: nginx/1.18.0

Download and unzip the same version of Nginx

$ wget http://nginx.org/download/nginx-1.18.0.tar.gz
$ tar -xvzf nginx-1.18.0.tar.gz

Download and unzip the PageSpeed ​​sources

$ wget https://github.com/apache/incubator-pagespeed-ngx/archive/v1.13.35.2-stable.tar.gz
$ tar -xvzf v1.13.35.2-stable.tar.gz

Download PSOL software and unpack it to the directory with the pagespeed module

$ cd incubator-pagespeed-ngx-1.13.35.2-stable
$ wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz
$ tar -xvzf 1.13.35.2-x64.tar.gz

Launching the Nginx configuration process

$ cd ~/nginx/nginx-1.18.0/
$ sudo ./configure --with-compat --add-dynamic-module=../incubator-pagespeed-ngx-1.13.35.2-stable
[...]
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

Starting with Nginx 1.11.5, it is possible to compile individual dynamic modules without compiling the full Nginx software. Compiling the ngx_pagespeed module

$ sudo make modules

Copy it to the nginx directory

$ sudo cp objs/ngx_pagespeed.so /etc/nginx/modules
$ sudo chmod 644 /etc/nginx/modules/ngx_pagespeed.so

Add the ngx_pagespeed.so module to the Nginx config

$ sudo nano /etc/nginx/nginx.conf
...
pid        /var/run/nginx.pid;

load_module modules/ngx_pagespeed.so;

events {
...

We will connect the PageSpeed ​​module in all our sites, for this we will create a file with the appropriate content

$ sudo nano /etc/nginx/conf.d/pagespeed.conf
pagespeed on;
pagespeed FileCachePath /var/cache/pagespeed;
pagespeed HttpCacheCompressionLevel 0;

# HTTPS Support
pagespeed FetchHttps enable;

# PageSpeed Filters
# CSS Minification
pagespeed EnableFilters combine_css,rewrite_css;

# JS Minification
pagespeed EnableFilters combine_javascript,rewrite_javascript;

# Images Optimization
pagespeed EnableFilters lazyload_images;
pagespeed EnableFilters rewrite_images;
pagespeed EnableFilters convert_jpeg_to_progressive,convert_png_to_jpeg,convert_jpeg_to_webp,convert_to_webp_lossless;

# Remove comments from HTML
pagespeed EnableFilters remove_comments;

# Remove WHITESPACE from HTML
pagespeed EnableFilters collapse_whitespace;

Checking

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart nginx and check the status

$ sudo systemctl restart nginx
$ systemctl restart nginx

See if pagespeed works

$ curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.18.0
Content-Type: text/html
Connection: keep-alive
Vary: Accept-Encoding
Date: Fri, 18 Dec 2020 13:46:53 GMT
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache
Sidebar