Centos7.4 Nginx服务管理的两种方式

Centos7.4脚本/etc/init.d/nginx一直报错,搜索发现Centos7服务管理有区别以前的版本,解决问题的心情是这样的:

好了,开始正文~^ – ^~

在服务器上安装好了Nginx, 这里安装目录prefix=/usr/local/nginx, 我们可以直接用下面的命令来管理

# 启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 重新加载
/usr/local/nginx/sbin/nginx -s reload
# 停止
/usr/local/nginx/sbin/nginx -s stop

我们知道这样管理很不方便,一般都使用Nginx Init Script来管理,这里说的一般是在Centos6以下版本中,而Centos7中就有两种方式来管理Nginx服务。

一、通过Systemd来管理
0、新建文件/lib/systemd/system/nginx.service.
1、写入内容,内容模版参考官网NGINX systemd service file
2、修改nginx安装路径, 修改后的示例如下

[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、增加执行权限

chmod +x /usr/lib/systemd/system/nginx.service

4、加入开启启动

systemctl enable nginx.service

5、Nginx服务管理

# Nginx启动
systemctl start nginx.service
# Nginx状态查看
systemctl status nginx.service
# Nginx重载
systemctl reload nginx.service
# Nginx停止
systemctl stop nginx.service
# Nginx重新启动
systemctl restart nginx.service

6、Nginx启动日志查看

journalctl -f -u nginx.service

7、常见错误
systemctl start nginx提示
Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details
查看错误日志如下,发现80端口被占用了, 查看占用这个端口的进程(netstat -anlp | grep 80),停止就好了

8、运行状态查看

二、使用Nginx Init Script来管理
Centos7.4上想通过Nginx Init Script来管理Nginx, 首先需要配置好systemd,也就是通过方式一可以管理,Centos6.5并不需要
0、新建Init脚本/etc/init.d/nginx
1、脚本内容参考Nginx官网,Red Hat NGINX Init Script,注意修改nginx安装目录和pid路径
2、添加执行权限

chmod +x /etc/init.d/nginx

3、运行

/etc/init.d/nginx status
/etc/init.d/nginx start
/etc/init.d/nginx reload
/etc/init.d/nginx restart
/etc/init.d/nginx stop

4、添加到服务

chkconfig --add nginx

5、通过服务来管理

service nginx status
service nginx start
service nginx reload
service nginx restart
service nginx stop

6、常见错误
执行/etc/init.d/nginx status提示failed
/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled
解决:检查/usr/lib/systemd/system/nginx.service是否存在并配置,执行systemctl start nginx
7、重启示例

8、运行状态查看示例同方式一第8条
8、本服务器上脚步示例

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    /usr/local/bin/nginxd
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -TERM
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -q -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

参考:
https://my.oschina.net/liucao/blog/470458

发表评论

电子邮件地址不会被公开。 必填项已用*标注