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