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 whatismyip.com(just for example) whatismyip.com 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.
Asked
Active
Viewed 447 times
2 Answers
1
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' => '192.168.0.100:0',
),
);
$context = stream_context_create($opts);
echo file_get_contents('http://whatismyip.com', false, $context);
This will let you control which IP is used for the outbound connection.
EDIT: see http://www.php.net/manual/en/context.socket.php for more detail.
0
1) You need to register the domains to point to the IP addresses you want with your DNS provider:
host1.com -> 10.1.1.1 host2.com -> 10.1.1.2 host3.com -> 10.1.1.3 host4.com -> 10.1.1.4
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.
<VirtualHost 10.1.1.1>
ServerAdmin webmaster@host1.com
DocumentRoot /var/www/html/host1.com
ServerName host1.com
ErrorLog /var/log/httpd/host1.com-error_log
TransferLog /var/log/httpd/host1.com-access_log
</VirtualHost>