Last active
August 29, 2015 14:03
-
-
Save ChrisMcKee/15d5832647473072b08f to your computer and use it in GitHub Desktop.
Install NGINX on CENTOS from source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### VARIABLES ### | |
PRE_PACK="gcc gcc-c++ make pcre-devel zlib-devel unzip wget htop" | |
OPT_PACK="openssl-devel" | |
VER="1.6.2" | |
USER="nginx" | |
GROUP="nginx" | |
INSTALL_DIR="/etc/nginx" | |
PROG_NAME="nginx" | |
DT=`date +"%d%m%y-%H%M%S"` | |
HN=$(uname -n) | |
# Pre-Checks to prevent screw ups | |
DIR_TMP='/svr-setup' | |
SCRIPT_DIR=$(readlink -f $(dirname ${BASH_SOURCE[0]})) | |
if [ ! -d "$DIR_TMP" ]; then | |
mkdir -p $DIR_TMP | |
fi | |
# Setup Colours | |
black='\E[30;40m' | |
red='\E[31;40m' | |
green='\E[32;40m' | |
yellow='\E[33;40m' | |
blue='\E[34;40m' | |
magenta='\E[35;40m' | |
cyan='\E[36;40m' | |
white='\E[37;40m' | |
boldblack='\E[1;30;40m' | |
boldred='\E[1;31;40m' | |
boldgreen='\E[1;32;40m' | |
boldyellow='\E[1;33;40m' | |
boldblue='\E[1;34;40m' | |
boldmagenta='\E[1;35;40m' | |
boldcyan='\E[1;36;40m' | |
boldwhite='\E[1;37;40m' | |
Reset="tput sgr0" # Reset text attributes to normal | |
#+ without clearing screen. | |
cecho () # Coloured-echo. | |
# Argument $1 = message | |
# Argument $2 = color | |
{ | |
message=$1 | |
color=$2 | |
echo -e "$color$message" ; $Reset | |
return | |
} | |
clear | |
if [[ -z $(cat /etc/resolv.conf) ]]; then | |
echo "" | |
echo "/etc/resolv.conf is empty. No nameserver resolvers detected !! " | |
echo "Please configure your /etc/resolv.conf correctly or you will not" | |
echo "be able to use the internet or download from your server." | |
echo "aborting script." | |
echo "" | |
exit | |
fi | |
if [ -f /proc/user_beancounters ]; then | |
CPUS='1' | |
MAKETHREADS=" -j$CPUS" | |
else | |
# speed up make | |
CPUS=`cat "/proc/cpuinfo" | grep "processor"|wc -l` | |
MAKETHREADS=" -j$CPUS" | |
fi | |
if [ -f /proc/user_beancounters ]; | |
then | |
cecho "OpenVZ system detected, NTP not installed" $boldgreen | |
else | |
echo "*************************************************" | |
cecho "* Installing NTP (and syncing time)" $boldgreen | |
echo "*************************************************" | |
yum -y install ntp | |
chkconfig --levels 235 ntpd on | |
ntpdate pool.ntp.org | |
echo "The date/time is now:" | |
date | |
echo "If this is correct, then everything is working properly" | |
echo "*************************************************" | |
cecho "* NTP installed" $boldgreen | |
echo "*************************************************" | |
fi | |
##### DO NOT EDIT BELOW THIS LINE ##### | |
CWD=`pwd` | |
cecho "Installing/upgrading your web server..." $boldgreen | |
# install the prerequisites | |
yum -y -q install $PRE_PACK $OPT_PACK > /dev/null | |
cd $DIR_TMP | |
cecho "Getting NGINX" $boldgreen | |
# optional configuration directives | |
# http://wiki.nginx.org/Modules | |
CONFIGURE_OPTIONS="--user=$USER --group=$GROUP | |
--prefix=/etc/nginx | |
--sbin-path=/usr/sbin/nginx | |
--conf-path=/etc/nginx/nginx.conf | |
--error-log-path=/var/log/nginx/error.log | |
--http-log-path=/var/log/nginx/access.log | |
--pid-path=/var/run/nginx.pid | |
--lock-path=/var/run/nginx.lock | |
--http-client-body-temp-path=/var/lib/nginx/body | |
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi | |
--http-proxy-temp-path=/var/lib/nginx/proxy | |
--with-http_ssl_module | |
--with-http_spdy_module | |
--with-http_realip_module | |
--with-http_gzip_static_module | |
--with-http_stub_status_module | |
--with-file-aio | |
--with-ipv6 | |
--without-http_autoindex_module | |
--without-http_auth_basic_module | |
--without-http_ssi_module | |
--without-http_empty_gif_module | |
--without-http_scgi_module | |
--without-http_uwsgi_module" | |
mkdir -p /var/lib/nginx/body /var/lib/nginx/fastcgi /var/lib/nginx/proxy | |
# download and install | |
wget -q http://nginx.org/download/nginx-$VER.tar.gz | |
tar xzf nginx-$VER.tar.gz && rm -f ng*.tar.gz; | |
cd nginx-$VER | |
cecho "Configuring and Making NGINX" $boldgreen | |
./configure $CONFIGURE_OPTIONS | |
cecho "Making NGINX" $green | |
make | |
cecho "Installing NGINX" $green | |
make install | |
cecho "Adding NGINX User" $boldyellow | |
id -u nginx &>/dev/null || useradd -r nginx | |
if [ -e "/etc/init.d/nginx" ] | |
then | |
cecho "Adding Daemon" $boldgreen | |
wget -qO /etc/init.d/nginx https://gist.github.com/sairam/5892520/raw/b8195a71e944d46271c8a49f2717f70bcd04bf1a/etc-init.d-nginx | |
chmod +x /etc/init.d/nginx | |
fi | |
cecho "chkconfig run" $boldyellow | |
chkconfig --add nginx | |
chkconfig --level 345 nginx on | |
# If upgrading lets make sure nginx is dead before we start, if you have a file lock this may need to be at the top | |
service nginx stop | |
pkill nginx | |
service nginx start | |
# Deal with log rotate | |
if [ -e "/etc/logrotate.d/nginx" ] | |
then | |
sudo cat <<EOT >> /etc/logrotate.d/nginx | |
/var/log/nginx/*.log { | |
daily | |
missingok | |
rotate 52 | |
compress | |
delaycompress | |
notifempty | |
sharedscripts | |
postrotate | |
/usr/sbin/nginx -s reopen | |
endscript | |
} | |
EOT | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment