centos7 nginx 配置 systemctl start stop 失败

在vmware上安装了centos7,安装了nginx。使用sbin/nginx -c conf/nginx.conf是可以成功启动的,然后我想做成开机启动之类的,使用systemctl start/stop nginx来控制nginx。

当我执行命令 systemctl start nginx ,结果卡在那里动不了,只能使用ctrl+c来结束,这个时候使用浏览器访问80端口,发现是成功,ps -ef | grep nginx是存在的,说明nginx启动成功了,只不过是systemctl没有成功结束。然后开始找原因。

我在/etc/init.d/下建了nginx文件,这个文件是我从另外一个服务器上面直接复制下来的,说明这个文件是没有问题的。因为现在这个nginx和另外一个服务器上的nginx的安装是一样位置。

文件内容:

#!/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

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user /etc/passwd`" ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'`
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=`echo $opt | cut -d "=" -f 2`
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    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 -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

文件创建之后,不管我是使用/etc/init.d/nginx start 还是使用 /etc/init.d/nginx stop都达不到预期的作用,/etc/init.d/nginx start和systemctl start nginx都是一样卡在那里动不了,只能使用ctrl+c来结束。/etc/init.d/nginx stop和systemctl stop nginx也是停止不了nginx,只能pkill -9 nginx 干掉nginx。

使用 system status nginx 查看状态:

[root@bogon nginx]# systemctl status nginx
● nginx.service - SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: activating (start) since Sat 2019-05-25 22:41:57 EDT; 3min 18s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 9963 ExecStop=/etc/rc.d/init.d/nginx stop (code=exited, status=0/SUCCESS)
  Process: 9988 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
 Main PID: 9941 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nginx.service
           ├─10005 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─10006 nginx: worker process

May 25 22:41:57 bogon systemd[1]: Starting SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMA...ver...
May 25 22:41:57 bogon nginx[9988]: Starting nginx: [  OK  ]
May 25 22:41:57 bogon systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.
Hint: Some lines were ellipsized, use -l to show in full.

搞了好久,死活不能成功。权限方面都看过了,没有发现问题。在/usr/local/nginx/conf/nginx.conf文件里面明确指定了pid文件路径,在 /usr/local/nginx目录下,执行 sbin/nginx -c conf/nginx.conf 是成功的,pid文件也确实是自己指定,使用 ps -ef | grep nginx 得到的id确实是指定的pid文件里面的数字。

最后,我干脆就把pid的路径设置成 /var/run/nginx.pid 

结果就成功,这个说明是pid的文件的设置问题,为什么会有这种现象呢???。

当我执行命令:systemctl enable nginx 时,报错了:

[root@bogon system]# systemctl enable nginx
nginx.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig nginx on

百度了一下,然后只能在/lib/systemd/system/目录下面创建nginx.service

[root@bogon system]# pwd
/lib/systemd/system
[root@bogon system]# vi nginx.service

文件内容:

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

然后再执行命令:systemctl enable nginx

[root@bogon system]# pwd
/lib/systemd/system
[root@bogon system]# vi nginx.service
[root@bogon system]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@bogon system]# 

成功了。

然后我再次在 /usr/local/nginx/conf/ngin.conf 配置文件中重新指定pid的路径,结果成功了。

经过这一路的走来,得出个人的肤浅的心得:

不是通过yum install nginx 来安装nginx的,手动下载原码编译安装nginx。在没有创建 /lib/systemd/system/nginx.service 之前,只创建 /etc/init.d/nginx 文件,是不能使用systemctl功能来 start/stop控制nginx的。systemctl应该是通过service stat/stop的功能来控制nginx。当 /usr/lib/systemd/system/nginx.service 不存在时,也就是nginx不能通过service功能来start/stop 时,systemctl start/stop nginx 也就不能正常工作了。当创建了 /usr/lib/systemd/system/nginx.service 之后,service start / stop nginx的功能正常了,systemctl start / stop nginx的功能也就可以用了。

不知道我这个新的理解是否正确,恳请大牛们指点指点。

 

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值