简体   繁体   English

无法让 PHP 在 Nginx 上服务 -- 错误 502 网关错误

[英]Cannot get PHP to serve on Nginx -- Error 502 Bad Gateway

I'm fairly new to nginx and assumed it would be very straightforward to serve php with it since that setup is so common, but it seems like it's much more complex than I anticipated.我对 nginx 还很陌生,并认为用它来服务 php 会非常简单,因为这种设置很常见,但它似乎比我预期的要复杂得多。

Here's my config..这是我的配置..

    server {
            listen 80;
            server_name domain.com www.domain.com;

            location / {
                    root   /srv/www/domain.com/public_html;
                    index index.php;
            }

    # serve static files directly
    #location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$
    #    access_log        off;
    #    expires           30d;

            location ~ [^/]\.php(/|$) {
                    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
                    if (!-f $document_root$fastcgi_script_name) {
                            return 404;
                    }

                    fastcgi_pass /var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
    }

If I replace the "index.php" with a "index.html" file, nginx serves up the html perfectly.如果我用“index.html”文件替换“index.php”,nginx 完美地提供了 html。 But switching over to php causes a 502 error.但是切换到 php 会导致 502 错误。

I've seen guides that recommend modifying anything from iptables to php-fpm to the php.ini, to fast-cgi to sites-available..?我看过指南建议修改从 iptables 到 php-fpm 到 php.ini 到 fast-cgi 到可用站点的任何内容..?

I'm not sure what many of these tutorials are trying to do exactly... for now I'd just like my index.php to serve up phpinfo().我不确定这些教程中的许多正在尝试做什么......现在我只想让我的 index.php 提供 phpinfo()。 What's the next step?下一步是什么?

Is there a clear guide that goes over the various options available for serving php with nginx?是否有明确的指南来介绍可用于为 php 和 nginx 提供服务的各种选项?

您必须安装php-fpm并添加此部分: http//wiki.nginx.org/PHPFcgiExample

I assume you have successfully installed php-fpm (FastCGI Process Manager) 我假设您已成功安装php-fpm(FastCGI Process Manager)

Go to php-fpm/php.ini 转到php-fpm/php.ini

Find the line cgi.fix_pathinfo=1 , uncomment it and change the value 1 to 0 . 找到行cgi.fix_pathinfo=1 ,取消注释并将值1更改为0

cgi.fix_pathinfo=0

Now restart php-fpm service. 现在重启php-fpm服务。

Through your terminal 通过您的终端

service php5-fpm restart (I'm not sure about your Linux distro must be same) service php5-fpm restart (我不确定你的Linux发行版必须是一样的)

This might be the issue with the permission, you can identify the error in the log file这可能是权限问题,您可以在日志文件中识别错误

  • Extract the record from the log从日志中提取记录

    022/08/09 20:37:27 [crit] 3930#0: *17 connect() to unix:/var/run/php/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.80.36, server: 127.0.0.1, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php-fpm.sock:", host: "127.0.0.1" 022/08/09 20:37:27 [crit] 3930#0: *17 connect() to unix:/var/run/php/php-fpm.sock 在连接到上游客户端时失败(13:权限被拒绝) :192.168.80.36,服务器:127.0.0.1,请求:“GET /info.php HTTP/1.1”,上游:“fastcgi://unix:/var/run/php/php-fpm.sock:”,主机: “127.0.0.1”

This give clue that server conflict, now to verify which user running we need to check the php-fpm and nginx worker node这提供了服务器冲突的线索,现在要验证运行哪个用户,我们需要检查 php-fpm 和 nginx 工作节点

  • Worker Node Execute the command ps aux | grep 'nginx'工作节点执行命令ps aux | grep 'nginx' ps aux | grep 'nginx' this will indicate the user of the nginx worker node, in this case it is nobody ps aux | grep 'nginx'这将指示 nginx 工作节点的用户,在这种情况下它是nobody

    root@ubuntu:/var/nginx/html# ps aux | root@ubuntu:/var/nginx/html# ps aux | grep 'nginx' grep 'nginx'

  • nobody 3930 0.0 0.0 37636 3784?没有人3930 0.0 0.0 37636 3784? S 20:37 0:00 nginx: worker process S 20:37 0:00 nginx:工作进程

  • Check the php fpm service Execute the command ps aux | grep 'php-fpm'检查 php fpm 服务执行命令ps aux | grep 'php-fpm' ps aux | grep 'php-fpm' It will indicate the user as "www-data" in my case ps aux | grep 'php-fpm'在我的情况下,它将指示用户为“www-data”

    root@ubuntu:/var/nginx/html# ps aux | root@ubuntu:/var/nginx/html# ps aux | grep 'php' grep 'php'

  • www-data 1320 0.0 0.2 498796 11908? www-数据1320 0.0 0.2 498796 11908? S 19:34 0:00 php-fpm: pool www S 19:34 0:00 php-fpm: 池 www

  • www-data 1323 0.0 0.2 498796 11908? www-数据 1323 0.0 0.2 498796 11908? S 19:34 0:00 php-fpm: pool www S 19:34 0:00 php-fpm: 池 www

To resolve this conflict, in the nginx.conf file, you have to define the user derivate at top to be www-data ie user www-data;为了解决这个冲突,在 nginx.conf 文件中,您必须在顶部定义用户派生为 www-data 即用户 www-data;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM