php - 为什么 chrooted PHP (FPM) 在 chrooted shell 没有问题时会出现 DNS 问题?

标签 php linux web-services networking nginx

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

7年前关闭。




Improve this question




我想做的事:

让 Nginx 在使用 debootstrap 创建的 chroot jail 中通过 FastCGI (FPM) 提供 PHP 文件。

问题:

每个将主机名解析为 IP 地址的函数都会失败,并显示 php_network_getaddresses: getaddrinfo failed: Name or service not known .奇怪的是,从 chrooted shell 解析主机名没有问题。

到目前为止我做了什么:

  • 我禁用了 jail 外的防火墙。
  • 我将/etc/resolv.conf、/etc/nsswitch.conf 和其他一些文件 (which I found here) 复制到了 jail 中。 (由于 Debootstrap,所有这些都已经存在,但我还是替换了它们!)
  • 我添加了 nameserver 8.8.8.8nameserver 8.8.4.4到/etc/resolv.conf。 (我以前没有这样做过,因为名称服务器是由 DHCP 服务器正确提供的!)
  • 我添加了 domain localhost 1 到/etc/resolv.conf 和 127.0.0.1 localhost 1 到/etc/hosts。
  • 我在 jail 里安装了一个域名服务器。
  • 我在 jail 外安装了一个名称服务器(哎呀)。
  • 我在 jail 里安装了/proc。

  • 不用说实际上没有解决问题,所以请帮助我。

    重现此所需的所有步骤:
  • 从 debian-7.4.0-amd64-netinst.iso 安装 Debian Wheezy 并使用默认设置,除了软件选择,只选中标准系统。
  • 意识到不选择距离较近的镜子是一个错误。
    vi /etc/apt/sources.list并使文件看起来像这样:
    deb http://ftp.de.debian.org/debian/ wheezy main contrib non-free
    deb-src http://ftp.de.debian.org/debian/ wheezy main contrib non-free
    
    deb http://security.debian.org/ wheezy/updates main
    deb-src http://security.debian.org/ wheezy/updates main
    
    deb http://ftp.de.debian.org/debian/ wheezy-updates main
    deb-src http://ftp.de.debian.org/debian/ wheezy-updates main
    
  • 在安装 Debootstrap、Nginx 和 PHP-FPM 之前,确保一切都是最新的。
    aptitude update && aptitude -y full-upgradeaptitude -y install debootstrap nginx php5-fpm
  • 使用 Debootstrap 为网站创建 chroot jail。
    debootstrap wheezy /srv/localhost http://ftp.de.debian.org/debian/ 1
  • 在之前创建的 jail 中创建一个名为 www 的目录,其中包含一个测试文件,并将 www-data 设为所有者。
    mkdir /srv/localhost/srv/www 1
    echo "<?php fsockopen('ftp.de.debian.org'); ?>" > /srv/localhost/srv/www/index.php 1
    chown -R 33:33 /srv/localhost/srv/www 1
  • 配置并启用站点。
    vi /etc/nginx/sites-available/localhost 1并使文件看起来像这样1:
    server {
      listen 127.0.0.1:80;
    
      server_name localhost;
    
      root /srv/localhost/srv/www;
      index index.html index.htm index.php;
    
      location ~ \.php$ {
        try_files     $uri =404;
        fastcgi_pass  unix:/var/run/localhost.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
        include       fastcgi_params;
      }
    
      location / {
        try_files $uri $uri/ /index.html;
      }
    
      location ~* \.(jpg|jpeg|gif|png|css|js|ico)$ {
        access_log off;
      }
    
      location = /favicon.ico {
        log_not_found off;
      }
    
      location ~ /\. {
        deny          all;
        access_log    off;
        log_not_found off;
      }
    }
    
    ln -s /etc/nginx/sites-available/localhost /etc/nginx/sites-enabled/ 1
  • 对 Nginx 提供的 FastCGI 参数进行微调。
    vi /etc/nginx/fastcgi_params并使文件看起来像这样:
    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;
    
    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  HTTPS              $https if_not_empty;
    
    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    
    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;
    
    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;
    
  • 为站点创建一个 PHP-FPM 池(?)。
    vi /etc/php5/fpm/pool.d/localhost.conf 1并使文件看起来像这样1:
    [localhost]
    
    user  = www-data
    group = www-data
    
    listen                 = /var/run/localhost.sock
    listen.allowed_clients = 127.0.0.1
    
    pm                      = ondemand
    pm.max_children         = 5
    pm.process_idle_timeout = 300s
    pm.max_requests         = 500
    
    ;access.log    = log/$pool.access.log
    ;access.format = "%R - %u %t \"%m %r\" %s"
    
    chroot = /srv/localhost/srv
    chdir  = /
    
    ;catch_workers_output = yes
    
    ;security.limit_extensions = .php .php3 .php4 .php5
    
    php_flag[display_errors]           = on
    php_admin_flag[log_errors]         = on
    php_admin_value[error_log]         = /var/log/php.log
    php_admin_value[memory_limit]      = 32M
    php_admin_value[session.save_path] = /tmp
    
    
    env[HOSTNAME] = $HOSTNAME
    env[PATH]     = /usr/local/bin:/usr/bin:/bin
    env[TMP]      = /tmp
    env[TMPDIR]   = /tmp
    env[TEMP]     = /tmp
    
  • 删除 Nginx 和 PHP-FPM 配置示例。
    rm /etc/nginx/sites-enabled/default /etc/php5/fpm/pool.d/wwww.conf
  • 重启 PHP-FPM 和 Nginx 服务。
    service php5-fpm restart && service nginx restart
  • 检查输出。
    wget -qO- http://localhost 1 个打印件:
    <br />
    <b>Warning</b>:  fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in <b>/www/index.php</b> on line <b>1</b><br />
    <br />
    <b>Warning</b>:  fsockopen(): unable to connect to ftp.de.debian.org:80:-1 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in <b>/www/index.php</b> on line <b>1</b><br />
    
  • chroot进 jail ,看看解析主机名没有问题
    chroot /srv/localhost/ 1
    ping -c1 ftp.de.debian.org打印:
    PING ftp.de.debian.org (141.76.2.4) 56(84) bytes of data.
    64 bytes from ftp.de.debian.org (141.76.2.4): icmp_req=1 ttl=56 time=15.1 ms
    
    --- ftp.de.debian.org ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 15.137/15.137/15.137/0.000 ms
    


  • 1 我的实际域的所有实例都已替换为 localhost,而我的实际 IP 地址的所有实例均已替换为 127.0.0.1。

    2 我已导出 Oracle® VirtualBox 设备并将其上传到 Mega.co.nz (root密码就是密码)给所有真正非常渴望在这里帮助我的人。

    最佳答案

    我不知道为什么会发生这种情况,但是您可能可以通过使用 strace 找出答案。要安装 strace,您需要执行 apt-get install strace .然后你用 ps ax | grep nginx 得到 nginx 的 PID .然后对于实际的跟踪:

    strace -f -s 1024 -p PIDOFNGINX -o /tmp/trace.out
    

    然后你启动 wget -qO- http://localhost ,停止 nginx(或杀死 strace),然后查看/tmp/trace.out 以及它在哪里引发错误。您的 chroot 中可能缺少一些库。
    编辑:
    你写你所做的事情的方式实际上并没有在chroot中执行测试ping。那应该是:
    chroot /srv/localhost /bin/ping -c1 ftp.de.debian.org
    

    然后,您实际上可以 strace 那个 ping,然后它会包含更少的来自 nginx 的东西。这将要求 ping 命令也位于 chroot 中(无论如何都需要它来测试它),包括它依赖的库等。

    关于php - 为什么 chrooted PHP (FPM) 在 chrooted shell 没有问题时会出现 DNS 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23155497/

    相关文章:

    php - 获取没有标题/编码的外部网页的html源

    php - 如何检索mysql数据库信息

    javascript - ER_NOT_SUPPORTED_AUTH_MODE - MySQL 服务器

    java - 依次调用15个Web服务

    web-services - 如何解决只有在类定义上具有[ScriptService]属性的Web服务才能从脚本中调用

    c# - .net 4.0 中返回 xml 而不是 json 的 asmx web 服务

    php - 这是 PDO 'good code' 的包装器吗?有没有潜在的问题?

    php - PHP 中的 MySQL 结果数组

    linux - 尾部反转/打印除最后 n 行之外的所有内容?

    linux - 如何仅在文件中的一行中查找两个单词,它们之间存在特定数量的随机单词