macos PHP5 - 在非交互模式下无法解析主机名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2223620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:44:48  来源:igfitidea点击:

PHP5 - Fails to resolve hostnames when not in interactive mode

phpmacosapache2dnshostname

提问by Kaja

I'm working with an OS X 10.6 Server running Apache2 / PHP5 and having a problem with PHP not resolving hostnames when 'fopen()' tries to retrieve a file from a remote server. When run in interactive mode on the command line 'fopen()' works perfectly. However, when run through the web it will always fail with the error:

我正在使用运行 Apache2/PHP5 的 OS X 10.6 服务器,当“fopen()”尝试从远程服务器检索文件时,PHP 无法解析主机名。在命令行中以交互模式运行时,'fopen()' 完美运行。但是,当通过网络运行时,它总是会失败并显示错误:

failed to open stream: php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known

I'm at a loss in finding the source of this problem: 'fopen()' works on the web when given an IP address instead of a hostname; 'gethostbyname()' also fails when run on the web (it doesn't error, it just returns whatever hostname it was given to resolve) but also works fine when run in interactive mode. The only exception seems to be 'dns_get_record()' which works fine when run on the web or in interactive mode.

我无法找到这个问题的根源:当给定 IP 地址而不是主机名时,'fopen()' 可以在网络上工作;'gethostbyname()' 在 web 上运行时也会失败(它不会出错,它只是返回它要解析的任何主机名),但在交互模式下运行时也能正常工作。唯一的例外似乎是“dns_get_record()”,它在网络上或交互模式下运行时运行良好。

I've been trying to find DNS problems on the server but dig, nslookup, and ping all work and "scutil -r" says the remote server is reachable with the current DNS settings. Any ideas on where the problem might be?

我一直在尝试查找服务器上的 DNS 问题,但 dig、nslookup 和 ping 都可以正常工作,并且“scutil -r”表示可以使用当前的 DNS 设置访问远程服务器。关于问题可能出在哪里的任何想法?

回答by Nobu

Try to recompile PHP with '--disable-ipv6' option. Then, apachectl stop & start (not 'apachectl restart'). I've got the same error message on Mac OS 10.6.4 + manually compiled PHP5.3.3 + Apache2.2.16.

尝试使用“--disable-ipv6”选项重新编译 PHP。然后,apachectl 停止并启动(不是 'apachectl restart')。我在 Mac OS 10.6.4 + 手动编译的 PHP5.3.3 + Apache2.2.16 上收到相同的错误消息。

回答by Johnco

You must set properly the allow_url_fopen value of your php.ini.

您必须正确设置 php.ini 的 allow_url_fopen 值。

http://ar.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

http://ar.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

Consider the CLI uses a different php.ini that the webserver (I assume you are using Apache).

考虑到 CLI 使用与网络服务器不同的 php.ini(我假设您使用的是 Apache)。

Another option may be you are not including libnss_dns

另一种选择可能是您不包括 libnss_dns

Try adding to your httpd configuration (correct path as needed for you system):

尝试添加到您的 httpd 配置(根据您的系统需要正确的路径):

LoadFile /lib/libnss_dns.so

回答by Stuart Cardall

If you are are running PHP-FPMwith chroot settings (as you should be) - then neither DNS nor mail will work.

如果您PHP-FPM使用 chroot 设置运行(正如您应该的那样) - 那么 DNS 和邮件都将不起作用。



Update Feb 2016

2016 年 2 月更新

straceusing gethostbynameonly from the test-script@ https://knzl.de/setting-up-a-chroot-for-php/solved dnsfor me in an Alpine Linux/ nginx chroot. I also use the shfrom this link with mini-sendmailto solve the mailproblem.

stracegethostbyname仅使用来自测试脚本@ https://knzl.de/setting-up-a-chroot-for-php/dnsAlpine Linux/ 中为我解决了nginx chroot。我还使用sh来自此链接的 withmini-sendmail来解决mail问题。



An approximate list of files needed for DNS & Mail in a chroot

chroot 中 DNS 和邮件所需文件的大致列表

The following commands got DNS working for me with PHP5.5 on Debian (tested with the Joomla builtin update component):

以下命令让 DNS 在 Debian 上使用 PHP5.5 对我来说有效(使用 Joomla 内置更新组件测试):

#!/bin/sh

chroot=/var/www
domain=mydomain
webuser=www-data
webgroup=www-data

cd $chroot/$domain

mkdir etc

cp /etc/resolv.conf etc/
cp /etc/nsswitch.conf etc/
echo $(cat /etc/group|grep $webgroup) > etc/group
echo $(cat /etc/passwd|grep $webuser) > etc/passwd
cp /etc/services etc/
cp /etc/protocols etc/
cp /etc/host.conf etc/
cp /etc/hosts etc/
cp /etc/networks etc/

mkdir -p usr/bin   
mkdir usr/sbin

cp /usr/sbin/ssmtp usr/sbin/
cp /usr/sbin/sendmail usr/sbin/

mkdir usr/share

cp -rf /usr/share/zoneinfo usr/share

mkdir lib

cp /lib/i386-linux-gnu/libresolv.so.2 lib/
cp /lib/i386-linux-gnu/libnss_compat.so.2 lib/
cp /lib/i386-linux-gnu/libc.so.6 lib/
cp /lib/i386-linux-gnu/libnsl.so.1 lib/
cp /lib/i386-linux-gnu/libnss_files.so.2 lib/
cp /lib/i386-linux-gnu/libz.so.1 lib/
cp /lib/i386-linux-gnu/libdl.so.2 lib/
cp /lib/i386-linux-gnu/libcidn.so.1 lib/
cp /lib/i386-linux-gnu/ld-linux.so.2 lib/
cp /lib/i386-linux-gnu/libcrypt.so.1 lib/
cp /lib/i386-linux-gnu/libnss_nis.so.2 lib/
cp /lib/i386-linux-gnu/libnss_dns.so.2 lib/

chown -R $webuser:$webgroup lib usr etc
service php5-fpm restart

I did not need /bin/sh or /usr/lib/locale

我不需要 /bin/sh 或 /usr/lib/locale

回答by BadPirate

Same problem, different solution then the original poster. I was calling mysqli(...) to connect, but the URL I had for the server was wrong. So it doesn't always mean you are unable to do any DNS lookup, just that the server that it is not able to do that specific lookup (check your host!)

同样的问题,与原始海报不同的解决方案。我正在调用 mysqli(...) 进行连接,但是我为服务器提供的 URL 是错误的。因此,这并不总是意味着您无法进行任何 DNS 查找,只是它无法进行特定查找的服务器(检查您的主机!)

回答by yvess

What is the hostname you tried to resolve? I had the same problem with localhost.

您尝试解析的主机名是什么?我在本地主机上遇到了同样的问题。

This didn't worked and I got your Error. I changed the hostname to 127.0.0.1 than it worked. Not the best solution, but good for a workaround.

这没有用,我得到了你的错误。我将主机名更改为 127.0.0.1 而不是有效。不是最好的解决方案,但有利于解决方法。

回答by user606618

Disable IPv6 in your TCP/IP configuration in net preference.

在网络首选项的 TCP/IP 配置中禁用 IPv6。