Skip to content

Instantly share code, notes, and snippets.

@andela-pessien
Last active November 7, 2017 09:32
Show Gist options
  • Save andela-pessien/ac2d693c60e4b1fc4e13276790ddff6c to your computer and use it in GitHub Desktop.
Save andela-pessien/ac2d693c60e4b1fc4e13276790ddff6c to your computer and use it in GitHub Desktop.
A Bash script that builds nginx from source
#!/bin/bash
# This script builds the popular nginx server (https://nginx.org) on *nix systems with
# thread pool support and the following modules:
#
# ngx_http_ssl_module (https://nginx.org/en/docs/http/ngx_http_ssl_module.html)
# ngx_http_v2_module (https://nginx.org/en/docs/http/ngx_http_v2_module.html)
# ngx_http_mp4_module (https://nginx.org/en/docs/http/ngx_http_mp4_module.html)
# ngx_http_gzip_static_module (https://nginx.org/en/docs/http/ngx_http_gzip_static_module.html)
# ngx_http_gunzip_module (https://nginx.org/en/docs/http/ngx_http_gunzip_module.html)
# ngx_http_perl_module (https://nginx.org/en/docs/http/ngx_http_perl_module.html)
# ngx_mail_core_module (https://nginx.org/en/docs/mail/ngx_mail_core_module.html)
# ngx_mail_ssl_module (https://nginx.org/en/docs/mail/ngx_mail_ssl_module.html)
# ngx_stream_core_module (https://nginx.org/en/docs/stream/ngx_stream_core_module.html)
# ngx_stream_ssl_module (https://nginx.org/en/docs/stream/ngx_stream_ssl_module.html)
#
# To specify an installation path other than /usr/local/nginx, pass the path as the first
# argument to the script.
read -d '' HELP_STRING <<'EOF'
This script builds and installs Nginx 1.12.1 on *nix systems with HTTPS,
HTTP/2, thread pool, auto-compression, MP4 streaming, Perl scripting,
debugging and mail proxy and stream proxy support.
Usage:
./nginx_setup.sh <options>
Options:
-h: Display this help menu
--path=PATH: Installation path for Nginx (default /usr/local/nginx)
This must be an absolute path
-v: Verbose (shows output of make command)
Examples:
./nginx_setup.sh -h
./nginx_setup.sh --path=/home/ubuntu/.nginx
./nginx_setup.sh -v --path=/etc/nginx
EOF
INSTALL_PATH=/usr/local/nginx
VERBOSE=0
set -e
function check_path() {
if [ -d "$INSTALL_PATH" ]; then
if [ -f "$INSTALL_PATH/sbin/nginx" ] || [ -f "$INSTALL_PATH/nginx" ]; then
echo "An nginx installation already exists at $INSTALL_PATH"
read -p "Please provide a different installation path (or q to exit): " INSTALL_PATH
case $INSTALL_PATH in
q)
exit 0
;;
*)
check_path
;;
esac
fi
fi
}
function get_nginx_deps() {
if [ ! -d pcre-8.41 ]; then
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz
fi
if [ ! -d zlib-1.2.11 ]; then
wget http://zlib.net/zlib-1.2.11.tar.gz
fi
if [ ! -d openssl-1.1.0f ]; then
wget http://www.openssl.org/source/openssl-1.1.0f.tar.gz
fi
if [ ! -d nginx-1.12.1 ]; then
wget http://nginx.org/download/nginx-1.12.1.tar.gz
fi
for archive in *.tar.gz; do
if [ -f $archive ]; then
if [ $VERBOSE -eq 0]; then
tar xf $archive
else
tar xvf $archive
fi
fi
done
rm -rf ./*.tar.gz
}
function configure() {
./configure \
--prefix="$INSTALL_PATH" \
--with-pcre=../pcre-8.41 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.0f \
--with-debug \
--with-threads \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_perl_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module
}
function spinner() {
spin='-\|/'
i=0
while kill -0 $1 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\r${spin:$i:1}"
sleep .1
done
}
function build_nginx() {
cd nginx-1.12.1
if [ $VERBOSE -eq 0 ]; then
echo "Configuring nginx..."
configure > /dev/null 2>&1
echo "Building nginx..."
make -j4 > /dev/null 2>&1 &
pid=$!
spinner $pid
echo
else
configure
make -j4
fi
}
function install_nginx() {
read -s -p "Please enter your password so nginx can be installed: " password
echo
echo $password | sudo -S make install
if (type -p nginx); then
echo "You already have an active nginx installation."
echo "To make this one the default, run the following command:"
echo
echo "sudo ln -s $INSTALL_PATH/sbin/nginx $(which nginx)"
echo
echo "For safety's sake, please stop any running instances of nginx first."
echo "Copying existing configuration files..."
if [ -d "$(dirname $(which nginx))/../conf" ]; then
CONF_FILES="$(dirname $(which nginx))/../conf"
elif [ -d "$(dirname $(which nginx))/conf" ]; then
CONF_FILES="$(dirname $(which nginx))/conf"
fi
if [ -z "$CONF_FILES" ]; then
echo "Search for configuration files failed. You may have to copy them yourself."
else
cd $CONF_FILES
echo $password | sudo -S cp -a `ls | grep -v 'default\$'` $INSTALL_PATH/conf/
echo "Existing configuration copied. Please review before running."
fi
else
echo $password | sudo -S ln -s "$INSTALL_PATH/sbin/nginx" /usr/local/bin/nginx
fi
}
function show_success() {
echo "Nginx 1.12.1 is installed at $INSTALL_PATH"
echo
ls -lFHa $INSTALL_PATH
}
# Parse options
for opt in "$@"
do
case $opt in
--path=*)
INSTALL_PATH="${opt#*=}"
shift
;;
-h)
echo "$HELP_STRING"
exit 1
;;
-v)
VERBOSE=1
shift
;;
*)
echo "Unknown option"
echo "$HELP_STRING"
exit 1
;;
esac
done
mkdir -p .nginx
cd .nginx
check_path
get_nginx_deps
build_nginx
install_nginx
show_success
set +e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment