3、Apache基于域名的虛拟主機配置
使用基于域名的虛拟主機配置是比較流行的方式,可以在同一個IP上配置多個域名并且都通過80端口通路。
3.1 假設伺服器有個IP位址為192.168.2.155
[root@localhost ~]# ifconfig ens33:5 192.168.2.155/24 up
[root@localhost ~]# ifconfig
ens33:: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet . netmask . broadcast .
ether :0c::::ae txqueuelen (Ethernet)
3.2 192.168.2.155對應的域名如下,配置主機的host檔案便于測試
[[email protected] ~]# vim /etc/hosts
[[email protected] ~]# cat /etc/hosts|grep 192.168.2.155
www.oa.com
www.bbs.com
www.test.com
3.3 建立虛拟主機存放網頁的根目錄,并建立首頁檔案index.html
[root@localhost ~]# cd /data/www/
[root@localhost www]# mkdir www.oa.com
[root@localhost www]# mkdir www.bbs.com
[root@localhost www]# mkdir www.test.com
[root@localhost www]# echo www.oa.com > www.oa.com/index.html
[root@localhost www]# echo www.bbs.com > www.bbs.com/index.html
[root@localhost www]# echo www.test.com > www.test.com/index.html
3.4 修改httpd.conf 在檔案末尾加入以下配置項
[root@localhost www]# vim /usr/local/apache/conf/httpd.conf
Listen :
Include conf/vhost/*.conf
3.5 編輯每個域名的配置檔案
[[email protected] conf]# cd vhost/
[[email protected] vhost]# cat www.oa.com.conf
<VirtualHost :>
#主機名
ServerName www.oa.com
#配置通路跟目錄
DocumentRoot "/data/www/www.oa.com"
#這裡配置歡迎首頁面
DirectoryIndex index.html index.htm index.php
<Directory "/data/www/www.oa.com">
#伺服器允許在此目錄中使用符号連接配接
Options Indexes FollowSymLinks
#不允許别人修改我們的頁面
AllowOverride None
#設定通路權限
Require all granted
</Directory>
</VirtualHost>
[[email protected] vhost]# cat www.bbs.com.conf
<VirtualHost :>
#主機名
ServerName www.bbs.com
#配置通路跟目錄
DocumentRoot "/data/www/www.bbs.com"
#這裡配置歡迎首頁面
DirectoryIndex index.html index.htm index.php
<Directory "/data/www/www.bbs.com">
#伺服器允許在此目錄中使用符号連接配接
Options Indexes FollowSymLinks
#不允許别人修改我們的頁面
AllowOverride None
#設定通路權限
Require all granted
</Directory>
</VirtualHost>
[[email protected] vhost]# cat www.test.com.conf
<VirtualHost :>
#主機名
ServerName www.test.com
#配置通路跟目錄
DocumentRoot "/data/www/www.test.com"
#這裡配置歡迎首頁面
DirectoryIndex index.html index.htm index.php
<Directory "/data/www/www.test.com">
#伺服器允許在此目錄中使用符号連接配接
Options Indexes FollowSymLinks
#不允許别人修改我們的頁面
AllowOverride None
#設定通路權限
Require all granted
</Directory>
</VirtualHost>
[[email protected] vhost]# cat /data/www/www.oa.com/index.html
www.oa.com
[[email protected] vhost]# cat /data/www/www.bbs.com/index.html
www.bbs.com
[[email protected] vhost]# cat /data/www/www.test.com/index.html
www.test.com
3.6 配置完以後可以啟動Apache服務并進行測試,在浏覽器測試是同樣的效果
[[email protected] vhost]# /usr/local/apache/bin/apachectl -t
Syntax OK
[[email protected] vhost]# /usr/local/apache/bin/apachectl -k start
httpd (pid ) already running
[[email protected] vhost]# curl http://www.oa.com
www.oa.com
[[email protected] vhost]# curl http://www.bbs.com
www.bbs.com
[[email protected] vhost]# curl http://www.test.com
www.test.com