PHP5 - Fails to resolve hostnames when not in interactive mode
Solution 1
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.
Solution 2
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
Solution 3
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
Solution 4
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!)
Kaja
Updated on June 04, 2022Comments
-
Kaja almost 2 years
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?
-
Pekka about 14 yearsI don't think allow_url_fopen() is the problem here, but comparing the PHP.ini's is a good idea.
-
Kaja about 14 yearsApologies, I should have explicitly stated that I'd checked the PHP.ini in use by Apache to be certain allow_url_fopen() was on. I'm looking into 'libnss_dns.so' but I've been unable to locate it in OS X 10.6 or the MacPorts distribution of Apache2. I didn't think it important when first posting, but this problem first occurred a few months ago and I was able to work around it by adding the remote server to '/etc/hosts'. Whatever the problem is now, manually editing the 'hosts' file doesn't seem to help.
-
richardkmiller over 13 yearsI was also getting this error with MacPorts 1.9.1. Installing ldns (sudo port install ldns) fixed it for me.
-
Derek Downey over 13 yearsI know this is old, but I was having this exact problem and removing ipv6 support fixed it for me
-
Marcel Colomb about 13 yearssudo port install ldns and restarting Apache fixed the problem for me as well thx!
-
Flexo over 12 yearsIt would be a better idea to fix the underlying problem than just brushing it under the carpet like that.
-
nortron about 12 yearsThe LoadFile directive fixed my problem, I couldn't get DNS resolution to work through Apache but it would work on the CLI.
-
BadPirate over 9 yearsThis is a gotcha listed in the PHP mysqli command (localhost is magic, so you need to use the IP 127.0.0.1 when trying to connect to it directly)
-
jakabadambalazs over 9 yearsYes, this work for me as well (i also have libnss_dns.so.2 and libnss_files.so.2) - I think if you look into your [chroot]/usr/share/zoneinfo you'll have a missing symlink to /etc/localtime (it seems missing from your procedure) - i think it is needed
-
jakabadambalazs over 9 yearsYou do not need the curl binary in your chrooted environment to be able to use php cURL. As a matter of fact, Joomla updates even without the php5-curl package installed. But if you need it and you install it, it works perfectly without the curl binary(and its libraries) in the chroot.
-
Stuart Cardall about 8 years@jakabadambalazs - yes
curl
binary not needed - have added an update to the instructions with my current method. -
TheDudeAbides almost 8 years@richardkmiller But how on earth did you determine that
ldns
was necessary? (I should note that this did did solve my problem with PHP 5.6.20 from MacPorts on OS X 10.11, while I was testing with the boris REPL.) -
richardkmiller almost 8 years@TheDudeAbides I wish I could remember. It has been too long and now I'm using brew.