您好,登錄后才能下訂單哦!
在同一臺物理服務器中運行多個Web站點,其中每一一個站點并不獨立占用一臺真正的計算機。
(1)安裝bind、httpd服務。
(2)進入named服務的主配置文件,將下圖兩個位置改為“any”。
[root@localhost ~]# vim /etc/named.conf
(3)進入named服務的區域配置文件,添加兩個域名的區域信息。
[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "aaa.com" IN {
type master;
file "aaa.com.zone";
allow-update { none; };
};
zone "bbb.com" IN {
type master;
file "bbb.com.zone";
allow-update { none; };
};
(4)進入“/var/named/”目錄,保留權限復制一份“named.localhost”區域數據配置文件,命名為“aaa.com.zone”,然后對其進行修改。
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost aaa.com.zone
[root@localhost named]#
[root@localhost named]# vim aaa.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.52.133
(5)再保留權限復制一份“aaa.com.zone”文件,命名為“bbb.com.zone”,不用進行修改。然后開啟named服務,關閉防火墻和增強性安全功能。
[root@localhost named]# cp -p aaa.com.zone bbb.com.zone
[root@localhost named]# systemctl start named
[root@localhost named]#
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0
[root@localhost named]#
(6)再開一臺win10虛擬機,將其DNS服務器的IP地址,設置為剛才Linux系統的IP地址。
(7)用win10主機去測試DNS服務能否解析,解析成功。
(8)進入“/etc/httpd/conf”目錄,創建一個“extra/”目錄,然后進入“extra/”目錄,用vim編輯器,新建一個配置文件“vhost.conf”,在配置文件里輸入以下內容。
[root@localhost named]# cd /etc/httpd/conf
[root@localhost conf]# ls
httpd.conf magic
[root@localhost conf]# mkdir extra
[root@localhost conf]# ls
extra httpd.conf magic
[root@localhost conf]# cd extra/
[root@localhost extra]# vim vhost.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/aaa/"
ServerName www.aaa.com
ErrorLog "logs/www.aaa.com.error_log"
CustomLog "logs/www.aaa.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/bbb/"
ServerName www.bbb.com
ErrorLog "logs/www.bbb.com.error_log"
CustomLog "logs/www.bbb.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
(9)進入“/var/www/html/”創建兩個目錄“aaa/”、“bbb/”。
[root@localhost extra]#
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# mkdir aaa bbb
[root@localhost html]# ls
aaa bbb
[root@localhost html]#
(10)進入“aaa/”目錄,新建一個站點首頁文件,內容如下:
[root@localhost html]# cd aaa
[root@localhost aaa]# ls
[root@localhost aaa]# vim index.html
<h2>this is aaa web</h2>
(11)進入“bbb/”目錄,新建一個站點首頁文件,內容如下:
[root@localhost aaa]# cd ../bbb
[root@localhost bbb]# ls
[root@localhost bbb]# vim index.html
<h2>this is bbb web</h2>
(12)進入httpd服務的主配置文件,在末行將我們新建的配置文件寫進主配置文件,然后啟動httpd服務。
[root@localhost bbb]# vim /etc/httpd/conf/httpd.conf
Include conf/extra/vhost.conf
[root@localhost bbb]# systemctl start httpd
[root@localhost bbb]#
(13)用win10 主機去分別訪問兩個域名,都能訪問成功。
(1)在上一個實驗的基礎上,進入配置文件“vhost.conf”,添加一個“www.aaa.com”域名的8080端口。
[root@localhost bbb]# vim /etc/httpd/conf/extra/vhost.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/aaa/"
ServerName www.aaa.com
ErrorLog "logs/www.aaa.com.error_log"
CustomLog "logs/www.aaa.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/bbb/"
ServerName www.bbb.com
ErrorLog "logs/www.bbb.com.error_log"
CustomLog "logs/www.bbb.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/var/www/html/aaa02/"
ServerName www.aaa.com
ErrorLog "logs/www.aaa02.com.error_log"
CustomLog "logs/www.aaa02.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
(2)進入“/var/www/html”目錄,新建一個“aaa02”目錄,進入“aaa02”目錄,新建一個站點首頁文件,內容如下:
[root@localhost bbb]# cd ../
[root@localhost html]# mkdir aaa02
[root@localhost html]# cd aaa02/
[root@localhost aaa02]# vim index.html
<h2>this is aaa02 web</h2>
(3)進入httpd服務主配置文件,添加監聽端口,同時將IPv6的端口的監聽注銷。重啟httpd服務。
[root@localhost aaa02]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.52.133:80
Listen 192.168.52.133:8080
#Listen 80
[root@localhost aaa02]# systemctl restart httpd
[root@localhost aaa02]#
(4)再次用win10主機訪問兩個端口不同的域名,訪問成功。
(1)給Linux主機添加一塊網卡,查看IP地址。
(2)進入配置文件“vhost.conf”,進行如下輸入:
[root@localhost aaa02]# vim /etc/httpd/conf/extra/vhost.conf
<VirtualHost 192.168.52.133:80>
DocumentRoot "/var/www/html/aaa/"
ErrorLog "logs/www.aaa.com.error_log"
CustomLog "logs/www.aaa.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.52.139:80>
DocumentRoot "/var/www/html/aaa02/"
ErrorLog "logs/www.aaa02.com.error_log"
CustomLog "logs/www.aaa02.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
(3)分別對“aaa”站點與“aaa02”站點的主頁文件進行如下修改:
[root@localhost aaa02]# cd ../aaa
[root@localhost aaa]# vim index.html
<h2>this is 133 aaa web</h2>
[root@localhost aaa]# cd ../aaa02
[root@localhost aaa02]# vim index.html
<h2>this is 139 aaa02 web</h2>
(4)進入httpd主配置文件,進行端口的添加與注釋。然后重啟httpd服務。
[root@localhost aaa02]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.52.133:80
Listen 192.168.52.139:80
#Listen 192.168.52.133:8080
#Listen 80
[root@localhost aaa02]# systemctl restart httpd
[root@localhost aaa02]#
(5)用win10主機去訪問兩個不同IP地址的站點,訪問成功。但是只能用IP地址訪問,一般情況訪問網站用的是域名,接下來我們進行域名訪問不同IP地址站點的配置。
(6)首先在配置文件“vhost.conf”中添加,域名“ServerName”。
[root@localhost aaa02]# vim /etc/httpd/conf/extra/vhost.conf
<VirtualHost 192.168.52.133:80>
DocumentRoot "/var/www/html/aaa/"
ServerName www.aaa.com
ErrorLog "logs/www.aaa.com.error_log"
CustomLog "logs/www.aaa.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.52.139:80>
DocumentRoot "/var/www/html/aaa02/"
ServerName www.aaa02.com
ErrorLog "logs/www.aaa02.com.error_log"
CustomLog "logs/www.aaa02.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
(7)進入named服務的區域配置文件中,添加一個“aaa02”的區域信息。
[root@localhost aaa02]# vim /etc/named.rfc1912.zones
zone "aaa.com" IN {
type master;
file "aaa.com.zone";
allow-update { none; };
};
zone "aaa02.com" IN {
type master;
file "aaa02.com.zone";
allow-update { none; };
};
(8)進入“/var/named/”目錄,保留權限復制一份“aaa.com.zone”文件,命名為“aaa02.com.zone”,同時對其進行如下修改:
[root@localhost aaa02]# cd /var/named/
[root@localhost named]# ls
aaa.com.zone data named.ca named.localhost slaves
bbb.com.zone dynamic named.empty named.loopback
[root@localhost named]# cp -p aaa.com.zone aaa02.com.zone
[root@localhost named]# vim aaa02.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.52.139
(9)再次用win10主機,通過域名去訪問兩個不同IP地址的站點,訪問成功。
(1)查看win10主機的IP地址。
(2)在配置文件“vhost.conf”中添加拒絕win10主機訪問,“www.aaa.com”域名規則。然后重啟服務
[root@localhost ~]# vim /etc/httpd/conf/extra/vhost.conf
<VirtualHost 192.168.52.133:80>
DocumentRoot "/var/www/html/aaa/"
ServerName www.aaa.com
ErrorLog "logs/www.aaa.com.error_log"
CustomLog "logs/www.aaa.com.access_log" common
<Directory "/var/www/html">
<RequireAll>
Require not ip 192.168.52.129
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
<VirtualHost 192.168.52.139:80>
DocumentRoot "/var/www/html/aaa02/"
ServerName www.aaa02.com
ErrorLog "logs/www.aaa02.com.error_log"
CustomLog "logs/www.aaa02.com.access_log" common
<Directory "/var/www/html">
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# systemctl restart httpd
(3)清除win10主機瀏覽器的歷史記錄,然后分別訪問兩個域名。此時“www.aaa.com”域名已經訪問不到首頁了,只能看到Apache默認的頁面。
(4)用“htpasswd”命令創建用戶認證數據庫,“-c”選項是用來創建文件“passwd”的,如果文件已經存在就不用加。
[root@localhost ~]# cd /etc/httpd/conf
[root@localhost conf]# ls
extra httpd.conf magic
[root@localhost conf]#
[root@localhost conf]# htpasswd -c /etc/httpd/conf/passwd test01
New password:
Re-type new password:
Adding password for user test01
[root@localhost conf]# htpasswd /etc/httpd/conf/passwd test02
New password:
Re-type new password:
Adding password for user test02
[root@localhost conf]# cat passwd
test01:$apr1$72w08g5z$26fEl6Yqym/nPi08lhrYj/
test02:$apr1$1sZRVmZ/$Qs2BrdK/SJoZwRe1sIXUQ/
[root@localhost conf]#
(5)在配置文件“vhost.conf”中,給“www.aaa02.com”域名添加身份驗證訪問規則,然后重啟服務。
[root@localhost conf]# vim extra/vhost.conf
<VirtualHost 192.168.52.133:80>
DocumentRoot "/var/www/html/aaa/"
ServerName www.aaa.com
ErrorLog "logs/www.aaa.com.error_log"
CustomLog "logs/www.aaa.com.access_log" common
<Directory "/var/www/html">
<RequireAll>
Require not ip 192.168.52.129
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
<VirtualHost 192.168.52.139:80>
DocumentRoot "/var/www/html/aaa02/"
ServerName www.aaa02.com
ErrorLog "logs/www.aaa02.com.error_log"
CustomLog "logs/www.aaa02.com.access_log" common
<Directory "/var/www/html">
AuthName "DocumentRoot"
AuthType Basic
AuthUserFile /etc/httpd/conf/passwd
Require valid-user
</Directory>
</VirtualHost>
[root@localhost conf]# systemctl restart httpd
[root@localhost conf]#
(6)清除win10主機的瀏覽器緩存,然后再次訪問“www.aaa02.com”域名,結果彈出身份驗證。輸入用戶和密碼后訪問成功。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。