您好,登錄后才能下訂單哦!
在同一臺服務器中運行多個Web站點,其中每一個站點并不獨立
占用一臺真正的計算機
1.基于域名的虛擬主機
2.基于IP地址的虛擬主機
3.基于端口的虛擬主機
1.安裝DNS的軟件包bind和httpd的服務,用以測試
[root@localhost ~]# yum install bind httpd -y
...
2.為虛擬主機提供域名解析
[root@localhost ~]# vim /etc/named.conf //進入主配置文件
options {
listen-on port 53 { any; }; //將原有的127.0.0.1替換成any
...
allow-query { any; }; //將localhost替換成any
[root@localhost ~]# vim /etc/named.rfc1912.zones //進入?yún)^(qū)域配置文件
zone "accp.com" IN { //第一個正向域名
type master;
file "accp.com.zone"; //指向區(qū)域數(shù)據(jù)配置文件accp.com.zone
allow-update { none; };
};
zone "kgc.com" IN { //第二個正向域名
type master;
file "kgc.com.zone"; //指向區(qū)域數(shù)據(jù)配置文件kgc.com.zone
allow-update { none; };
};
[root@localhost ~]# cd /var/named/ //進入/var/named/目錄
[root@localhost named]# cp -p named.localhost accp.com.zone //復制模板文件named.localhost為accp.com.zone的區(qū)域數(shù)據(jù)配置文件
[root@localhost named]# vim accp.com.zone //編輯區(qū)域數(shù)據(jù)配置文件
$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.235.129 //添加解析A記錄,并指向本主機IP地址
[root@localhost named]# cp -p accp.com.zone kgc.com.zone //復制accp.com.zone為kgc.com.zone的區(qū)域數(shù)據(jù)配置文件,無需進行修改
[root@localhost named]# systemctl start named //啟動域名解析服務
[root@localhost named]# systemctl stop firewalld.service //關閉防火墻
[root@localhost named]# setenforce 0 //關閉增強型安全功能
1.修改客戶機的DNS服務器地址為虛擬主機的地址
2.打開客戶機的cmd命令提示符,使用nslookup命令加上域名即可獲取解析到的地址
1.配置虛擬主機的配置文件
[root@localhost named]# cd /etc/httpd/conf //進入配置文件的conf目錄
[root@localhost conf]# mkdir extra //創(chuàng)建擴展目錄
[root@localhost conf]# cd extra/ //進入目錄
[root@localhost extra]# vim vhost.conf //編輯虛擬主機配置文件,可自行定義
<VirtualHost *:80> //標簽 *所有ip 80端口
DocumentRoot "/var/www/html/accp/" //站點目錄
ServerName www.accp.com //站點域名
ErrorLog "logs/www. accp.com.error_log" //站點錯誤日志文件,logs為/var/log/httpd的軟鏈接(相對路徑)
CustomLog "logs/www.accp.com.access_1og" common //站點訪問日志
<Directory "/var/www/html/"> //控制目錄權限
Require all granted //允許所有訪問
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/ www/ html/kgc/"
ServerName www. kgc. com
ErrorLog "logs/www.kgc.com.error_1og"
CustomLog "logs/www.kgc.com.access_log” common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
2.配置網(wǎng)頁
[root@localhost extra]# cd /var/www/html/ //進入站點目錄
[root@localhost html]# mkdir accp kgc
[root@localhost html]# cd accp/
[root@localhost accp]# vim index.html
<h2>this is accp web</h2>
[root@localhost accp]# cd ../kgc/
[root@localhost kgc]# vim index.html
<h2>this is kgc web</h2>
3.進入httpd.conf配置文件,聲明包含先前創(chuàng)建的虛擬主機子配置文件
[root@localhost kgc]# cd /etc/httpd/conf
[root@localhost conf]# vim httpd.conf
...
Include conf/extra/vhost.conf //在末行接入此條目,用以聲明包含虛擬主機子配置文件
[root@localhost extra]# systemctl start httpd //啟動httpd服務
1.編輯虛擬主機配置文件
[root@localhost extra]# vim vhost.conf
... //省略之前設定好得部分配置
<VirtualHost *:8080> //添加accp域名的8080端口
DocumentRoot "/var/www/html/accp02"
ServerName www.accp.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>
2.創(chuàng)建新建的8080端口的網(wǎng)頁站點
[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 accp02 test web</h2>
3.修改httpd.conf配置文件的監(jiān)聽地址
vim /etc/httpd/conf/httpd.conf //進入配置文件
...
Listen 192.168.235.129:80
Listen 192.168.235.129:8080 //找到Listen條目并添加8080端口監(jiān)聽項
[root@localhost accp02]# systemctl restart httpd //重啟服務
4.使用客戶機網(wǎng)頁
1.首先添加第二塊網(wǎng)卡,然后他會自動獲取到一個IP地址
2.配置虛擬主機的配置文件
[root@localhost accp02]# cd /etc/httpd/conf/extra/
[root@localhost extra]# ls
vhost.conf
[root@localhost extra]# vim vhost.conf
1 <VirtualHost 192.168.235.129:80>
2 DocumentRoot "/var/www/html/accp/"
3 ServerName www.accp.com
4 ErrorLog "logs/www.accp.com.error_log"
5 CustomLog "logs/www.accp.com.access_1og" common
6 <Directory "/var/www/html/">
7 Require all granted
8 </Directory>
9 </VirtualHost>
10
11 <VirtualHost 192.168.235.142:80>
12 DocumentRoot "/var/www/html/accp02"
13 ServerName www.accp.com
14 ErrorLog "logs/www.accp02.com.error_log"
15 CustomLog "logs/www.accp02.com.access_log" common
16 <Directory "/var/www/html/">
17 Require all granted
18 </Directory>
19 </VirtualHost>
3.修改httpd.conf配置文件的監(jiān)聽地址
[root@localhost extra]# vim /etc/httpd/conf/httpd.conf
...
Listen 192.168.235.129:80
Listen 192.168.235.142:80 //添加另一個IP的監(jiān)聽
[root@localhost extra]# systemctl restart httpd //重啟服務
4.使用客戶機網(wǎng)頁
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。