kernel upgraded, now php_network_getaddresses can't resolve localhost
I did a kernel upgrade, and all our php code that referenced localhost is now failing. Strange part is, it all works from the command line. Here is sample code:
Prelude: allow_url_fopen = On <?php $url = "http://localhost/index.php"; echo "gethostbyname yields ", gethostbyname("localhost") , "\n"; var_export( dns_get_record("localhost") ); $result = file_get_contents("$url"); if ($result == false) { echo "\nCould not open connection to localhost\n"; } else { echo "\nResult is: ", $result; } ?> Here is the output when accessed via a browser: gethostbyname yields localhost array ( 0 => array ( 'host' => 'localhost', 'type' => 'A', 'ip' => '127.0.0.1', 'class' => 'IN', 'ttl' => 655360, ), ) Could not open connection to localhost And the error message from the php logs: [Wed Jul 18 10:44:28 2012] [error] [client 10.40.0.26] PHP Warning: file_get_contents() [<a href='function.file-get-contents'>function.file-get-contents</a>]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/website/html/mytest.php on line 3 [Wed Jul 18 10:44:28 2012] [error] [client 10.40.0.26] PHP Warning: file_get_contents(http://localhost/index.php) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: Not a directory in /var/www/website/html/mytest.php on line 3 As I mentioned, I can run this from the command line and get our home page, even when I su to apache. Can anyone help give me some insight? Wew resolved this by substituting 127.0.0.1 for localhost in our code, but that is not the solution I'm looking for (since we are using name based virtual hosts, this only works because we're looking for the first vhost...) |
I always recommend people do not use localhost or loopback addresses. Can you verify that the /etc/hosts file has your localhost hostname mapped to your lo address of 127.0.0.1.
NOTE: localhost simply maps to 127.0.0.1 so there really is no difference in using one or the other in your code. |
I had a similar problem yesterday on Centos 5. It turns out a sudo updated corrupted the selinux context of /etc/nsswitch.conf which made all localhost lookups fail. See if it works with selinux set to permissive. If so, try restorecon /etc/nsswitch.conf and reset selinux to enforcing. Good luck.
|
Quote:
|
Quote:
|
Quote:
|
All times are GMT -5. The time now is 05:56 PM. |