本篇文章主要是关于Nginx在编译安装时,可能出现的错误的解决方法。

1 Nginx启动脚本错误:env: /etc/init.d/nginx: No such file or directory

nginx ,但是在执行 service nginx start^M 的字符,就可以确定是DOS格式的了。那么可以在vim中执行命令 :%s/\r\+$//e

2 启动Nginx服务失败

service nginx start

[root@typecodes init.d]# service nginx start
Starting nginx (via systemctl):  Warning: Unit file of nginx.service changed on disk, 'systemctl daemon-reload' recommended.
Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.
[FAILED]

[root@typecodes init.d]# service nginx start
Starting nginx (via systemctl):  Warning: Unit file of nginx.service changed on disk, 'systemctl daemon-reload' recommended.
Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.
[FAILED]

systemctl status nginx.service

很明显是由于下面这个错误,导致Nginx服务启动失败。

Apr 11 21:43:07 typecodes nginx[4026]: Starting nginx: nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)

这种错误一般都是目录不存在或者权限不足,所以直接执行下面两条命令即可。

[root@typecodes ~]# cd /var/tmp/
[root@typecodes ~]# mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}

[root@typecodes ~]# cd /var/tmp/
[root@typecodes ~]# mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}

3 启动Nginx服务时,界面卡住

service nginx restart 后,终端界面能卡住,也就是Nginx服务控制脚本 nginx

#######启动Nginx服务出现警告
[root@typecodes init.d]# service nginx restart 
Restarting nginx (via systemctl):  Warning: Unit file of nginx.service changed on disk, 'systemctl daemon-reload' recommended.
Restarting nginx (via systemctl):  Warning: Unit file of nginx.service changed on disk, 'systemctl daemon-reload' recommended.
^C     ######终端界面卡住,使用ctrl+c命令强制结束

#######停掉Nginx服务
[root@typecodes init.d]# service nginx stop
Stopping nginx (via systemctl):  [  OK  ]

#######查看Nginx进程是否已被停止(可以看到未停止)
[root@typecodes init.d]# ps -aux|grep nginx 
root      7796  0.0  0.2  84184  2044 ?        Ss   21:14   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     7798  0.0  0.3  86656  3380 ?        S    21:14   0:00 nginx: worker process
root      7981  0.0  0.0 112644   964 pts/0    S+   21:19   0:00 grep --color=auto nginx

解决方法:可能是Nginx服务控制脚本(/etc/init.d/nginx)代码不正确,推荐使用文章 《Nginx服务启动、停止和重启等操作的SHELL脚本》 中的shell脚本。最后再重新执行下面的脚本即可。

[root@typecodes init.d]# chkconfig --add nginx
[root@typecodes init.d]# chkconfig nginx on
[root@typecodes init.d]# service nginx restart

[root@typecodes init.d]# chkconfig --add nginx
[root@typecodes init.d]# chkconfig nginx on
[root@typecodes init.d]# service nginx restart

/etc/nginx/nginx.conf 不正确,但是使用 nginx -t

4 Compilation failed in require或者perl_parse() failed

service nginx start 启动Nginx服务时报错,于是通过systemctl status nginx.service

nginx -t

Can't load '/usr/local/lib64/perl5/auto/nginx/nginx.so' for module nginx: /usr/local/lib64/perl5/au...m line 68. 的错误,导致了Nginx在调用函数时失败: perl_parse() failed 。也就是证明是之前没有安装perl依赖包,于是通过执行命令 yum -y install perl-devel perl-ExtUtils-Embed