標籤:解析 eth0 inet6 dex 目錄 基於網域名稱 res host forever
1 Apache虛擬機器主機的實現方式有3種。
- 基於IP的虛擬機器主機
- 基於連接埠的虛擬機器主機
- 基於網域名稱的虛擬機器主機
2.1 啟用虛擬機器主機的準備工作
2.1.1安裝httpd
[[email protected] httpd]# yum install httpd -y
2.1.2禁用預設的主機模式
[[email protected] httpd]# vim /etc/httpd/conf/httpd.conf注釋下面這行內容#DocumentRoot "/var/www/html"
2.2基於IP的虛擬機器主機配置
2.2.1為主機添加多個IP
[[email protected] conf.d]# ip addr show dev eth0 #查看原有IP
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:77:77:7d brd ff:ff:ff:ff:ff:ff
inet 192.168.137.200/24 brd 192.168.137.255 scope global eth0
inet6 fe80::20c:29ff:fe77:777d/64 scope link
valid_lft forever preferred_lft forever
[[email protected] conf.d]# ip addr add 192.168.137.201/24 dev eth0 #添加一個IP
[[email protected] conf.d]# ip addr show dev eth0 #查看添加後的IP資訊, 此時有2個IP地址了。 200,201
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:77:77:7d brd ff:ff:ff:ff:ff:ff
inet 192.168.137.200/24 brd 192.168.137.255 scope global eth0
inet 192.168.137.201/24 scope global secondary eth0
inet6 fe80::20c:29ff:fe77:777d/64 scope link
valid_lft forever preferred_lft forever
2.2.2添加虛擬機器主機設定檔
[[email protected] conf.d]# cd /etc/httpd/conf.d/ #進入配置目錄[[email protected] conf.d]# vim virtualhost.conf #建立一個設定檔, 編輯內容如下[[email protected] conf.d]# cat virtualhost.conf #查看並檢查設定檔<VirtualHost 192.168.137.200:80> DocumentRoot "/var/www/test200" ServerName www.test200.com</VirtualHost><VirtualHost 192.168.137.201:80> DocumentRoot "/var/www/test201" ServerName www.test201.com</VirtualHost>
[[email protected] conf.d]# cd /var/www #切換目錄[[email protected] www]# mkdir test200 test201 #建立目錄[[email protected] www]# echo test200 >>./test200/index.html #建立IP為200的首頁[[email protected] www]# echo test201 >>./test201/index.html #建立IP為200的首頁
2.2.3測試
[[email protected] www]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain for ServerName
[ OK ]
我們這裡使用elinks進行測試, 當然用瀏覽器測試是一樣的[[email protected] conf]# elinks -source 192.168.137.200test200[[email protected] conf]# elinks -source 192.168.137.201test201
2.3基於連接埠的虛擬機器主機配置
2.3.1在主設定檔添加監聽連接埠
[[email protected] conf]# vim /etc/httpd/conf/httpd.conf 在原有行Listen 80行的基礎上, 在添加一行Listen 8080
2.3.2添加8080的連接埠虛擬配置
[[email protected] conf.d]# cat virtualhost.conf <VirtualHost 192.168.137.200:80> DocumentRoot "/var/www/test200" ServerName www.test200.com</VirtualHost><VirtualHost 192.168.137.201:80> DocumentRoot "/var/www/test201" ServerName www.test201.com</VirtualHost>#下面的內容是在上面的配置的基礎上添加的。<VirtualHost 192.168.137.201:8080> DocumentRoot "/var/www/test201-8080" ServerName www.test201-8080.com</VirtualHost>
[[email protected] conf.d]# cd /var/www/ #切換目錄[[email protected] www]# mkdir test201-8080 #建立目錄[[email protected] www]# echo "test201-8080" >>./test201-8080/index.html #建立首頁
2.3.2測試
[[email protected] www]# service httpd restartStopping httpd: [ OK ]Starting httpd: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain for ServerName [ OK ][[email protected] conf]# elinks -source 192.168.137.201:80test201[[email protected] conf]# elinks -source 192.168.137.201test201[[email protected] conf]# elinks -source 192.168.137.201:8080test201-8080
2.4基於網域名稱的虛擬機器主機配置
2.4.1 添加網域名稱的虛擬機器主機配置
[[email protected] conf.d]# vim virtualhost.conf #編輯虛擬機器主機設定檔[[email protected] conf.d]# cat virtualhost.conf #內容如下, 紅色部分是在上面的基礎上添加的NameVirtualHost 192.168.137.200:80 <VirtualHost 192.168.137.200:80> DocumentRoot "/var/www/test200" ServerName www.test200.com</VirtualHost><VirtualHost 192.168.137.200:80> DocumentRoot "/var/www/test200net" ServerName www.test200.net</VirtualHost><VirtualHost 192.168.137.201:80> DocumentRoot "/var/www/test201" ServerName www.test201.com</VirtualHost><VirtualHost 192.168.137.201:8080> DocumentRoot "/var/www/test2018080" ServerName www.test2018080.com</VirtualHost>
[[email protected] conf.d]# !serservice httpd restartStopping httpd: [ OK ]Starting httpd: httpd: Could not reliably determine the server‘s fully qualified domain name, using localhost.localdomain for ServerName [ OK ]
[[email protected] conf.d]# cd /var/www #切換目錄[[email protected] www]# mkdir test200net #建立目錄[[email protected] www]# echo "test200net" >>./test200net/index.html #建立首頁
2.4.2 測試
2.4.2.1 添加網域名稱解析
這裡我們沒有提供dns去解析,簡單的使用hosts檔案區解析就可以了。
[[email protected] www]# vim /etc/hosts 編輯hosts檔案, 添加兩行[[email protected] www]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.137.200 www.test200.com192.168.137.200 www.test200.net
接下來就可以測試了
[[email protected] www]# elinks -source http://www.test200.com #測試.com域test200[[email protected] www]# elinks -source http://www.test200.net #測試.net域test200net
apache配置虛擬機器主機的三種方式