English 中文(简体)
PHP5 - Fails to resolve hostnames when not in interactive mode
原标题:

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:

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.

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?

问题回答

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.

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

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).

Another option may be you are not including libnss_dns

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

LoadFile /lib/libnss_dns.so

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


Update Feb 2016

strace using gethostbyname only from the test-script @ https://knzl.de/setting-up-a-chroot-for-php/ solved dns for me in an Alpine Linux / nginx chroot. I also use the sh from this link with mini-sendmail to solve the mail problem.


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

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

#!/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

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!)

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.

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





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签