LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-18-2012, 01:35 PM   #1
sean.simpson
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Rep: Reputation: 0
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...)
 
Old 07-18-2012, 01:57 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
Old 07-18-2012, 10:39 PM   #3
tkhater
LQ Newbie
 
Registered: Aug 2004
Posts: 6

Rep: Reputation: 1
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.
 
1 members found this post helpful.
Old 07-19-2012, 02:57 PM   #4
sean.simpson
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Kustom42 View Post
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.
 
Old 07-19-2012, 03:01 PM   #5
sean.simpson
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tkhater View Post
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?
 
Old 07-19-2012, 03:08 PM   #6
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by sean.simpson View Post
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
localhost bind will not resolve from other clients cwc Linux - Networking 3 01-17-2007 10:06 PM
Unable to resolve any hostnames, including localhost calamari Linux - Networking 10 11-02-2005 01:17 PM
can't resolve localhost auji Linux - Networking 16 09-29-2005 02:39 PM
Message from syslogd@localhost localhost kernel: Disabling IRQ #21 ylts Linux - Hardware 0 02-26-2005 08:01 AM
postfix can't resolve localhost yapp Linux - Networking 2 12-02-2003 01:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration