我们在网上看到的Nginx的启动脚本通常是/etc/rc.d/init.d/nginx这个脚本,其内容是:
#!/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.confnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/usr/local/nginx/logs/nginx.pidRETVAL=0prog="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 1fi 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" instart) start ;;stop) stop ;;reload) reload ;;restart) stop start ;;status) status $prog RETVAL=$? ;;*) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1esacexit $RETVAL
对于这个脚本,我之前在一些Linux服务器(CentOS ?)上试过是好使的,但是在redhat上安装Nginx时,突然发现这个脚本并不能启动Nginx了
最后通过查阅资料问题得以解决,解决方案如下:
(1)备份脚本:
[root@prx03 init.d]# mv nginx nginx.bak
(2)新建nginx.service:
[root@prx03 init.d]# vim /usr/lib/systemd/system/nginx.service
其内容如下:
[Unit]Description=nginx - high performance web serverDocumentation=http://nginx.org/en/docs/After=network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.confExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target
(3)给这个脚本赋权:
[root@prx03 init.d]# chmod a+x /usr/lib/systemd/system/nginx.service
(4)关闭Nginx及其他:
[root@prx03 init.d]# fuser -k 80/tcp[root@prx03 init.d]# systemctl daemon-reload[root@prx03 init.d]# service nginx stop
(5)启动Nginx:
[root@prx03 system]# service nginx start
(6)查看Nginx的状态:
[root@prx03 system]# service nginx status
最后效果如下: