#!/bin/bash
#install nginx for centos
#author:21yunwei
#date:2015.12.16
downloaddir=/usr/local/src
function menu(){
echo "**************************************************"
cat < < EOF
Plese choose nginx version you want to install
1, nginx for 1.10.3
2, nginx for 1.12.3
default for 1.10.3
EOF
echo "**************************************************"
}
function checkroot() {
if [ $UID -ne 0 ]
then
echo "Please login as user root."
exit
fi
}
function create_user_nginx(){
gflag=`cat /etc/group |awk -F':' '{print $1}' | grep nginx`
[[ $gflag != "" ]] && echo "group 'nginx' already exists" || groupadd nginx
uflag=`cat /etc/passwd |awk -F':' '{print $1}' | grep nginx`
[[ $uflag != "" ]] && echo "user 'nginx' already exists" ||useradd -r nginx -g nginx -s /sbin/nologin
}
function install_libs() {
yum -y install gcc gcc-c++ automake autoconf libtool make >/dev/null && echo 'install gcc automake autoconf libtool make OK' || echo "install gcc automake autoconf libtool make install error"
yum -y install pcre* >/dev/null && echo "pcre install OK" || echo "pcre install failed ,please check"
yum -y install zlib* >/dev/null && echo "zlib install ok" || echo " zlib install failed ,please check"
yum -y install openssl openssl-devel >/dev/null && echo "openssl install ok" || echo " openssl install failed ,please check"
[ $? -eq 0 ] && echo "libs install ok,go next step." || echo "libs install failed ,please check"
}
function select_nginx() {
read -p "Plese choose nginx version you want to install:" ver
}
function download_install_nginx {
#read -p "Plese choose nginx version you want to install:" ver &&
cd $downloaddir
case $ver in
"1") echo "downloading nginx 1.10.3 " && wget -c http://nginx.org/download/nginx-1.10.3.tar.gz
;;
"2") echo "downloading nginx 1.12.0 " && wget -c http://nginx.org/download/nginx-1.12.0.tar.gz
;;
*) echo "downloading nginx 1.10.3 " && wget -c http://nginx.org/download/nginx-1.10.3.tar.gz
;;
esac
cpunum=`cat /proc/cpuinfo |grep -c processor`
echo "Nginx is installing by shell,please wait:"
case $ver in
1)tar zxf nginx-1.10.3.tar.gz &&cd ${downloaddir}/nginx-1.10.3 &&./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module >/dev/null && make -j$cpunum >/dev/null && make install >/dev/null
[ $? -eq 0 ] && echo "nginx install compleate,go next step." || echo "nginx install failed ,please check"
;;
2)tar zxf nginx-1.12.0.tar.gz && cd ${downloaddir}/nginx-1.12.0 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module >/dev/null && make -j$cpunum>/dev/null && make install >/dev/null
[ $? -eq 0 ] && echo "nginx install compleate,go next step." || echo "nginx install failed ,please check"
;;
*)tar zxf nginx-1.10.3.tar.gz && cd ${downloaddir}/nginx-1.10.3 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module >/dev/null && make -j$cpunum>/dev/null && make install >/dev/null
[ $? -eq 0 ] && echo "nginx install compleate,go next step." || echo "nginx install failed ,please check"
;;
esac
}
function service_nginx() {
echo "nginx service is downloading by shell,please wait:"
wget -O /etc/init.d/nginx -c https://download.21yunwei.com/service/nginx-init && chmod +x /etc/init.d/nginx && chkconfig nginx on && /etc/init.d/nginx start
netstat -tunlp | grep 80
[ $? -eq 0 ] && echo "nginx install OK" || echo "nginx install failed,please check"
}
function main(){
menu
checkroot
select_nginx
create_user_nginx
install_libs
download_install_nginx
service_nginx
}
main
该脚本是练手自己写着玩的,很多地方需要调整,但安装没啥问题,仅供参考。手工安装请参考linux下如何安装nginx环境配置