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
@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