解决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
[root@prx03 init.d] # mv nginx nginx.bak

(2)新建nginx.service:

1
[root@prx03 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
[root@prx03 init.d] # chmod a+x /usr/lib/systemd/system/nginx.service

(4)关闭Nginx及其他:

1
2
3
[root@prx03 init.d] # fuser -k 80/tcp
[root@prx03 init.d] # systemctl daemon-reload
[root@prx03 init.d] # service nginx stop

(5)启动Nginx:

1
[root@prx03 system] # service nginx start

(6)查看Nginx的状态:

1
[root@prx03 system] # service nginx status

最后效果如下:

wKiom1eohcaDZtCxAAChkV4QaZ4248.png




本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1835813,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值