Skip to content

Instantly share code, notes, and snippets.

@jniltinho
Last active June 26, 2023 19:41
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save jniltinho/393728f0323a3aad636387d6e584de77 to your computer and use it in GitHub Desktop.
Save jniltinho/393728f0323a3aad636387d6e584de77 to your computer and use it in GitHub Desktop.
How to Install Nginx and Google PageSpeed on Debian/Ubuntu
#!/bin/bash
## Install PageSpeed on Debian 8/9 and Ubuntu 16.04 64Bits
## https://www.howtoforge.com/tutorial/how-to-install-nginx-and-google-pagespeed-on-ubuntu-16-04/
## http://nginx.org/en/linux_packages.html
## https://www.modpagespeed.com/doc/build_ngx_pagespeed_from_source
## https://developers.google.com/speed/pagespeed/module/
## Debian ISO: https://cdimage.debian.org/cdimage/archive/8.9.0/amd64/iso-cd/
## No Link abaixo tem o pacote do Nginx para o Debian 8 64Bits
## https://github.com/jniltinho/ispconfig/tree/master/packages/debian/jessie
## Run as root (sudo su)
NPS_VERSION=1.12.34.3-stable
NPS_RELEASE_NUMBER=${NPS_VERSION/stable/}
sed -i "s|# deb-src|deb-src|" /etc/apt/sources.list
apt-get update
apt-get install -y lsb-release dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip curl
cd ~
mkdir -p ~/nginx_source/
cd ~/nginx_source/
apt-get source nginx
rm -rf /var/lib/apt/lists/
apt-get update
apt-get build-dep -y nginx
sed -i "s|deb-src|# deb-src|" /etc/apt/sources.list
cd ~
wget https://github.com/pagespeed/ngx_pagespeed/archive/v${NPS_VERSION}.tar.gz
tar xvfz v${NPS_VERSION}.tar.gz
cd ngx_pagespeed-${NPS_VERSION}/
psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz
psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL)
wget ${psol_url}
tar -xzvf $(basename ${psol_url})
sed -i "s|--with-http_auth_request_module|--with-http_auth_request_module --add-module=$HOME/ngx_pagespeed-${NPS_VERSION}|" $HOME/nginx_source/nginx-1.*.*/debian/rules
cd ~/nginx_source/nginx-1.*.*/
dpkg-buildpackage -b
cd ../
## Caso exista o Nginx o Script vai efetuar o Backup
if [ -f /etc/nginx/nginx.conf ]; then service nginx stop; cp -aR /etc/nginx /etc/nginx_$$; fi
dpkg --force-all -i nginx_1.*_all.deb nginx-common_1.*_all.deb nginx-full_1.*_amd64.deb
## Caso exista o Backup vai ser efetuado o restore
if [ -f "/etc/nginx_$$/nginx.conf" ]; then rm -rf /etc/nginx/*; cp -aR /etc/nginx_$$/* /etc/nginx/; fi
## Para Finalizar
mkdir -p /var/ngx_pagespeed_cache && chmod 777 /var/ngx_pagespeed_cache
## Set on PageSpeed Config /etc/nginx/conf.d/default.conf
sed -i "s|# concurs with nginx's one|include /etc/nginx/mod_pagespeed.conf;|" /etc/nginx/sites-enabled/default
echo 'pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }' >/etc/nginx/mod_pagespeed.conf
## Para checar as configs do Nginx
nginx -t
nginx -V
## Reinciar o Nginx
service nginx restart
@cryptoniekko
Copy link

./install_pagespeed_nginx.sh: line 35: cd: ngx_pagespeed-1.12.34.3-stable/: No such file or directory
./install_pagespeed_nginx.sh: line 37: scripts/format_binary_url.sh: No such file or directory
wget: missing URL
Usage: wget [OPTION]... [URL]...

Try `wget --help' for more options.
basename: missing operand
Try 'basename --help' for more information.
tar: option requires an argument -- 'f'
Try 'tar --help' or 'tar --usage' for more information.

PSOL URL looking be broken

@toptools
Copy link

toptools commented Mar 2, 2018

configuring additional modules
adding module in /root/ngx_pagespeed-1.12.34.3-stable
./configure: error: no /root/ngx_pagespeed-1.12.34.3-stable/config was found
debian/rules:183: recipe for target 'config.arch.full' failed
make[1]: *** [config.arch.full] Error 1
make[1]: Leaving directory '/root/nginx_source/nginx-1.13.3'
debian/rules:141: recipe for target 'build' failed
make: *** [build] Error 2
dpkg-buildpackage: error: debian/rules build gave error exit status 2
dpkg: error: cannot access archive 'nginx_1.*_all.deb': No such file or directory
sed: can't read /etc/nginx/sites-enabled/default: No such file or directory
./install_pagespeed_nginx.sh: line 66: /etc/nginx/mod_pagespeed.conf: No such file or directory
./install_pagespeed_nginx.sh: line 70: nginx: command not found
./install_pagespeed_nginx.sh: line 71: nginx: command not found
Failed to restart nginx.service: Unit nginx.service not found.

@toptools
Copy link

toptools commented Mar 2, 2018

debian 9

make: *** [build] Error 2
dpkg-buildpackage: error: debian/rules build gave error exit status 2
dpkg: error: cannot access archive 'nginx_1.*_all.deb': No such file or directory
sed: can't read /etc/nginx/sites-enabled/default: No such file or directory
./install_pagespeed_nginx.sh: line 66: /etc/nginx/mod_pagespeed.conf: No such file or directory
./install_pagespeed_nginx.sh: line 70: nginx: command not found
./install_pagespeed_nginx.sh: line 71: nginx: command not found
Failed to restart nginx.service: Unit nginx.service not found.

@toptools
Copy link

toptools commented Mar 2, 2018

cd ngx_pagespeed-${NPS_VERSION}/ >>>>cd incubator-pagespeed-ngx-${NPS_VERSION}/

sed -i "s|--with-http_auth_request_module|--with-http_auth_request_module --add-module=$HOME/ngx_pagespeed-${NPS_VERSION}|" >>>>>>sed -i "s|--with-http_auth_request_module|--with-http_auth_request_module --add-module=$HOME/incubator-pagespeed-ngx-${NPS_VERSION}|"

@toptools
Copy link

toptools commented Mar 2, 2018

Setting up nginx-full (1.13.3-1bpo9+1) ...
Setting up nginx (1.13.3-1
bpo9+1) ...
Processing triggers for systemd (232-25+deb9u1) ...
Processing triggers for man-db (2.7.6.1-2) ...
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx version: nginx/1.13.3
built with OpenSSL 1.1.0f 25 May 2017
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/root/nginx_source/nginx-1.13.3=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --add-module=/root/incubator-pagespeed-ngx-1.12.34.3-stable --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-mail=dynamic --with-mail_ssl_module --add-dynamic-module=/root/nginx_source/nginx-1.13.3/debian/modules/nginx-auth-pam --add-dynamic-module=/root/nginx_source/nginx-1.13.3/debian/modules/nginx-dav-ext-module --add-dynamic-module=/root/nginx_source/nginx-1.13.3/debian/modules/nginx-echo --add-dynamic-module=/root/nginx_source/nginx-1.13.3/debian/modules/nginx-upstream-fair --add-dynamic-module=/root/nginx_source/nginx-1.13.3/debian/modules/ngx_http_substitutions_filter_module

@toptools
Copy link

toptools commented Mar 2, 2018

apt-get -y upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
nginx-full : Depends: libnginx-mod-http-auth-pam (= 1.13.3-1bpo9+1) but it is not installed
Depends: libnginx-mod-http-dav-ext (= 1.13.3-1
bpo9+1) but it is not installed
Depends: libnginx-mod-http-echo (= 1.13.3-1bpo9+1) but it is not installed
Depends: libnginx-mod-http-geoip (= 1.13.3-1
bpo9+1) but it is not installed
Depends: libnginx-mod-http-image-filter (= 1.13.3-1bpo9+1) but it is not installed
Depends: libnginx-mod-http-subs-filter (= 1.13.3-1
bpo9+1) but it is not installed
Depends: libnginx-mod-http-upstream-fair (= 1.13.3-1bpo9+1) but it is not installed
Depends: libnginx-mod-http-xslt-filter (= 1.13.3-1
bpo9+1) but it is not installed
Depends: libnginx-mod-mail (= 1.13.3-1bpo9+1) but it is not installed
Depends: libnginx-mod-stream (= 1.13.3-1
bpo9+1) but it is not installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

apt --fix-broken install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following packages will be REMOVED:
nginx nginx-full
0 upgraded, 0 newly installed, 2 to remove and 1 not upgraded.
1 not fully installed or removed.
After this operation, 13.2 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 39063 files and directories currently installed.)
Removing nginx (1.13.3-1bpo9+1) ...
Removing nginx-full (1.13.3-1
bpo9+1) ...
Processing triggers for man-db (2.7.6.1-2) ...

systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2018-03-02 05:08:56 EST; 18s ago
Docs: man:nginx(8)
Process: 32296 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 32005 (code=exited, status=0/SUCCESS)
CPU: 107ms

Mar 02 05:05:37 vps158253 systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 02 05:05:37 vps158253 systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
Mar 02 05:05:37 vps158253 systemd[1]: Started A high performance web server and a reverse proxy server.
Mar 02 05:08:56 vps158253 systemd[1]: Stopping A high performance web server and a reverse proxy server...
Mar 02 05:08:56 vps158253 systemd[1]: Stopped A high performance web server and a reverse proxy server.
root@vps158253:~# sudo systemctl start nginx.service
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.

@darylounet
Copy link

Or you can use directly my Debian / Ubuntu packages :
https://packagecloud.io/DaryL/libnginx-mod-pagespeed

@degoya
Copy link

degoya commented Jun 12, 2018

@darylounet how about a module for Nginx 1.15?

@pi0
Copy link

pi0 commented Sep 20, 2018

My alternative using automated install script:

https://gist.github.com/pi0/a4ce5fdfe5a2a8fa7b51315d1f13bb00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment