Script to Install and Upgrade Nginx from Source

Nginx logoNginx is a free, open-source, high-performance HTTP server and reverse proxy. It is widely used in many really high traffic websites such as WordPress.com. It is also used here in tinywp.in since the beginning. There are plenty of methods to install it on any VPS or dedicated servers. The official installation instructions available from the official wiki. Here I show you my own script to compile it from source, install and upgrade using a bash script.

Pre-requisites To Nginx

I created this script specifically for Amazon Linux that is based on CentOS. If you are already into bash scripting, you wouldn’t find it hard to tweak to your setup. Anyway, here we go…


#!/bin/bash
# script to install and upgrade nginx from source
# for questions - contact the author at tinywp.in

### VARIABLES ###
PRE_PACK="gcc make pcre-devel zlib-devel"
# OPT_PACK="openssl-devel"
VER="1.0.14"
PREV_VER="1.0.13"
USER="apache"
GROUP="apache"
INSTALL_DIR="/usr/local/nginx"
PROG_NAME="nginx"

# optional configuration directives
# --with-http_stub_status_module
# --with-http_realip_module
# --without-http_limit_zone_module
# --without-http_limit_req_module
# --without-http_memcached_module
CONFIGURE_OPTIONS="--user=$USER --group=$GROUP
                   --prefix=$INSTALL_DIR-$VER
                   --without-http_upstream_ip_hash_module
                   --without-http_map_module
                   --without-http_autoindex_module
                   --without-http_auth_basic_module
                   --without-http_ssi_module
                   --without-http_fastcgi_module
                   --without-http_empty_gif_module"
USER_BIN="/usr/sbin"
TEMP_DIR="some_temp_dir_name"

##### DO NOT EDIT BELOW THIS LINE #####
CWD=`pwd`

echo "Installing/upgrading your web server..."

# install the prerequisites
yum -y -q install $PRE_PACK $OPT_PACK > /dev/null

mkdir $TEMP_DIR; cd $TEMP_DIR
# download and install
wget -q http://nginx.org/download/nginx-$VER.tar.gz
tar xzf ng*.tar.gz && rm -f ng*.tar.gz; cd nginx-*
./configure $CONFIGURE_OPTIONS
make && make install

# remove previous symlinks, if any
rm -f $INSTALL_DIR > /dev/null
rm -f /etc/nginx > /dev/null
rm -f $USER_BIN/$PROG_NAME > /dev/null

# sym links creation
cd /usr/local; ln -s $INSTALL_DIR-$VER nginx
cd /etc/; ln -s $INSTALL_DIR/conf nginx
cd $USER_BIN; ln -s $INSTALL_DIR/sbin/$PROG_NAME .

# copy previous configuration files when upgrading
cd $INSTALL_DIR/conf/
cp nginx.conf nginx-conf-$(date +%F) # take a backup
cat $INSTALL_DIR-$PREV_VER/conf/nginx.conf > nginx.conf

cp -R $INSTALL_DIR-$PREV_VER/conf/conf.d/ .
cd .. # backu to $INSTALL_DIR
cp $INSTALL_DIR-$PREV_VER/logs/*.pid logs/

# remove the temp directory
cd $CWD; rm -rf $TEMP_DIR

echo "done."; echo

# restart the daemon
$PROG_NAME -t && service $PROG_NAME restart

exit 0

To Do

As you may have already noticed, a lot of improvements can be done in this script. For example, we can check, if this script is being executed as a root or not, we can check, if this is a fresh installation (or upgrade).

Leave a Reply

Your email address will not be published. We use cookies to prevent spam comments. Required fields are marked *

css.php