Friday, March 18, 2016

nginx installation script

install_nginx.sh


NGINX_VERSION="1.9.12"
PCRE_VERSION="8.37"
OPENSSL_VERSION="1.0.2g"
ZLIB_VERSION="1.2.8"

INSTALL_FILE_DIR="/tmp/"

NGINX_DOWNLOAD_URL="http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz"
NGINX_PACKAGE_FILE="$INSTALL_FILE_DIR/nginx-$NGINX_VERSION.tar.gz"
NGINX_SOURCE_DIR="/usr/local/src/nginx"
NGINX_SOURCE_PATH="$NGINX_SOURCE_DIR/nginx-$NGINX_VERSION"  

PCRE_DOWNLOAD_URL="http://downloads.sourceforge.net/project/pcre/pcre/$PCRE_VERSION/pcre-$PCRE_VERSION.tar.gz"
PCRE_PATH="/usr/local/lib/libpcre.so"
PCRE_PACKAGE_FILE="$INSTALL_FILE_DIR/pcre-$PCRE_VERSION.tar.gz"
PCRE_SOURCE_DIR="/usr/local/src/pcre"
PCRE_SOURCE_PATH="$PCRE_SOURCE_DIR/pcre-$PCRE_VERSION"

OPENSSL_DOWNLOAD_URL="https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz"
OPENSSL_PACKAGE_FILE="$INSTALL_FILE_DIR/openssl-$OPENSSL_VERSION.tar.gz"
OPENSSL_SOURCE_DIR="/usr/local/src/openssl"
OPENSSL_SOURCE_PATH="$OPENSSL_SOURCE_DIR/openssl-$OPENSSL_VERSION"

ZLIB_DOWNLOAD_URL="http://zlib.net/zlib-$ZLIB_VERSION.tar.gz"
ZLIB_PACKAGE_FILE="$INSTALL_FILE_DIR/zlib-$ZLIB_VERSION.tar.gz"
ZLIB_SOURCE_DIR="/usr/local/src/zlib"
ZLIB_SOURCE_PATH="$ZLIB_SOURCE_DIR/zlib-$ZLIB_VERSION"
  
LIB_CONF_DIR="/etc/ld.so.conf.d"
NGINX_SYMBOLIC_LINK_DIR="/usr/local/sbin"

echo "NGINX VERSION: $NGINX_VERSION"
echo "PCRE VERSION: $PCRE_VERSION"
echo "OPENSSL VERSION: $OPENSSL_VERSION"
echo "ZLIB VERSION: $ZLIB_VERSION"
echo "NGINX PACKAGE FILE:$NGINX_PACKAGE_FILE"
echo "PCRE PACKAGE FILE: $PCRE_PACKAGE_FILE"
echo "OPENSSL PACKAGE FILE: $OPENSSL_PACKAGE_FILE"
echo "ZLIB PACKAGE FILE: $ZLIB_PACKAGE_FILE"

if [ -r $NGINX_PACKAGE_FILE ]
then
    echo "Found Nginx Package File!"
else
    wget $NGINX_DOWNLOAD_URL
fi

if [ -r $PCRE_PACKAGE_FILE ]
then
    echo "Found PCRE Package File!"
else
    wget $PCRE_DOWNLOAD_URL
fi

if [ -r $OPENSSL_PACKAGE_FILE ]
then
    echo "Found OPENSSL Package File!"
else
    wget $OPENSSL_DOWNLOAD_URL
fi

if [ -r $ZLIB_PACKAGE_FILE ]
then
    echo "Found ZLIB Package File!"
else
    wget $ZLIB_DOWNLOAD_URL
fi

if [ -r $PCRE_SOURCE_PATH ];
then
 echo "PCRE already extracted"
else 
 if [ -r $PCRE_SOURCE_DIR ];
 then
  echo "PCRE source directory exists"
 else
  echo "PCRE source directory no exists. package will be extracted."
  mkdir $PCRE_SOURCE_DIR
 fi
 
 echo "PCRE source package will be extracted in $PCRE_SOURCE_PATH"
 tar -xvzf $PCRE_PACKAGE_FILE -C $PCRE_SOURCE_DIR > pcre_extract.log 2> pcre_extract.err
