Apache基于IP和端口
Apache基于IP
步驟1:添加并配置虛擬網卡
- 添加虛擬網卡:通常在虛擬機環境中,可以通過虛擬機軟件(如VMware或VirtualBox)的網絡設置來添加額外的網絡適配器。
- 配置IP地址:編輯
/etc/sysconfig/network-scripts/ifcfg-ethX文件,,并將它們設置為靜態IP地址。
vi /etc/sysconfig/network-scripts/ifcfg-ens33
# 內容如下:
DEVICE=ens33
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.10.100
NETMASK=255.255.255.0
vi /etc/sysconfig/network-scripts/ifcfg-ens36
# 內容如下:
DEVICE=ens36
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.10.200
NETMASK=255.255.255.0

3. 重啟網絡服務:
systemctl restart network
步驟2:修改Apache配置文件
編輯/etc/httpd/conf/httpd.conf,確保以下行未被注釋(即刪除#):
IncludeOptional conf.d/*.conf
然后,在/etc/httpd/conf.d/目錄下創建兩個新的配置文件,比如vhost1.conf和vhost2.conf,分別用于配置基于IP的虛擬主機:
vim /etc/httpd/conf.d/vhost1.conf
# 內容如下:
<VirtualHost 192.168.10.100:80>
DocumentRoot "/var/www/html/web1"
</VirtualHost>
vim /etc/httpd/conf.d/vhost2.conf
# 內容如下:
<VirtualHost 192.168.10.200:80>
DocumentRoot "/var/www/html/web2"
</VirtualHost>
或者寫到一個配置文件中
[root@localhost ~]# cat /etc/httpd/conf.d/vhosts-ip.conf
<VirtualHost 192.168.10.100:80>
DocumentRoot "/var/www/html/web1"
</VirtualHost>
<VirtualHost 192.168.10.200:80>
DocumentRoot "/var/www/html/web2"
</VirtualHost>

步驟3:創建目錄和主頁文件
mkdir -p /var/www/html/web1 /var/www/html/web2
echo "This is my virtual website1." > /var/www/html/web1/index.html
echo "This is my virtual website2 ." > /var/www/html/web2/index.html
重啟Apache服務
systemctl restart httpd
步驟4:測試基于IP的虛擬主機


Apache基于端口
步驟5:配置基于端口的虛擬主機
編輯/etc/httpd/conf.d/下的新配置文件,例如vhost3.conf和vhost4.conf,配置不同的端口:
vim /etc/httpd/conf.d/vhost3.conf
# 內容如下:
Listen 8000
<VirtualHost *:8000>
DocumentRoot "/var/www/html/web3"
</VirtualHost>
vim /etc/httpd/conf.d/vhost4.conf
# 內容如下:
Listen 8888
<VirtualHost *:8888>
DocumentRoot "/var/www/html/web4"
</VirtualHost>
或者全部寫到一個配置文件中
[root@localhost ~]# cat /etc/httpd/conf.d/vhosts-port.conf
Listen 8000
<VirtualHost 192.168.10.100:8000>
DocumentRoot "/var/www/html/web3"
</VirtualHost>
Listen 8888
<VirtualHost 192.168.10.100:8888>
DocumentRoot "/var/www/html/web4"
</VirtualHost>
步驟6:創建目錄和主頁文件
mkdir -p /var/www/html/web3 /var/www/html/web4
echo "This is my virtual website3." > /var/www/html/web3/index.html
echo "This is my virtual website4." > /var/www/html/web4/index.html
步驟7:重啟Apache服務
systemctl restart httpd
測試基于端口的虛擬主機
再次在Windows系統下使用瀏覽器訪問這兩個端口,確保能夠看到正確的網頁。



浙公網安備 33010602011771號