你好,游客 登录 注册 搜索
背景:
阅读新闻

CentOS 7 编译安装Nginx1.10.2 脚本启动失败解决思路

[日期:2017-01-21] 来源:CSDN  作者:mingjie1212 [字体: ]

环境

问题

将nginx脚本放入/etc/init.d/中,在root下使用

/etc/init.d/nginx start
Starting nginx (via systemctl): #卡住

检测服务状态如下输出:

[root@nginx]# service nginx status
● 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: inactive (dead) since Wed 2017-01-18 08:37:35 UTC; 2s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 18249 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nginx.service
           ├─7276 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─7277 nginx: worker process

Jan 18 08:34:26 systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
Jan 18 08:34:26 systemd[1]: PID file /usr/local/nginx/logs/nginx.pid not readable (yet?) after start.
Jan 18 08:37:35 systemd[1]: Stopped SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.

nginx脚本如下:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
    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

解决思路

开始以为是文件权限的问题。创建nginx用户,并切换到nginx用户启动服务。
创建nginx nginx 组和用户
passwd nginx #创建密码登陆
并将ngixn 的所属关系都使用chown更改。

chown nginx:nginx /usr/local/nginx -R #更改所属关系
groupadd nginx
useradd -g nginx nginx
passwd nginx

su nginx #切换nginx用户

/etc/init.d/nginx start #执行启动Nginx命令
#输出
Starting nginx (via systemctl):  ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: Cloud User (centos)
Password: 
#发现还是要输入密码...
#应该是解决思路不对,启动关联到了centos用户,nginx用户尝试使用sudo命令启动
sudo /etc/init.d/nginx start
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for nginx: 
nginx is not in the sudoers file.  This incident will be reported.
#需要把nginx用户加入sudoers 文件中,好麻烦...
#大致清楚了,就算root运行这个命令也需要root就切换到root尝试了下。
exit
#root下sudo 运行成功
[root@nginx]# sudo /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  OK  ]
[root@nginx]# systemctl status nginx.service
�� 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: active (running) since Wed 2017-01-18 09:03:56 UTC; 15min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 18719 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
 Main PID: 18726 (nginx)
   CGroup: /system.slice/nginx.service
           ├─18726 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           ├─18727 nginx: worker process
           └─18728 nginx: worker process

Jan 18 09:03:55 systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
Jan 18 09:03:56 nginx[18719]: Starting nginx: [  OK  ]
Jan 18 09:03:56 systemd[1]: Started SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.
#检查状态
systemctl status nginx.service
● 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: active (running) since Wed 2017-01-18 09:44:10 UTC; 2s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 18957 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
 Main PID: 18964 (nginx)
   CGroup: /system.slice/nginx.service
           ├─18964 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           ├─18965 nginx: worker process
           └─18966 nginx: worker process

Jan 18 09:44:10 systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
Jan 18 09:44:10 nginx[18957]: Starting nginx: [  OK  ]
Jan 18 09:44:10 systemd[1]: Started SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.

由于云主机不能随便重启,共享地址会变化…所以没有尝试以下方式,应该改selinux也能实现访问的限制的移出。
应该修改/etc/selinux/config
SELINUX=disabled

以后尝试一下。

在生产环境 单独创建出来个用户运行nginx 比直接用root运行安全。还是使用nginx用户来运行。

CentOS 7.2下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1  http://www.linuxidc.com/Linux/2016-09/134804.htm

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx的500,502,504错误解决方法 http://www.linuxidc.com/Linux/2015-03/115507.htm

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-01/139794.htm

linux
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2017/8/1 14:12:08
你这最后说了个啥 怎么解决的说了个啥