Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I buy Dedicated Server and provider say I have 4 usable IPs. Lets say this are ip1, ip2...ip4 , so I want to set my Dedicated Server to run multiple domains on this IPs, example domain1 on ip1... I did this with no problem, now I want that when I run php script on domain1/script.php when this script fetches data(file_get_contents()) from for example) shows ip1, and when I run on domain2 it shows ip2,etc. So this means every domain has its own separate external IP, I need help how to set this on linux(CentOS) based server. I know is possible, VPS is example how this works, but I want to know where is the solution, do I need custom linux script started from PHP code, or apache config or third option? Please help me if you know solution. Thank you in advance.

share|improve this question

migrated from May 18 '11 at 0:31

This question came from our site for professional and enthusiast programmers.

If you are specifically using file_get_contents you need to use the stream_create_context function call to create a context and bind it to the specific IP you want. Something like this:

$opts = array(
    'socket' => array(
        'bindto' => '',
$context = stream_context_create($opts);
echo file_get_contents('', false, $context);

This will let you control which IP is used for the outbound connection.

EDIT: see for more detail.

share|improve this answer

1) You need to register the domains to point to the IP addresses you want with your DNS provider: -> -> -> ->

2) Try to create virtual hosts in apache like this: but create one virtual host entry for every IP so that you end up with 4 virtual hosts.

    DocumentRoot /var/www/html/
    ErrorLog /var/log/httpd/
    TransferLog /var/log/httpd/
share|improve this answer

Your Answer


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