apache配置虛擬主機的三種方式


首先需要在yum倉庫中下載apache服務命令如下  yum install httpd*

基於IP

使用nmtui命令或者在設置里面添加多個ip地址(192.168.21.200/210/220)

重啟網卡  systemctl restart network

創建網站數據目錄

在/home/wwwroot目錄下分別創建三個網站目錄  mkdir -p /home/wwwroot/{00,10,20}

在這三個網站數據目錄中寫入主頁文件,內容為改網站的ip地址

echo "192.168.21.200" > /home/wwwroot/00/index.html  echo "192.168.21.210" > /home/wwwroot/10/index.html  echo "192.168.21.220" > /home/wwwroot/20/index.html

在/etc/httpd/conf.d 中創建一個配置文件  xxx.conf

內容為

<VirtualHost 192.168.21.200>
DocumentRoot /home/wwwroot/00
ServerName www.linuxprobe.com
<Directory /home/wwwroot/00 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.21.210>
DocumentRoot /home/wwwroot/00
ServerName bbs.linuxprobe.com
<Directory /home/wwwroot/10 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.21.220>
DocumentRoot /home/wwwroot/00
ServerName tech.linuxprobe.com
<Directory /home/wwwroot/20 >
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

重啟一下服務  service httpd restart

分別訪問一下驗證結果

完成本次實驗之后請還原虛擬機在進行以下實驗

圖片如下

 

 

 

 

 

 

 

 

 

 

 

 基於主機名

配置hosts文件  vi /etc/hosts

每行只能寫一條

192.168.21.200 www.linuxprobe.com  192.168.21.200 bbs.linuxprobe.com  192.168.21.200 tech.linuxprobe.com

 創建網站數據目錄

mkdir -p /home/wwwroot/{www,bbs,tech}

分別寫入不同的首頁文件

echo "WWW" > /home/wwwroot/www/index.html  echo "BBS" > /home/wwwroot/www/index.html   echo "TECH" > /home/wwwroot/www/index.html

在配置文件(/etc/httpd/conf/httpd.conf)中描述基於主機名稱的虛擬主機(在配置文件的末尾加入)

<VirtualHost 192.168.21.200>

DocumentRoot "/home/wwwroot/www"

ServerName "www.linuxprobe.com"

<Directory "/home/wwwroot/www">

AllowOverride None

Require all granted

</directory>

</VirtualHost>

<VirtualHost 192.168.21.200>

DocumentRoot "/home/wwwroot/www"

ServerName "bbs.linuxprobe.com"

<Directory "/home/wwwroot/bbs">

AllowOverride None

Require all granted

</directory>

</VirtualHost>

<VirtualHost 192.168.21.200>

DocumentRoot "/home/wwwroot/tech"

ServerName "tech.linuxprobe.com"

<Directory "/home/wwwroot/tech">

AllowOverride None

Require all granted

</directory>

</VirtualHost>

重新啟動服務  service httpd restart

完成本實驗后請還原虛擬機快照到下一個實驗,避免配置文件沖突

圖示如下

 

 

 

 

 

 

 

 

 

 

 

 

基於端口號

 

創建網站數據目錄  mkdir -p /home/wwwroot/{6111,6222}

寫入不同的內容的主頁文件 echo "port:6111" > /home/wwwroot/6111/index.html  echo "port:6222" > /home/wwwroot/6222/index.html

在配置文件中(/etc/httpd/conf/httpd.conf)描述虛擬主機

在42行的Listen 80 ,並在下面加入  Listen 6111  Listen 6222

在某尾定義虛擬主機信息

 <VirtualHost 192.168.21.200:6111>
DocumentRoot “/home/wwwroot/6111”
ServerName www.linuxprobe.com
<Directory “/home/wwwroot/6111”>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.21.200:6222>
DocumentRoot “/home/wwwroot/6222”
ServerName bbs.linuxprobe.com
<Directory “/home/wwwroot/6222”>
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

重新啟動服務

完成本實驗后請還原虛擬機快照在進行下一個實驗

圖示如下

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM