1. 程式人生 > >linux nginx 開機重啟

linux nginx 開機重啟

function case code color res bin div ech exit

1.首先,在linux系統的/etc/init.d/目錄下創建nginx文件

vim /etc/init.d/nginx

2.加入腳本

技術分享圖片
 1 #!/bin/bash
 2 # nginx Startup script for the Nginx HTTP Server
 3 # it is v.0.0.2 version.
 4 # chkconfig: - 85 15
 5 # description: Nginx is a high-performance web and proxy server.
 6 #              It has a lot of features, but it‘s not for everyone.
7 # processname: nginx 8 # pidfile: /var/run/nginx.pid 9 # config: /usr/local/nginx/conf/nginx.conf 10 nginxd=/usr/local/nginx/sbin/nginx 11 nginx_config=/usr/local/nginx/conf/nginx.conf 12 nginx_pid=/var/run/nginx.pid 13 RETVAL=0 14 prog="nginx" 15 # Source function library. 16 . /etc/rc.d/init.d/functions 17 # Source networking configuration.
18 . /etc/sysconfig/network 19 # Check that networking is up. 20 [ ${NETWORKING} = "no" ] && exit 0 21 [ -x $nginxd ] || exit 0 22 # Start nginx daemons functions. 23 start() { 24 if [ -e $nginx_pid ];then 25 echo "nginx already running...." 26 exit 1 27 fi 28 echo -n $"Starting $prog: " 29
daemon $nginxd -c ${nginx_config} 30 RETVAL=$? 31 echo 32 [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx 33 return $RETVAL 34 } 35 # Stop nginx daemons functions. 36 stop() { 37 echo -n $"Stopping $prog: " 38 killproc $nginxd 39 RETVAL=$? 40 echo 41 [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid 42 } 43 # reload nginx service functions. 44 reload() { 45 echo -n $"Reloading $prog: " 46 #kill -HUP `cat ${nginx_pid}` 47 killproc $nginxd -HUP 48 RETVAL=$? 49 echo 50 } 51 # See how we were called. 52 case "$1" in 53 start) 54 start 55 ;; 56 stop) 57 stop 58 ;; 59 reload) 60 reload 61 ;; 62 restart) 63 stop 64 start 65 ;; 66 status) 67 status $prog 68 RETVAL=$? 69 ;; 70 *) 71 echo $"Usage: $prog {start|stop|restart|reload|status|help}" 72 exit 1 73 esac 74 exit $RETVAL
View Code

或者腳本:

技術分享圖片
 1 #!/bin/bash
 2 # nginx Startup script for the Nginx HTTP Server
 3 # it is v.0.0.2 version.
 4 # chkconfig: - 85 15
 5 # description: Nginx is a high-performance web and proxy server.
 6 #              It has a lot of features, but it‘s not for everyone.
 7 # processname: nginx
 8 # pidfile: /var/run/nginx.pid
 9 # config: /usr/local/nginx/conf/nginx.conf
10 nginxd=/usr/local/nginx/sbin/nginx
11 nginx_config=/usr/local/nginx/conf/nginx.conf
12 nginx_pid=/var/run/nginx.pid
13 RETVAL=0
14 prog="nginx"
15 # Source function library.
16 . /etc/rc.d/init.d/functions
17 # Source networking configuration.
18 . /etc/sysconfig/network
19 # Check that networking is up.
20 [ ${NETWORKING} = "no" ] && exit 0
21 [ -x $nginxd ] || exit 0
22 # Start nginx daemons functions.
23 start() {
24 if [ -e $nginx_pid ];then
25    echo "nginx already running...."
26    exit 1
27 fi
28    echo -n $"Starting $prog: "
29    daemon $nginxd -c ${nginx_config}
30    RETVAL=$?
31    echo
32    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
33    return $RETVAL
34 }
35 # Stop nginx daemons functions.
36 stop() {
37         echo -n $"Stopping $prog: "
38         killproc $nginxd
39         RETVAL=$?
40         echo
41         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
42 }
43 # reload nginx service functions.
44 reload() {
45     echo -n $"Reloading $prog: "
46     #kill -HUP `cat ${nginx_pid}`
47     killproc $nginxd -HUP
48     RETVAL=$?
49     echo
50 }
51 # See how we were called.
52 case "$1" in
53 start)
54         start
55         ;;
56 stop)
57         stop
58         ;;
59 reload)
60         reload
61         ;;
62 restart)
63         stop
64         start
65         ;;
66 status)
67         status $prog
68         RETVAL=$?
69         ;;
70 *)
71         echo $"Usage: $prog {start|stop|restart|reload|status|help}"
72         exit 1
73 esac
View Code

源碼來源於:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

註意其中

nginx=”/usr/sbin/nginx” 修改成nginx執行程序的路徑。

NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路徑。

3.設置文件權限

chmod a+x /etc/init.d/nginx

4.使用chkconfig進行管理

先將nginx服務加入chkconfig管理列表:

chkconfig --add /etc/init.d/nginx

加完這個之後,就可以使用service對nginx進行啟動,重啟等操作了。

service nginx start
service nginx stop

5.設置終端模式開機啟動:

chkconfig nginx on

其他參考:

https://blog.csdn.net/u013870094/article/details/52463026

https://www.cnblogs.com/jimisun/p/8057156.html

linux nginx 開機重啟