Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rickyhewitt/b25baf88c323d016f97379bf1886f4d1 to your computer and use it in GitHub Desktop.
Save rickyhewitt/b25baf88c323d016f97379bf1886f4d1 to your computer and use it in GitHub Desktop.
Nginx with pagespeed build/install/config script for Ubuntu Bionic (18.04)
#!/bin/bash
# custom nginx install script
#
set -e
NGINX_STRING="nginx-1.14.2"
PAGESPEED_STRING="1.13.35.2-stable"
PSOL_STRING="1.13.35.2-x64"
# build dependencies
apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev libssl-dev -y
# nginx
mkdir -p ~/nginx_build
cd ~/nginx_build
wget -c https://nginx.org/download/$NGINX_STRING.tar.gz
tar -xzvf $NGINX_STRING.tar.gz
wget -c https://github.com/pagespeed/ngx_pagespeed/archive/v$PAGESPEED_STRING.zip
unzip v$PAGESPEED_STRING.zip
cd incubator-pagespeed-ngx-$PAGESPEED_STRING/
wget -c https://dl.google.com/dl/page-speed/psol/$PSOL_STRING.tar.gz
tar -xvzf $PSOL_STRING.tar.gz
cd ~/nginx_build/$NGINX_STRING/
./configure \
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/access.log \
--user=www-data \
--group=www-data \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -march=native -mtune=native -D_GLIBCXX_USE_CXX11_ABI=0' \
--add-module=$HOME/nginx_build/incubator-pagespeed-ngx-$PAGESPEED_STRING ${PS_NGX_EXTRA_FLAGS}
make
make install
# install config
#ln -s /usr/local/nginx/conf/ /etc/nginx
#ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
mkdir -pv /etc/nginx/sites-enabled/
mkdir -pv /etc/nginx/sites-available/
mkdir -pv /etc/nginx/conf.d/
mkdir -pv /etc/nginx/modules-enabled/
mkdir -pv /var/www/
# install service
echo "
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
" > /lib/systemd/system/nginx.service
echo "user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
" > /etc/nginx/nginx.conf
echo "
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
index index.html index.htm;
pagespeed on;
# Needs to exist and be writable by nginx. Use tmpfs for best performance.
pagespeed FileCachePath /var/ngx_pagespeed_cache;
# Ensure requests for pagespeed optimized resources go to the pagespeed handler
# and no extraneous headers get set.
location ~ \"\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+\" {
add_header \"\" \"\";
}
location ~ \"^/pagespeed_static/\" { }
location ~ \"^/ngx_pagespeed_beacon$\" { }
server_name _;
}
" > /etc/nginx/sites-available/www.conf
ln -s "/etc/nginx/sites-available/www.conf" "/etc/nginx/sites-enabled/www.conf"
echo "pagespeed On;
pagespeed FileCachePath \"/var/cache/ngx_pagespeed/\";
pagespeed EnableFilters combine_css,combine_javascript;" > /etc/nginx/conf.d/pagespeed
# start nginx
nginx -t
systemctl start nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment