您需要做的第一件事是查看是否在 NIC 接口上设置了 IP 地址,在 SSH 的命令行中键入 ifconfig 并查找具有您的 IP 地址的接口,如果只有一个您需要做的公共以下。
在 SSH 中输入 cd /etc/network/interfaces
然后输入ls -l
您将看到如下界面列表:
eth0 是你要找的东西,看看你有没有eth0:0 eth0:1 eth0:2 如果你没有其他eth接口,请执行以下操作。
假设我们要创建四个额外的虚拟接口来将 4 个 IP 地址(172.16.16.126、172.16.16.127、172.16.16.128、172.16.16.129 和 172.16.16.130)绑定到 NIC。
输入cd /etc/network/interfaces
现在打开“eth0”文件并添加“IPADDR_START”和“IPADDR_END”IP地址范围,如下所示。
输入vi eth0
这个 /etc/network/interfaces 文本将三个 IP 地址分配给 eth0。
auto eth0
allow-hotplug eth0
iface eth0 inet static
address 192.168.1.42
netmask 255.255.255.0
gateway 192.168.1.1
auto eth0:0
allow-hotplug eth0:0
iface eth0:0 inet static
address 192.168.1.43
netmask 255.255.255.0
auto eth0:1
allow-hotplug eth0:1
iface eth0:1 inet static
address 192.168.1.44
netmask 255.255.255.0
别名接口不应有“gateway”或“dns-nameservers”;允许动态 IP 分配。
以上配置是之前的传统方式,体现了传统使用ifconfig配置网络设备的方式。 ifconfig 引入了别名或虚拟接口的概念。这些类型的虚拟接口具有 interface:integer 形式的名称,ifconfig 将它们与真实接口非常相似。
保存并重启/启动网络服务
输入service network restart
验证是否使用 IP 地址创建了虚拟接口。
输入ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:172.16.16.125 Bcast:172.16.16.100 Mask:255.255.255.224
inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1385 errors:0 dropped:0 overruns:0 frame:0
TX packets:1249 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:127317 (124.3 KiB) TX bytes:200787 (196.0 KiB)
Interrupt:18 Base address:0x2000
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:172.16.16.126 Bcast:172.16.16.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:172.16.16.127 Bcast:172.16.16.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000
eth0:2 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:172.16.16.128 Bcast:172.16.16.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000
eth0:3 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:172.16.16.129 Bcast:172.16.16.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000
eth0:4 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:172.16.16.130 Bcast:172.16.16.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000
对于 HTTP:
要设置基于名称的虚拟主机,您必须告诉 Apache 您将使用哪个 IP 来接收 Apache 对所有网站或域名的请求。我们可以使用 NameVirtualHost 指令来做到这一点。使用 VI 编辑器打开 Apache 主配置文件。
输入vi /etc/httpd/conf/httpd.conf
搜索 NameVirtualHost 并通过删除它前面的 # 符号取消注释此行。
名称虚拟主机
接下来添加您希望接收 Apache 请求的可能 IP。更改后,您的文件应如下所示:
NameVirtualHost 192.168.0.100:80
现在,是时候为您的域设置虚拟主机部分了,按 Shift + G 移动到文件底部。在此示例中,我们正在为两个域设置虚拟主机部分
www.example1.com
www.example2.com
在文件底部为您在下面列出的 2 个示例中的每个站点和 IP 地址添加以下虚拟指令。
<VirtualHost 192.168.0.100:80>
ServerAdmin webmaster@example1.com
DocumentRoot /var/www/html/example1.com
ServerName www.example1.com
ErrorLog logs/www.example1.com-error_log
CustomLog logs/www.example1.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@example2.com
DocumentRoot /var/www/html/example2.com
ServerName www.example2.com
ErrorLog logs/www.example2.com-error_log
CustomLog logs/www.example2.com-access_log common
</VirtualHost>
您可以在域虚拟主机部分中添加任意数量的指令。当您完成对 httpd.conf 文件的更改后,请使用以下命令检查文件的语法。
输入httpd -t
Syntax OK
建议在进行一些更改之后和重新启动 Web 服务器之前检查文件的语法,因为如果任何语法出现错误,Apache 将拒绝处理一些错误并最终影响您现有的 Web 服务器关闭一段时间。如果语法没问题。请重新启动您的 Web 服务器并将其添加到 chkconfig 以使您的 Web 服务器仅在启动时以运行级别 3 和 5 启动。
输入service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]