LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   kernel upgraded, now php_network_getaddresses can't resolve localhost (https://www.linuxquestions.org/questions/linux-server-73/kernel-upgraded-now-php_network_getaddresses-cant-resolve-localhost-4175417433/)

sean.simpson 07-18-2012 01:35 PM

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

Kustom42 07-18-2012 01:57 PM

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.

tkhater 07-18-2012 10:39 PM

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.

sean.simpson 07-19-2012 02:57 PM

Quote:

Originally Posted by Kustom42 (Post 4731921)
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.

Actually, there is a nuance. Third party software packages like CakePHP, WordPress, etc. expect localhost to work.

sean.simpson 07-19-2012 03:01 PM

Quote:

Originally Posted by tkhater (Post 4732247)
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.

Oh how I wish this was it. It looks just like it, and I did do the sudo upgrade yesterday as well. However, my SELinux contexts are all fine. It is something like this though, though not SELinux related (or I'd see it in the results of sealert) Shared objects?

Kustom42 07-19-2012 03:08 PM

Quote:

Originally Posted by sean.simpson (Post 4733015)
Actually, there is a nuance. Third party software packages like CakePHP, WordPress, etc. expect localhost to work.

And I despise the code behind all of those open source CMS systems...


All times are GMT -5. The time now is 05:56 PM.