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

环境

  • 系统:CentOS 7 amazon aws 云
  • 软件:nginx 1.10.2
  • 默认用户:CentOS
  • 安装方法:http://www.linuxidc.com/Linux/2017-01/139792.htm

问题

将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-1808: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 1808:34:26 systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
Jan 1808:34:26 systemd[1]: PID file /usr/local/nginx/logs/nginx.pid not readable (yet?) after start.
Jan 1808: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" ] && exit0

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 ] || exit5
    [ -f$NGINX_CONF_FILE ] || exit6echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval-eq0 ] && touch $lockfilereturn$retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval-eq0 ] && rm -f$lockfilereturn$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 && exit0$1
        ;;
    stop)
        rh_status_q || exit0$1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit7$1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit2esac

解决思路

开始以为是文件权限的问题。创建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 fromthelocal 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 isnotinthe 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-1809: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 1809:03:55 systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
Jan 1809:03:56 nginx[18719]: Starting nginx: [  OK  ]
Jan 1809: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-1809: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 1809:44:10 systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
Jan 1809:44:10 nginx[18957]: Starting nginx: [  OK  ]
Jan 1809: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用户来运行。

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/219833.html<

(0)
运维的头像运维
上一篇2025-04-14 07:15
下一篇 2025-04-14 07:16

相关推荐

  • RAKsmart独立服务器2026年测评,CN2 GIA实测数据与性能表现,CN2 GIA服务器到底怎么样,CN2 GIA独立服务器推荐

    RAKsmart 独立服务器在 2026 年已确立为连接中国内地与全球的高性能网络枢纽,其 CN2 GIA 线路实测延迟稳定在 35ms 以内,吞吐量突破 900Mbps,是跨境业务场景下兼顾稳定性与性价比的优选方案,核心网络性能深度解析2026 年 CN2 GIA 线路实测数据在 2026 年的网络架构中,R……

    2026-05-02
    0
  • hosteonsVPS测评,实测体验,hosteonsVPS怎么样?

    Hosteons VPS 在 2026 年的实测表现显示,其依托全球 BGP 线路优化与 NVMe 全闪存架构,在亚洲至北美跨洋延迟控制上表现优异,是追求高性价比与稳定性的中小型企业首选,但需注意其部分机房在晚高峰期的波动风险,核心性能与网络架构深度解析在 2026 年云计算基础设施全面向 AI 算力与边缘计算……

    2026-05-02
    0
  • BaCloud独立服务器测评不限流量实测表现,BaCloud独立服务器不限流量怎么样

    2026 年实测结论:BaCloud 独立服务器在不限流量场景下表现优异,特别适合高并发视频流媒体与大数据传输业务,其性价比与稳定性在同类竞品中处于第一梯队,但需关注其节点覆盖密度,在 2026 年云计算市场进入存量博弈与精细化运营并存的阶段,企业用户对于“不限流量”的诉求已从单纯的带宽大小转向实际吞吐能力与计……

    2026-05-02
    0
  • 香港旅游好去处,香港自由行攻略,香港签证怎么办理

    2026 年香港作为全球顶级金融与科创枢纽,其核心优势在于“一国两制”下的资金自由流动、低税率环境及与国际市场无缝对接的法治体系,是跨境企业布局亚太的首选地,2026 香港宏观环境:政策红利与产业格局进入 2026 年,香港在巩固国际金融中心地位的同时,正加速向“国际创新科技中心”转型,根据香港特区政府统计处及……

    2026-05-02
    0
  • RamNode 是什么?RamNode 价格贵吗

    RamNode 在 2026 年依然是全球高性价比独立服务器首选,尤其适合预算有限但追求极致 I/O 性能与 99.9% 在线率的中小型跨境电商及游戏开发者,在 2026 年的云计算版图中,RamNode 凭借其独特的“内存优先”架构与极致的成本控制策略,继续稳固其在 VPS 市场的头部地位,对于寻求RamNo……

    2026-05-02
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注