Skip to content

Instantly share code, notes, and snippets.

@nurrony
Last active September 27, 2019 11:00
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save nurrony/4ac985e6d8fe7f603f10ec8dc59777c1 to your computer and use it in GitHub Desktop.
Save nurrony/4ac985e6d8fe7f603f10ec8dc59777c1 to your computer and use it in GitHub Desktop.
All configuration files needed to install nginx on Ubuntu 16.04 Compile Nginx from source with nginx-rtmp-module, pagespeed and cache-purge module.
#change it to latest version
NPS_VERSION=1.11.33.0;
echo "Changing Directory to $HOME..."
cd $HOME;
echo "Nginx version to install: " && \
read NGINX_VERSION && \
echo "Downloading nginx-$NGINX_VERSION..." && \
wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && \
echo "Installing Nginx Dependencies..." && \
sudo apt-get -qq -y install checkinstall build-essential software-properties-common git libssl-dev openssl libpcre3 libpcre3-dev zlib1g zlib1g-dbg zlib1g-dev && \
tar xzf nginx-* && rm $HOME/nginx-*.tar.gz && \
cd nginx-*/ && \
mkdir modules && cd modules && \
echo 'cloning nginx-rtmp-module and ngx_cache_purge modules' && \
git clone https://github.com/arut/nginx-rtmp-module && git clone https://github.com/FRiCKLE/ngx_cache_purge && \
#https://developers.google.com/speed/pagespeed/module/build_ngx_pagespeed_from_source#build-instructions
echo 'fetching pagespeed module' && \
curl -fsSOL https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.tar.gz && \
tar xzf release-${NPS_VERSION}-beta.tar.gz && \
cd ngx_pagespeed-release-${NPS_VERSION}-beta/ && \
curl -fsSOL https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz && \
tar -xzvf ${NPS_VERSION}.tar.gz && \ # extracts to psol/
cd $HOME/nginx-* && ./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-pcre \
--with-file-aio \
--with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -DTCP_FASTOPEN=23' \
--with-ld-opt='-Wl,-z,relro -Wl,--as-needed -L /usr/lib' \
--with-ipv6 \
--with-debug \
--without-http_scgi_module \
--without-http_uwsgi_module \
--add-module=./modules/nginx-rtmp-module \
--add-module=./modules/ngx_cache_purge \
--add-module=./modules/ngx_pagespeed-release-${NPS_VERSION}-beta && \
make && \
sudo checkinstall --install=no -y && \
sudo dpkg -i *.deb && \
#create necessary directories
sudo mkdir -p /etc/nginx/sites-avaiable && sudo chmod 755 /etc/nginx/sites-avaiable && \
sudo mkdir -p /etc/nginx/sites-enabled && sudo chmod 755 /etc/nginx/sites-enabled && \
sudo mkdir -p /etc/nginx/conf.d && sudo chmod 755 /etc/nginx/conf.d && sudo mkdir -p /var/log/nginx;
user www-data;
worker_processes auto;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# 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_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 20;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.apple.mpegurl;
# Disable for IE < 6 because there are some known problems
gzip_disable "MSIE [1-6].(?!.*SV1)";
# Add a vary header for downstream proxies to avoid sending cached gzipped files to IE6
gzip_vary on;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
######################
# RTMP Server #
######################
rtmp {
server {
listen 1935;
timeout 60s;
publish_time_fix off;
idle_streams off;
include /etc/nginx/rtmp-servers/*;
}
}
# Stop dance for nginx
# =======================
# Save in /etc/nginx/systemd/system directory
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /var/run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /media/nmrony/devzone/www;
index index.php index.html index.htm;
server_name nmrony.local;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /media/nmrony/devzone/www;
index index.php index.html index.htm;
server_name nmrony.local;
location / {
autoindex on;
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

Configure Nginx with RTMP, Pagespeed, purge-cache Modules

Confguration files here reflects My Need. You need to change some configurations in compile-nginx-from-source.sh, nginx.conf and nmrony.local file according to your need For more please read http://nmrony.info/configuring-nginx-virtual-host-with-php-7-0/

Nginx Systemd commads

#To check status
systemctl status nginx 

#To start nginx
systemctl start nginx 

#To restart nginx
systemctl start nginx 

 
#To stop nginx
systemctl stop nginx 
@guleriagishere
Copy link

do i need to create nginx.service file in /etc/nginx/systemd/system folder beacuse i did and its not working?

@speedwheel
Copy link

speedwheel commented Oct 18, 2016

@guleriagishere You put the script in /lib/systemd/system/ and create a file named nginx.service
As here: https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

Hope that helps.

@Firegarden
Copy link

why start with NPS_VERSION=1.11.33.0;
why not make it 1.13.35.2?

@IotaSpencer
Copy link

the script was made in 2016, was probably the latest at the time? I'm not too sure

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