Skip to content

Instantly share code, notes, and snippets.

@pihug12
Created June 4, 2014 16:56
Show Gist options
  • Save pihug12/b97a7a7a274a505bc812 to your computer and use it in GitHub Desktop.
Save pihug12/b97a7a7a274a505bc812 to your computer and use it in GitHub Desktop.
nginxautoinstall-rasp.sh
#!/bin/bash
#
# My own script to install/upgrade NGinx+PHP5_FPM+MemCached from sources
#
# Nicolargo - 06/2014
# LGPL
#
# Syntaxe: # su - -c "./nginxautoinstall.sh"
# Syntaxe: or # sudo ./nginxautoinstall.sh
#
# Adaptation pour "Raspbian Wheezy 2014-01-07" par ~pl
#
VERSION="1.160.01-rasp"
##############################
# NGinx version to install
# Use LEGACY, STABLE or DEV
# - LEGACY or STABLE for a production server
# - DEV for testing only
VERSION_TO_INSTALL="STABLE"
##############################
# !!!! Do not change the code bellow
# Current NGinx version
NGINX_LEGACY_VERSION="1.4.7"
NGINX_STABLE_VERSION="1.6.0"
NGINX_DEV_VERSION="1.7.1"
# Functions
#-----------------------------------------------------------------------------
displaymessage() {
echo "$*"
}
displaytitle() {
displaymessage "------------------------------------------------------------------------------"
displaymessage "$*"
displaymessage "------------------------------------------------------------------------------"
}
displayerror() {
displaymessage "$*" >&2
}
# First parameter: ERROR CODE
# Second parameter: MESSAGE
displayerrorandexit() {
local exitcode=$1
shift
displayerror "$*"
exit $exitcode
}
# First parameter: MESSAGE
# Others parameters: COMMAND (! not |)
displayandexec() {
local message=$1
echo -n "[En cours] $message"
shift
echo ">>> $*" >> $LOG_FILE 2>&1
sh -c "$*" >> $LOG_FILE 2>&1
local ret=$?
if [ $ret -ne 0 ]; then
echo -e "\r\e[0;31m [ERROR]\e[0m $message"
else
echo -e "\r\e[0;32m [OK]\e[0m $message"
fi
return $ret
}
########################
# Configuration de NGinx
NGINX_DEPS=""
NGINX_OPTIONS="--conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log"
NGINX_MODULES="--with-http_dav_module --http-client-body-temp-path=/var/lib/nginx/body --with-http_ssl_module --http-proxy-temp-path=/var/lib/nginx/proxy --with-http_stub_status_module --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_flv_module --with-http_realip_module --with-http_mp4_module"
if [[ $VERSION_TO_INSTALL == "LEGACY" ]]; then
# The LEGACY version
NGINX_VERSION=$NGINX_LEGACY_VERSION
NGINX_DEPS=$NGINX_DEPS" php5-apc"
elif [[ $VERSION_TO_INSTALL == "STABLE" ]]; then
# The STABLE version
NGINX_VERSION=$NGINX_STABLE_VERSION
NGINX_DEPS=$NGINX_DEPS" openssl php-apc"
NGINX_MODULES=$NGINX_MODULES" --with-http_ssl_module --with-http_spdy_module"
elif [[ $VERSION_TO_INSTALL == "DEV" ]]; then
# The DEV version
NGINX_VERSION=$NGINX_DEV_VERSION
NGINX_DEPS=$NGINX_DEPS" openssl php-apc"
NGINX_MODULES=$NGINX_MODULES" --with-http_ssl_module --with-http_spdy_module"
else
displayerrorandexit 1 "Error: VERSION_TO_INSTALL should be set to LEGACY, STABLE or DEV... Exit..."
fi
displaytitle "Installation of NGinx $NGINX_VERSION ($VERSION_TO_INSTALL)"
if [[ $NGINX_DEPS != "" ]]; then
displaymessage "Packages needed: $NGINX_DEPS"
fi
displaymessage "Options: $NGINX_OPTIONS"
displaymessage "Modules: $NGINX_MODULES"
##############################
# Variables globales
#-------------------
APT_GET="apt-get -q -y --force-yes"
WGET="wget --no-check-certificate"
UNZIP="unzip"
DATE=`date +"%Y%m%d%H%M%S"`
LOG_FILE="/tmp/nginxautoinstall-$DATE.log"
# Debut de l'installation
#-----------------------------------------------------------------------------
# Test que le script est lance en root
if [ $EUID -ne 0 ]; then
displayerrorandexit 1 "Error: Script should be ran as root..." 1>&2
fi
displaytitle "Install prerequisites"
# MaJ des depots
displayandexec "Update the repositories list" $APT_GET update
# Pre-requis
displayandexec "Install development tools" $APT_GET install build-essential libpcre3-dev libssl-dev zlib1g-dev php5-dev unzip
displayandexec "Install PHP-FPM5" $APT_GET install php5-cli php5-common php5-sqlite php5-fpm php-pear php5-gd php5-curl
displayandexec "Install MemCached" $APT_GET install libcache-memcached-perl php5-memcache memcached
if [[ $NGINX_DEPS != "" ]]; then
displayandexec "Install NGinx dependencies" $APT_GET install $NGINX_DEPS
fi
# php5-suhosin no longer available in Wheezy
displayandexec "Remove php5-suhosin" dpkg --purge php5-suhosin
displaytitle "Install NGinx version $NGINX_VERSION"
# Telechargement des fichiers
displayandexec "Download NGinx version $NGINX_VERSION" $WGET http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
# Extract
displayandexec "Uncompress NGinx version $NGINX_VERSION" tar zxvf nginx-$NGINX_VERSION.tar.gz
# Configure
cd nginx-$NGINX_VERSION
displayandexec "Configure NGinx version $NGINX_VERSION" ./configure $NGINX_OPTIONS $NGINX_MODULES
# Compile
displayandexec "Compile NGinx version $NGINX_VERSION" make
# Install or Upgrade
TAGINSTALL=0
if [ -x /usr/local/nginx/sbin/nginx ]
then
# Upgrade
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.$DATE
displayandexec "Upgrade NGinx to version $NGINX_VERSION" make install
else
# Install
displayandexec "Install NGinx version $NGINX_VERSION" make install
TAGINSTALL=1
fi
# Post installation
if [ $TAGINSTALL == 1 ]
then
displayandexec "Post installation script for NGinx version $NGINX_VERSION" "cd .. ; mkdir /var/lib/nginx ; mkdir /etc/nginx/conf.d ; mkdir /etc/nginx/sites-enabled ; mkdir /var/www ; chown -R www-data:www-data /var/www"
fi
# Download the default configuration file
# Nginx + default site
if [ $TAGINSTALL == 1 ]
then
displayandexec "Init the default configuration file for NGinx" "$WGET https://raw.github.com/nicolargo/debianpostinstall/master/nginx.conf ; $WGET https://raw.github.com/nicolargo/debianpostinstall/master/default-site ; mv nginx.conf /etc/nginx/ ; mv default-site /etc/nginx/sites-enabled/"
FASTCGI_PASS_AVANT="127.0.0.1:9000"
FASTCGI_PASS_APRES="unix:/var/run/php5-fpm.sock"
displayandexec "Patch default-site" "sed -i s,$FASTCGI_PASS_AVANT,$FASTCGI_PASS_APRES,g /etc/nginx/sites-enabled/default-site"
fi
# Download the init script
displayandexec "Install the NGinx init script" "$WGET https://raw.github.com/nicolargo/debianpostinstall/master/nginx ; mv nginx /etc/init.d/ ; chmod 755 /etc/init.d/nginx ; /usr/sbin/update-rc.d -f nginx defaults"
# Log file rotate
cat > /etc/logrotate.d/nginx <<EOF
/var/log/nginx/*_log {
missingok
notifempty
sharedscripts
postrotate
/bin/kill -USR1 \`cat /var/run/nginx.pid 2>/dev/null\` 2>/dev/null || true
endscript
}
EOF
displaytitle "Start processes"
# Start PHP5-FPM and NGinx
if [ $TAGINSTALL == 1 ]
then
displayandexec "Start PHP" /etc/init.d/php5-fpm start
displayandexec "Start NGinx" /etc/init.d/nginx start
else
displayandexec "Restart PHP" /etc/init.d/php5-fpm restart
displayandexec "Restart NGinx" "killall nginx ; /etc/init.d/nginx start"
fi
# Summary
echo ""
echo "------------------------------------------------------------------------------"
echo " NGinx + PHP5-FPM installation finished"
echo "------------------------------------------------------------------------------"
echo "NGinx configuration folder: /etc/nginx"
echo "NGinx default site configuration: /etc/nginx/sites-enabled/default-site"
echo "NGinx default HTML root: /var/www"
echo ""
echo "Installation script log file: $LOG_FILE"
echo ""
echo "Notes: If you use IpTables add the following rules"
echo "iptables -A INPUT -i lo -s localhost -d localhost -j ACCEPT"
echo "iptables -A OUTPUT -o lo -s localhost -d localhost -j ACCEPT"
echo "iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT"
echo "iptables -A INPUT -p tcp --dport http -j ACCEPT"
echo ""
echo "------------------------------------------------------------------------------"
echo ""
# Fin du script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment