[UPDATE 2023] How to Install and Compile ngx_pagespeed with Nginx with VestaCP/myVesta on Linux
First, I would like to thank Mysterydata for their article on this subject. The thing is they did provide a downloadable library ngx_pagespeed.so but this file is compiled for the Nginx 1.18.0 version. And now VestaCP provide another Nginx version and their library is not compatible anymore.
Some of my websites really need Pagespeed, so I found the way to compile it myself. I will also provide the downloadable library here for the last version.
Downloadable ngx_pagespeed (easy method)
ngx_pagespeed Downloads
Google Pagespeed version | Nginx version | ngx_pagespeed |
1.13.35.1-0 | Nginx 1.19.0 | ngx_pagespeed-1.19.0 |
1.13.35.2-0 | Nginx 1.20.1 | ngx_pagespeed-1.20.1 |
1.13.35.2-0 | Nginx 1.20.2 | ngx_pagespeed-1.20.2 |
1.13.35.2-0 | Nginx 1.22.1 | ngx_pagespeed-1.22.1 |
Get the module
1 2 3 4 5 |
cd /etc/nginx/modules sudo rm -rf ngx_http_brotli* sudo wget --no-cache https://metrodyn.fr/wp-content/uploads/2023/03/ngx_pagespeed-1.22.1.zip sudo unzip ngx_pagespeed-1.22.1.zip sudo rm -rf ngx_pagespeed-1.22.1.zip |
Load ngx_pagespeed in Nginx
1 2 |
sudo mkdir -p /var/ngx_pagespeed_cache sudo chown -R nginx:root /var/ngx_pagespeed_cache |
In /etc/nginx/nginx.conf, add on the first line
1 |
load_module "modules/ngx_pagespeed.so"; |
Enable ngx_pagespeed on domain vhost
Vhost config files are located on a specific folder with VestaCP :
For each domain, modify both the non-ssl and the ssl config files
1 2 |
nano /home/admin/conf/web/domain.com.nginx.conf nano /home/admin/conf/web/domain.com.nginx.ssl.conf |
Add under server { block add the foloowing code
1 2 3 4 5 6 |
#Pagespeed config pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } location ~ "^/ngx_pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon" { } |
Restart Nginx
Check the configs
1 |
nginx -t |
If the output show OK, proceed with restart
1 |
service nginx restart |
Testing Pagespeed
Test nginx pagespeed is working or not via this Online checker : https://ismodpagespeedworking.com/
Or via cURL
1 |
curl -I -p https://www.domain.tld/ |
You will see a line like this: X-Page-Speed: 1.13.35.2-0
Compile ngx_pagespeed for any Nginx version
Simply follow the guide from Google itself, at the chapter Build instructions: https://www.modpagespeed.com/doc/build_ngx_pagespeed_from_source
Note the results of the line
1 |
sudo nginx -V |
And change the version of Nginx on the line
1 |
NGINX_VERSION=1.22.1 |
And add the content of configure arguments: to the end of the line
1 |
./configure --add-module=$HOME/$nps_dir ${PS_NGX_EXTRA_FLAGS} |
Example
On my Nginx with VestCP installation, I use the command sudo nginx -V and I have the output
1 2 3 4 5 |
nginx version: nginx/1.22.1 built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1) built with OpenSSL 1.1.1 11 Sep 2018 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-p ath=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/v ar/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 --ht tp-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/c ache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=ng inx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addit ion_module --with-http_auth_request_module --with-http_dav_module --with-http_fl v_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp 4_module --with-http_random_index_module --with-http_realip_module --with-http_s ecure_link_module --with-http_slice_module --with-http_ssl_module --with-http_st ub_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_mod ule --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/d ata/builder/debuild/nginx-1.19.0/debian/debuild-base/nginx-1.19.0=. -fstack-prot ector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --w ith-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pi e' |
So I will us the ./configure command below
1 |
./configure <strong>--add-dynamic-module</strong>=$HOME/$nps_dir ${PS_NGX_EXTRA_FLAGS} --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-p ath=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/v ar/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 --ht tp-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/c ache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=ng inx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addit ion_module --with-http_auth_request_module --with-http_dav_module --with-http_fl v_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp 4_module --with-http_random_index_module --with-http_realip_module --with-http_s ecure_link_module --with-http_slice_module --with-http_ssl_module --with-http_st ub_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_mod ule --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/d ata/builder/debuild/nginx-1.19.0/debian/debuild-base/nginx-1.19.0=. -fstack-prot ector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --w ith-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pi e' |
More generally the compilation command will be
1 2 3 4 5 6 7 8 |
#[check nginx's site for the latest version] NGINX_VERSION=<strong>1.22.1</strong> cd wget -O- http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz | tar -xz cd nginx-${NGINX_VERSION}/ <strong>./configure --add-module=$HOME/$nps_dir ${PS_NGX_EXTRA_FLAGS} --add-module=$HOME/$nps_dir ${PS_NGX_EXTRA_FLAGS} --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-p ath=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/v ar/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 --ht tp-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/c ache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=ng inx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addit ion_module --with-http_auth_request_module --with-http_dav_module --with-http_fl v_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp 4_module --with-http_random_index_module --with-http_realip_module --with-http_s ecure_link_module --with-http_slice_module --with-http_ssl_module --with-http_st ub_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_mod ule --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/d ata/builder/debuild/nginx-1.19.0/debian/debuild-base/nginx-1.19.0=. -fstack-prot ector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --w ith-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pi e' </strong>make sudo make install |
La commande –add-dynamic-module permet de récupérer le fichier /usr/lib/nginx/modules/ngx_pagespeed.so. Si vous préfèrez recompiler nginx pour intégrer la librairie, il faut utiliser la commande –add-module.
Problems with the building
./configure: error: SSL modules require the OpenSSL library.
You will need to install libssl-dev:
1 |
sudo apt-get install libssl-dev |
free(): double free detected in tcache 2
If you have this error, you will first need to clean your repository:
1 |
make clean |
And then, use –add-dynamic-module= instead of –add-module= on the ./configure step:
1 |
./configure ---add-dynamic-module=$HOME/$nps_dir ${PS_NGX_EXTRA_FLAGS} |
What about the performances?
When I use gtmetrix.com, I could pass from a score of C to a score of B.
2 commentaires
[Update 2021] Guide Ultime d'Installation d'une Boutique b2c/b2b Prestashop 1.7 - Partie 1 - Metrodyn · 23 février 2021 à 14 h 34 min
[…] Voir l’article à propos de l’installation de Pagespeed […]
[Update 2021] Guide Ultime d’Installation d’une Boutique Prestashop 1.7 – VPS, myVesta – Partie 1 - Metrodyn · 20 décembre 2021 à 9 h 14 min
[…] Voir l’article à propos de l’installation de PageSpeed […]