Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a number of instances behind a loadbalancer. Is it possible to detect the name of the machine (e.g. ec2-XXX-XX-XXX-XXX.compute-1.amazonaws.com) in PHP for debugging? I looked through phpinfo() and it doesn't seem to have any kind of machine specific information.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

you'll have to use $host = system("hostname") or similar as it appears PHP won't provide that info directly.

IP seems a little more involved. Depends on how they've got things set up but you can try

$config = system("ifconfig | grep \"inet addr\"");
preg_match("/(\d{1,3})(\.\d{1,3})/", $config, $matches);
$ip_addr = $matches[1];
share|improve this answer
    
Hmmm. I think we are getting closer, but all machines are reporting 'p-aws-img-www-02' as the hostname. –  Louis W Oct 5 '10 at 14:11
    
Is there a way to get the machine's (not the domain's) ip address? –  Louis W Oct 5 '10 at 14:12

IIRC, the public hostname actually doesn't exist anywhere on the server. It's stored in EC2's metadata, which can be fetched from 169.254.169.254 on any EC2 instance.

$hostname = file_get_contents('http://169.254.169.254/latest/meta-data/public-hostname/');
share|improve this answer

This should do it:

http://php.net/manual/en/reserved.variables.server.php

$myservername = $_SERVER['SERVER_NAME'];
share|improve this answer
    
$_SERVER['SERVER_NAME'] is set to the domain name of my website, not the machine name. Because these are load balanced cloud instances I am sure there is a lot of magic happening behind the scenes. –  Louis W Oct 5 '10 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.