fi

if [ -r $OPENSSL_SOURCE_PATH ];
then
 echo "OPENSSL already extracted"
else 
 if [ -r $OPENSSL_SOURCE_DIR ];
 then
  echo "OPENSSL source directory exists"
 else
  echo "OPENSSL source directory no exists."
  mkdir $OPENSSL_SOURCE_DIR
 fi
 
 echo "OPENSSL source package will be extracted in $OPENSSL_SOURCE_PATH"
 tar -xvzf $OPENSSL_PACKAGE_FILE -C $OPENSSL_SOURCE_DIR > openssl_extract.log 2> openssl_extract.err
fi


if [ -r $ZLIB_SOURCE_PATH ];
then
 echo "ZLIB already extracted" 
else
 if [ -r $ZLIB_SOURCE_DIR ];
 then
  echo "ZLIB source directory exists"
 else
  echo "ZLIB source directory no exists. package will be extracted."
  mkdir $ZLIB_SOURCE_DIR
 fi
 
 echo "ZLIB source package will be extracted in $ZLIB_SOURCE_PATH"
 tar -xvzf $ZLIB_PACKAGE_FILE -C $ZLIB_SOURCE_DIR > openssl_extract.log 2> openssl_extract.err
fi


if [ -r $NGINX_SOURCE_PATH ];
then
    echo "Found nginx sources. skip extracting..."
else
    echo "Make nginx source directory at $NGINX_SOURCE_DIR"
    mkdir $NGINX_SOURCE_DIR
    tar -xvzf $NGINX_PACKAGE_FILE -C $NGINX_SOURCE_DIR > $INSTALL_FILE_DIR/nginx_extract.log 2> $INSTALL_FILE_DIR/nginx_extract.err
    echo "Extracted nginx sources successfully at $NGINX_SOURCE_PATH"
fi

echo "Installing nginx..."
cd $NGINX_SOURCE_PATH
echo "Configuring..."
./configure --prefix=/usr/local/nginx --builddir=./objs --sbin-path=/usr/local/sbin --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --with-pcre=$PCRE_SOURCE_PATH --with-zlib=$ZLIB_SOURCE_PATH --with-openssl=$OPENSSL_SOURCE_PATH --with-http_ssl_module --with-http_gunzip_module > $INSTALL_FILE_DIR/nginx_configure.log 2> $INSTALL_FILE_DIR/nginx_configure.err
  
yum install -y gcc gcc-c++

echo "Making..."
make > $INSTALL_FILE_DIR/nginx_make.log 2> $INSTALL_FILE_DIR/nginx_make.err
make install > $INSTALL_FILE_DIR/nginx_make_install.log 2> $INSTALL_FILE_DIR/nginx_make_install.err
  
echo "Make lib directory configuration file at $LIB_CONF_DIR"
echo "/usr/local/lib" > $LIB_CONF_DIR/nginx.conf
ldconfig
  
echo "Checking symbolic link at $NGINX_SYMBOLIC_LINK_DIR"

if [ -r $NGINX_SYMBOLIC_LINK_DIR ]
then
    echo "symbolic link already exists!"
else    
 echo "Making symbolic link..."
    ln -s /usr/local/nginx/sbin/nginx $NGINX_SYMBOLIC_LINK_DIR/nginx
fi
  
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx-original.conf
cp $INSTALL_FILE_DIR/nginx.conf /usr/local/nginx/conf/.
  
cd $NGINX_SYMBOLIC_LINK_DIR
./nginx
  
echo "All done! nginx has been started. enjoy it!"



nginx.conf


worker_processes  auto;

events {
    worker_connections  65000;
    multi_accept on;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;

    keepalive_timeout 0;

    upstream tomcat {
         server localhost:8080;
    }
 
 server {
  listen   80;
  server_name localhost;
  
  location / {
   proxy_redirect off;
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto $scheme;

   proxy_pass http://tomcat;
  }
 }
}