您好,登錄后才能下訂單哦!
1.在同一-臺物理服務器中運行多個Web站點,其中每一個站點并不獨立占用一臺真正的計算機
1.基于域名的虛擬主機(企業(yè)常用)
2.基于IP地址的虛擬主機
3.基于端口的虛擬主機
大型公司實例項目:
兩臺物理主機(A/B)
兩臺主機中分別有兩個虛擬站點
主機A和B中都有accp網(wǎng)站和kgc網(wǎng)站
網(wǎng)站內(nèi)容完全相同,對外提供業(yè)務時,既用到了合理分配,也起到了雙機熱備
學習一臺主機架構(gòu)兩個網(wǎng)站
1.構(gòu)建2個虛擬Web站點
www.kgc.com, IP地址為173.17.17.11
www.kcce.com, IP地址為173.17.17.11
2.在瀏覽器中訪問這兩個域名時,分別顯示不同的內(nèi)容
[root@dnssvr ~]# vim /var/named/chroot/var/named/kgc.com.zone
@ IN NS dnssvr.kgc.com.
dnssvr IN A 173.1 7.1 7.2
www IN A 173.17.17.11
[root@dnssvr ~]# vim /var/named/chroot/var/named/kcce.com.zone
@ IN NS dnssvr.kgc.com.
www IN A 1 73.1 7.1 7.1 1
[root@www ~]# mkdir -p /var/www/html/kgccom
[root@www ~]# mkdir -p /var/www/html/kccecom
[root@www ~]# echo "<h2 > www.kgc.com</h2>" >
/var/www/html/kgccom/index.html
[root@www ~]# echo "<h2 > www.kcce.com </h2>" >
/var/www/html/kccecom/index.html
[root@www ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/kgccom"
ServerName www.kgc.com
ErrorLog "logs/www.kgc.com.error_log"
CustomLog "logs/www.kgc.com.access_log" common
< Directory "/var/www/html" >
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
<VirtualHost>
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.56.131 netmask 255.255.255.0 broadcast 192.168.56.255
inet6 fe80::a7c6:cf70:62f2:faf0 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:86:f9:b2 txqueuelen 1000 (Ethernet)
RX packets 667004 bytes 977833518 (932.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 165563 bytes 10169247 (9.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.56.134 netmask 255.255.255.0 broadcast 192.168.56.255
inet6 fe80::9ce4:351a:2c74:9ad5 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:86:f9:bc txqueuelen 1000 (Ethernet)
RX packets 216 bytes 17516 (17.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 4391 (4.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
//目前主機中有兩張網(wǎng)卡:ens33和ens36,地址分別為:192.168.56.131和 192.168.56.134
[root@localhost ~]# yum install bind httpd -y
[root@localhost ~]# vim /etc/named.conf
options {
listen-on port 53 { any; }; //其中的127.0.0.1改為any
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; //其中的locahost改為any
修改完成后按Esc,輸入:wq保存退出
[root@localhost ~]# vim /etc/named.rfc1912.zones
//找到以下格式,按5yy復制五行,在任意一個5行的最后一行按o在下一行插入,修改其中域名
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
};
修改完成后按Esc,輸入:wq保存退出
[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 accp.com.zone
[root@localhost named]# vim accp.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.56.131 //此處為添加的主機A記錄,地址指向自己
//需要刪除IPv6的解析
修改完成后按Esc,輸入:wq保存退出
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
[root@localhost named]# vim kgc.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.56.131
//此處的文件內(nèi)容不需要做任何修改
可直接輸入:q退出
//為方便后面測試解析,接下來我們啟動服務,并關(guān)閉防火墻及安全功能
[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0
此處我們使用一臺win7虛擬機進行DNS的解析:
先需要在網(wǎng)絡的更改適配器設置中對IPv4的DNS地址進行修改:192.168.56.131
//此處修改的是DNS主機的地址
再使用管理員身份運行cmd命令:
C:\Windows\system32>nslookup www.accp.com
服務器: UnKnown
Address: 192.168.56.131
名稱: www.accp.com
Address: 192.168.56.131
C:\Windows\system32>nslookup www.kgc.com
服務器: UnKnown
Address: 192.168.56.131
名稱: www.kgc.com
Address: 192.168.56.131
//此時通過驗證表明DNS的解析是沒有問題的,接下來我們就可以做網(wǎng)站了
[root@localhost named]# cd /etc/httpd
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# cd 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/accp/" //站點目錄
ServerName www.accp.com //域名
ErrorLog "logs/www.accp.com.error_log" //錯誤日志
CustomLog "logs/www.accp.com.access_log" common //訪問日志
<Directory "/var/www/html/">
Require all granted //訪問權(quán)限為允許所有
</Directory>
</VirtualHost>
按Esc退出插入模式,輸入:11,19 s/accp/kgc/g //把從11行到19行把其中的accp改成kgc,做全局替換
11 <VirtualHost *:80>
12 DocumentRoot "/var/www/html/kgc/"
13 ServerName www.kgc.com
14 ErrorLog "logs/www.kgc.com.error_log"
15 CustomLog "logs/www.kgc.com.access_log" common
16 <Directory "/var/www/html/">
17 Require all granted
18 </Directory>
19 </VirtualHost>
修改完成后按Esc,輸入:wq保存退出
[root@localhost extra]# cd ../../
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# pwd
/etc/httpd //參考點,以工作目錄為參考
[root@localhost httpd]# ls -l
總用量 0
drwxr-xr-x. 3 root root 50 10月 22 22:36 conf
drwxr-xr-x. 2 root root 82 10月 22 21:53 conf.d
drwxr-xr-x. 2 root root 146 10月 22 21:53 conf.modules.d
lrwxrwxrwx. 1 root root 19 10月 22 21:53 logs -> ../../var/log/httpd //軟鏈接在此目錄下
lrwxrwxrwx. 1 root root 29 10月 22 21:53 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root 10 10月 22 21:53 run -> /run/httpd
[root@localhost httpd]# cd /var/log/httpd/
[root@localhost httpd]# ls
//此時是空的,因為沒有啟動,所以沒有日志文件
[root@localhost httpd]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# mkdir accp kgc
[root@localhost html]# ls
accp kgc
[root@localhost html]# cd accp/
[root@localhost accp]# ls
[root@localhost accp]# vim index.html
<h2>this is accp web</h2>
輸入完成后按Esc,輸入:wq保存退出
[root@localhost accp]# cd ../kgc/
[root@localhost kgc]# vim index.html
<h2>this is kgc web</h2>
輸入完成后按Esc,輸入:wq保存退出
在主配置文件中的包含語句需要進行檢查,因為之前的寫入內(nèi)容未在主配置文件中進行聲明:
[root@localhost kgc]# cd /etc/httpd/
[root@localhost httpd]# cd conf/extra/
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# pwd
/etc/httpd/conf/extra
[root@localhost extra]# cd ../../
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# pwd
/etc/httpd
[root@localhost httpd]# cd conf
[root@localhost conf]# ls
extra httpd.conf magic
[root@localhost conf]# vim httpd.conf
//按大寫字母G到末行,按o在下行插入
Include conf/extra/vhost.conf
輸入完成后按Esc,輸入:wq保存退出
[root@localhost extra]# systemctl start httpd
——————————————————————————————————————————————————
1.構(gòu)建兩個個虛擬Web站點
www.kcce.com, IP地址、端口為173.17.17.11:80
www.kcce.com, IP地址、端口為173.17.17.11:8353
2.在瀏覽器中訪問這兩個端口時,分別顯示不同的內(nèi)容
[root@www ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
< VirtualHost 173.1 7.17.11:80>
DocumentRoot "/var/www/htm/kccecom"
ServerName www.kcce.com
</VirtualHost>
<VirtualHost 173.17.17.11:8353>
DocumentRoot "/var/www/htm/kccepad"
ServerName www.kcce.com
</virtualHost>
Listen 80
Listen 8353
#監(jiān)聽端口寫在主配置文件中
[root@localhost extra]# vim vhost.conf
<VirtualHost *:8080>
DocumentRoot "/var/www/html/kgc02/"
ServerName www.kgc.com
ErrorLog "logs/www.kgc02.com.error_log"
CustomLog "logs/www.kgc02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
輸入完成后按Esc,輸入:wq保存退出
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# ls
accp kgc
[root@localhost html]# mkdir kgc02
[root@localhost html]# ls
accp kgc kgc02
[root@localhost html]# cd kgc02/
[root@localhost kgc02]# vim index.html
<h2>this is kgc02 web</h2>
輸入完成后按Esc,輸入:wq保存退出
[root@localhost kgc02]# vim /etc/httpd/conf/httpd.conf
輸入:/Listen查到到監(jiān)聽端口位置
Listen 192.168.56.131:80
Listen 192.168.56.131:8080
#Listen 80
此處需要做的更改的是把IPv6的監(jiān)聽給注釋關(guān)閉,同時把IPv4的監(jiān)聽地址更換為自己Linux的IP地址,使用yy復制,在下行粘貼,將端口改為8080
#修改完成后按Esc,輸入:wq保存退出
[root@localhost kgc02]# systemctl restart httpd //重啟服務
[root@localhost kgc02]# netstat -ntap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.56.131:8080 0.0.0.0:* LISTEN 52732/httpd
tcp 0 0 192.168.56.131:80 0.0.0.0:* LISTEN 52732/httpd
#此時可以看到兩個端口都處于監(jiān)聽狀態(tài)
此時顯示的就是我們預先寫好的8080端口的kgc02的網(wǎng)頁主頁
如果輸入:www.kgc.com:80,那就還顯示this is kgc web的界面
——————————————————————————————————————————————————
此前我們有兩張網(wǎng)卡地址分別為:192.168.56.131和192.168.56.134,此處需要用上
[root@localhost kgc02]# cd /etc/httpd/conf/extra/
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf
//11,29 s/^/#/g //全局把11行到29行進行注釋,此處不需要使用
<VirtualHost 192.168.56.131:80> //此處將*號修改為第一張網(wǎng)卡的IP
DocumentRoot "/var/www/html/accp/"
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.56.134:80> //此處將*號改為第二張網(wǎng)卡的IP,并將下面的accp改為accp02以作區(qū)分
DocumentRoot "/var/www/html/accp02/"
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
#修改完成后按Esc,輸入:wq保存退出
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir accp02
[root@localhost html]# cd accp02/
[root@localhost accp02]# vim index.html
<h2>this is 134 accp02 web</h2>
#輸入完成后按Esc,輸入:wq保存退出
[root@localhost accp02]# vim ../accp/index.html
<h2>this is 131 accp web</h2>
#修改完成后按Esc,輸入:wq保存退出
[root@localhost accp02]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.56.131:80
Listen 192.168.56.134:80
#Listen 192.168.56.131:8080
把8080端口進行注釋,并添加一行192.168.56.134的80端口監(jiān)聽
#修改完成后按Esc,輸入:wq保存退出
[root@localhost accp02]# systemctl restart httpd
[root@localhost accp02]# cd /etc/httpd/conf/extra/
[root@localhost extra]# vim vhost.conf
<VirtualHost 192.168.56.131:80>
DocumentRoot "/var/www/html/accp/"
ServerName www.accp.com
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.56.134:80>
DocumentRoot "/var/www/html/accp02/"
ServerName www.naccp.com
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
#以上兩段需要添加ServerName
[root@localhost extra]# vim /etc/named.rfc1912.zones
zone "naccp.com" IN {
type master;
file "naccp.com.zone";
allow-update { none; };
};
#添加以上內(nèi)容
#修改完成后按Esc,輸入:wq保存退出
[root@localhost extra]# cd /var/named/
[root@localhost named]# cp -p accp.com.zone naccp.com.zone
[root@localhost named]# vim naccp.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.56.134 //IP地址改為二網(wǎng)卡的地址
#修改完成后按Esc,輸入:wq保存退出
[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart httpd //重啟服務
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。