一、一鍵安裝nginx
前提:yum源需要配置完成、wget命令能夠正常使用、設備能夠上外網
腳本如下:
#!/bin/bash #Author: Jiangfeng #Created Time: 2019/08/03 #nginx service manage script #定義變量 softname=nginx-1.17.2.tar.gz soft=nginx-1.17.2 #網絡下載Nginx cd /usr/local/src wget http://nginx.org/download/nginx-1.17.2.tar.gz tar xf $softname if [ $? != 0 ];then echo "軟件包下載失敗,請查看wget是否安裝" exit 1 fi #解決依賴 cd $soft yum install -y pcre-devel zlib-devel openssl-devel gcc if [ $? != 0 ];then echo "無法安裝依賴包,請檢查yum" exit 2 fi #配置 ./configure --prefix=/usr/local/nginx --with-http_ssl_module if [ $? != 0 ];then echo "$soft 配置失敗" exit 3 fi #編譯安裝 make && make install if [ $? != 0 ];then echo "編譯失敗" exit 4 cd /usr/local/src rm -f $softname rm -rf $soft
二、shell腳本來實現nginx的啟動|關閉|重啟|重新加載配置文件(reload)|查看狀態
前提:nginx安裝完成,並且在/usr/local目錄下
腳本名字叫做”nginx”,放在/etc/init.d/目錄下
使用方法:
/etc/init.d/nginx start|stop|restart|reload|status
腳本如下:
#!/bin/bash #Author: Jiangfeng #Created Time: 2019/08/03 #nginx service manage script #variable ##nginx安裝路徑 nginx_path=/usr/local/nginx ##nginx腳本啟動路徑 nginxd=$nginx_path/sbin/nginx ##nginx服務啟動后存放PID的文件 nginx_pid_file=$nginx_path/logs/nginx.pid #調用shell的函數庫 if [ -f /etc/init.d/functions ];then . /etc/init.d/functions else echo "not find file:/etc/init.d/functions" eixt 1 fi #對nginx PID腳本文件進行判斷 if [ -f $nginx_pid_file ];then pid=`cat $nginx_pid_file` nginx_process_num=`ps -ef | grep $pid | grep -v "grep" | wc -l` fi #函數部分 ##Nginx的啟動函數 start () { ##如果nginx啟動則報錯 if [ -f $nginx_pid_file ] && [ $nginx_process_num -ne 0 ];then echo "Nginx服務已經啟動" else ##如果pid文件存在,但是沒有進程,說明上一次非法關閉了nginx,造成pid文件沒有自動刪除,所以啟動nginx之前先刪除舊的pid文件 if [ -f $nginx_pid_file ] && [ $nginx_process_num -eq 0 ];then rm -f $nginx_pid_file action "nginx start" $nginxd fi action "nginx start" $nginxd fi } ##Nginx關閉函數 stop () { ##如果Nginx服務沒有啟動,則提示服務沒有啟動 if [ -f $nginx_pid_file ] && [ $nginx_process_num -eq 0 ];then echo "Nginx服務沒有啟動" exit 2 else action "nginx stop" killall -s QUIT nginx rm -f $nginx_pid_file fi } ##Nginx重啟函數 restart () { stop sleep 1 start if [ $? -eq 0 ];then action "nginx 重啟完成" fi } ##重新讀取配置文件,不會更改pid reload () { if [ -f $nginx_pid_file ] && [ $nginx_process_num -ne 0 ];then action "nginx reload" killall -s HUP nginx else echo "Nginx沒有啟動" fi } ##查看Nginx啟動狀態 status () { tmp=`mktemp nginx.XXXX` curl -s -I 127.0.0.1 1> $tmp #curl -I 127.0.0.1 > $tmp &>/dev/null sed -i "s/\r//" $tmp val=`grep "HTTP" $tmp | cut -d ' ' -f3` if [ "$val" == "OK" ];then echo "Nginx start" else echo "Nginx stop" fi rm -f $tmp } #main case $1 in start) start;; stop) stop;; restart) restart;; reload) reload;; status) status;; *) echo "USAGE: $0 start|stop|restart|reload|status";; esac