1. 使用LNMP安装完成以后,出现404错误。
    • 检查/etc/nginx/conf.d/目录下的.conf星号表示根据实际情况,由用户自定义的文件名)文件中,root字段一行的路径是否为网站的根目录。
      server {
          listen 80;
          root /home/wwwroot/default/;    # 检查此行是否为网站的根目录
          location / {
              index index.php index.html index.htm;
              try_files $uri $uri/ /index.php index.php;
          }
          # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
          location ~ .php$ {
              # fastcgi_pass  /tmp/php-cgi.sock;
              fastcgi_pass    127.0.0.1:9000;
              fastcgi_index  index.php;
              fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
              include        fastcgi_params;
          }
      }
      
    • 通过ls -l检查网站根目录的权限是否正常,如果不正常,使用chmod 755 [dir]配置权限。

  2. 路径权限配置正常,出现502 Gateway错误

    • 使用cat /etc/nginx/nginx.conf | grep ^user查看检查nginx所属的用户/用户组。也可以使用ps aux | grep nginx检查nginx所属的用户/用户组。
    • 更改php-fpm.conf文件中,listen.owner和listen.group的值为上一步console展示的user后面的结果。例如上一步结果为user nginx,那么将listen.owner和listen.group的值更新为nginx。
    • 保持/etc/nginx/conf.d/*.conf文件中fastcgi_pass和/usr/local/php/etc/php-fpm.conflisten的值一致。一般情况下,/etc/nginx/conf.d/*.conf中fastcgi_pass的值为127.0.0.1:9000,/usr/local/php/etc/php-fpm.conf中listen的值为/tmp/php-cgi.sock。可以将fastcgi_pass的值更改为/tmp/php-cgi.sock,也可以将listen的值更改为127.0.0.1:9000,这两种做法都是可以的。但是,如果都把值都更新为/tmp/php-cgi.sock,可能会出现nginx: [emerg] invalid host in upstream "/tmp/php-cgi.sock" in /etc/nginx/conf.d/*.conf:10的报错提示,网上的解决办法无一例外都是采用chmod或者chown配置nginx的访问/tmp/php-fpm.sock的权限,但笔者尝试过这种办法以后依旧不可行。所以推荐将这两处的值统一更新为127.0.0.1:9000。
    • 一些题外话,在解决502 Gateway错误的时候,由于nginx和php所属的用户/用户组不同,有人将这两个的用户/用户组改为了一样,但是这样做的后果就是,会导致nginx或者php的系统文件无法访问,乃至配置及用户配置无法保存。因为你也不知道这些程序到底影响了哪些文件,可能会遭至后期莫名奇妙的bug。