所以我购买专用服务器和提供商说我有4个可用的IP。 让我们说,这是IP1,IP2 … IP4,所以我想设置我的专用服务器上运行多个域上的IP,例如在IP1上的domain1 …我没有这个问题,现在我想当我运行PHP脚本在domain1 / script.php上,当这个脚本从whatismyip.com(例如)whatismyip.com显示ip1的数据(file_get_contents()),当我运行domain2它显示ip2等。 所以这意味着每个域都有自己独立的外部IP,我需要帮助如何在基于Linux(CentOS)的服务器上设置这个。 我知道是可能的,VPS是如何工作的例子,但我想知道解决scheme在哪里,我需要从PHP代码,或Apacheconfiguration或第三个选项启动自定义的Linux脚本? 如果你知道解决scheme,请帮助我。 先谢谢你。
如果您正在使用file_get_contents ,则需要使用stream_create_context函数调用来创build上下文,并将其绑定到所需的特定IP。 像这样的东西:
$opts = array( 'socket' => array( 'bindto' => '192.168.0.100:0', ), ); $context = stream_context_create($opts); echo file_get_contents('http://whatismyip.com', false, $context);
这将允许您控制哪个IP用于出站连接。
编辑:见http://www.php.net/manual/en/context.socket.php更多的细节。
1)您需要注册域以指向您的DNS提供商所需的IP地址:
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)尝试像这样在apache中创build虚拟主机:但是为每个IP创build一个虚拟主机条目,以便最终获得4个虚拟主机。
<VirtualHost 10.1.1.1> ServerAdmin [email protected] 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>