解决Nginx启动脚本在redhat上不兼容问题
我们在网上看到的Nginx的启动脚本通常是/etc/rc.d/init.d/nginx这个脚本,其内容是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.1.3.0 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd= /usr/local/nginx/sbin/nginx
nginx_config= /usr/local/nginx/conf/nginx .conf
nginx_pid= /usr/local/nginx/logs/nginx .pid
RETVAL=0 prog= "nginx"
# Source function library. . /etc/rc .d /init .d /functions
# Source networking configuration. . /etc/sysconfig/network
# Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions. start() { if [ -e $nginx_pid ]; then
echo "nginx already running...."
exit 1
fi echo -n $ "Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
} # Stop nginx daemons functions. stop() { echo -n $ "Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx .pid
} reload() { echo -n $ "Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
} # See how we were called. case "$1" in
start) start
;;
stop) stop
;;
reload) reload
;;
restart) stop
start
;;
status) status $prog
RETVAL=$?
;;
*) echo $ "Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac exit $RETVAL
|
对于这个脚本,我之前在一些Linux服务器(CentOS ?)上试过是好使的,但是在redhat上安装Nginx时,突然发现这个脚本并不能启动Nginx了
最后通过查阅资料问题得以解决,解决方案如下:
(1)备份脚本:
1
|
[[email protected] init.d] # mv nginx nginx.bak
|
(2)新建nginx.service:
1
|
[[email protected] init.d] # vim /usr/lib/systemd/system/nginx.service
|
其内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[Unit] Description=nginx - high performance web server Documentation=http: //nginx .org /en/docs/
After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile= /usr/local/nginx/logs/nginx .pid
ExecStartPre= /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx .conf
ExecStart= /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx .conf
ExecReload= /bin/kill -s HUP $MAINPID
ExecStop= /bin/kill -s QUIT $MAINPID
PrivateTmp= true
[Install] WantedBy=multi-user.target |
(3)给这个脚本赋权:
1
|
[[email protected] init.d] # chmod a+x /usr/lib/systemd/system/nginx.service
|
(4)关闭Nginx及其他:
1
2
3
|
[[email protected] init.d] # fuser -k 80/tcp
[[email protected] init.d] # systemctl daemon-reload
[[email protected] init.d] # service nginx stop
|
(5)启动Nginx:
1
|
[[email protected] system] # service nginx start
|
(6)查看Nginx的状态:
1
|
[[email protected] system] # service nginx status
|
最后效果如下:
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1835813,如需转载请自行联系原作者