centos 一鍵安裝配置nginx腳本

centos 一鍵安裝配置nginx腳本

installNginx.ssh

用vi或則vim編輯 installNginx.ssh

#!/bin/bash
# author:kwin
# Email:[email protected]

src="/usr/local/src/"
cd $src

#找到指定進程,並殺死
#findPortKill 80
findPortKill (){
  processe=`lsof -i:${1} -n|awk '{print $2}'|grep '^[1-9]'`
  for i in $processe
    do
#  echo "Kill the $1 process [ $i ]"
  kill -9 $i
    done
}

#將命令所在目錄添加到系統參數PATH中,方便調用
addToPATH(){

bin=${1}

echo $PATH|grep ${bin} >/dev/null
if [ $? -ne 0 ]; then

echo "export PATH=\$PATH:${bin}">>/etc/profile
fi
}



nginxConf(){

cp /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.conf.bak
sed -i '35,79c  include  ../site/*.conf;' /usr/local/nginx/conf/nginx.conf
mkdir /usr/local/nginx/site

cat>/usr/local/nginx/site/default.conf<<EOF
    server {
        listen       80;
        server_name  localhost;

        gzip on; #開啓gizip
        gzip_buffers 32 4K;#壓縮在內存中緩衝32塊? 每塊4K
        gzip_comp_level 6 ;#壓縮級別 推薦6
        gzip_min_length 4000;#開始壓縮的最小長度4bit
        gzip_types text/css text/xml apploation/x-javascript;#只對CSS、XML、HTML、JS文件進行壓縮

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        root   /var/www;

        location / {
          #   try_files \$uri \$uri/ /index.php?\$query_string;
            index  index.html index.htm index.php;

        #如果是jpg、jpeg、gif、png、js、css則緩存一天
        if (\$fastcgi_script_name ~* \.[jpg|jpeg|gif|png|js|css] ) {
            expires 1d;
            }
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php\$ { 

            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index index.php; 
            fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
            include        fastcgi_params; 

        } 

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php\$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    } 
EOF
}


addNginxService(){
cat>/etc/init.d/nginx<<EOF
#!/bin/sh  
#  
# nginx - this script starts and stops the nginx daemin  
#  
# chkconfig:   - 85 15  
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \  
#               proxy and IMAP/POP3 proxy server  
# processname: nginx  
# config:      /usr/local/nginx/conf/nginx.conf  
# pidfile:     /usr/local/nginx/logs/nginx.pid  

# Source function library.  
. /etc/rc.d/init.d/functions  

# Source networking configuration.  
. /etc/sysconfig/network  

# Check that networking is up.  
[ "$NETWORKING" = "no" ] && exit 0  

nginx="/usr/local/nginx/sbin/nginx"  
prog="Nginx"  

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"  

lockfile=/var/lock/subsys/nginx  

start() {  
    [ -x $nginx ] || exit 5  
    [ -f $NGINX_CONF_FILE ] || exit 6  
    echo -n $"Starting $prog: "  
    daemon $nginx -c $NGINX_CONF_FILE  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && touch $lockfile  
    return $retval  
}  

stop() {  
    echo -n $"Stopping $prog: "  
    killproc $prog -QUIT  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && rm -f $lockfile  
    return $retval  
}  

restart() {  
    configtest || return $?  
    stop  
    start  
}  

reload() {  
    configtest || return $?  
    echo -n $"Reloading $prog: "  
    killproc $nginx -HUP  
    RETVAL=$?  
    echo  
}  

force_reload() {  
    restart  
}  

configtest() {  
  $nginx -t -c $NGINX_CONF_FILE  
}  

rh_status() {  
    status $prog  
}  

rh_status_q() {  
    rh_status >/dev/null 2>&1  
}  

case "$1" in  
    start)  
        rh_status_q && exit 0  
        $1  
        ;;  
    stop)  
        rh_status_q || exit 0  
        $1  
        ;;  
    restart|configtest)  
        $1  
        ;;  
    reload)  
        rh_status_q || exit 7  
        $1  
        ;;  
    force-reload)  
        force_reload  
        ;;  
    status)  
        rh_status  
        ;;  
    condrestart|try-restart)  
        rh_status_q || exit 0  
            ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"  
        exit 2  
esac  
EOF

 chmod 755 /etc/init.d/nginx 
 chkconfig --add nginx
 chkconfig --level 2345 nginx on
}

#安裝nginx
installNginx(){

yum -y  install pcre-devel openssl openssl-devel gcc gcc-c++ ncurses-devel perlddd

fileName="nginx-1.9.9"
package="${fileName}.tar.gz"
installDir="/usr/local/nginx"

if test ! -f ${package}
then
wget http://nginx.org/download/${package}
fi


tar zxvf $package

cd $fileName
./configure --prefix=${installDir}
make && make install
echo "安裝完成"


nginxConf


#如果出現錯誤 找到80佔用的進程並殺死,再重啓
#如果還有問題 請自行調試配置文件
/usr/local/nginx/sbin/nginx 1> /dev/null 2>&1
if [ $? -ne 0 ]; then
findPortKill 80
/usr/local/nginx/sbin/nginx
fi

#sleep : 默認以秒爲單位。
#usleep : 默認以微秒爲單位。
#1s = 1000ms = 1000000us

usleep 100000

pid=`cat /usr/local/nginx/logs/nginx.pid`

echo "nginx 已經啓動,進程號爲${pid}"

bin="${installDir}/sbin"

#將命令所在目錄添加到系統參數PATH中,方便調用
addToPATH ${bin}


#設置開機啓動
addNginxService
}

installNginx

賦予installNginx.ssh文件可執行限權

# chmod +x installNginx.ssh

執行安裝

./installNginx.ssh

基本命令

service nginx [start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest]

虛擬主機管理

在安裝目錄(//usr/local/nginx/)裏的site目錄中有一個default.conf默認虛擬主機管理模版,後續拓展虛擬主機可以直接複製default.conf文件,文件名格式爲 *.conf,只要後綴爲.conf就能解析

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章