插件窝 干货文章 LNMP部署网站出现404的解决方法

LNMP部署网站出现404的解决方法

shell php lnmp 404 服务器部署 971    来源:segmentfault    2019-10-14

今天在一个新的服务器上部署网站,访问php文件,竟然出现404
服务器是腾讯云的免费的服务器
Centos 7.2 + php7.1.7 + nginx1.12.1
安装的是lnmp一键安装包,没有使用编译的方式安装。
安装完后,添加配置文件在 /usr/local/nginx/conf/vhost
配置文件内容如下:

server {    
listen       80;    
root /home/wwwroot/site;   
index index.php index.html index.htm;    #charset koi8-r;
    access_log  /home/wwwlogs/site.access.log  main;   
error_log   /home/wwwlogs/site.error.log warn;    # redirect server error pages to the static page /50x.html
    #
    #error_page   500 502 503 504  /50x.html;
   location = /50x.html {       
 root   /usr/share/nginx/html;
    }    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php($|/)  {    
fastcgi_split_path_info ^(.+\.php)(/.+)$;       
fastcgi_pass   127.0.0.1:9000;        
fastcgi_read_timeout 150;       
fastcgi_index  index.php;        
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        
fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;        
include        fastcgi_params;
     }            
}

这个配置,一般来说都是没有问题的,然后就执行下面的命令重启nginx

service nginx restart

然后访问 我放在网站根目录的php文件,提示404,

这我就郁闷了,文件明显存在啊,怎么会找不到呢!难道根目录设置错了?
就在根目录放了个html文件,访问一下,200,可以访问!
那么这问题就明显了,这应该是配置文件中 php的问题!或者说是 php-fpm的问题
但是我没有配置错误日志,我先在配置文件中配置错误日志,测试一下,看看报错!
看到报错,我惊呆了!请看下面:

[error] 32520#0: *1 open() "/usr/share/nginx/html/50x.html" failed (2: No such file or directory)

怎么会这样,原来应该爆500错误的,但是找不到50x.html文件,就爆了404错误。

那我添加上这个 50x.html 文件,访问后,显示了这个50x.html 文件,然后查看错误日志显示如下:

[error] 344#0: *1 connect() failed (111: Connection refused) while connecting to upstream,

链接失败?怎么会?

这里的链接应该是 nginx配置文件中配置的链接 php-fpm的配置链接,配置文件应该是没问题的,那就查看下php-fpm是否已经启动!

ps -aux | grep php

显示正常,如图:
1571025118236262.png

说明php-fpm已经启动,就查看了下 9000端口是否已经开启:

netstat -ant | grep 9000

没有输出,也就是说,9000端口没有开启,也就是会所php-fpm没有占用900端口

然后查看php-fpm.conf,查看其中 代码,如下:

;listen = /tmp/php-cgi.sock

看到这,问题就很明显了,就是没有侦听9000端口啊,那么nginx配置中侦听的9000当然会失败
这里只需要修改下这句话就行了,如下:

listen = 9000
然后执行命令

service nginx restart

重启nginx

service php-fpm restart

重启 php-fpm

测试看看,一切OK!,不再报错!