apache 虛擬目錄 不同主機名 不同端口
實驗目的:實現網頁多路訪問
實驗需求:服務端ip地址:192.168.100.10 訪問端ip地址:192.168.100.25(只要是100段的就行)
1 . 首先安裝httpd服務,進入配置文件進行配置
vim /etc/httpd/conf/httpd.conf
Listen 192.168.100.10:80 //監聽端口
#Listen 80 //ipv6註銷掉
ServerName www.benet.com:80 //主機名
2 .service httpd start //開啟服務
service iptables stop //
此時就可以訪問apache了 瀏覽器輸入192.168.100.10 可以看到首頁
3 .自定義網頁內容,並且重啟服務
4 .再次訪問apache,可以看到自定義的內容
5 . 此時我們來添加虛擬用戶
先進入到配置所在目錄 cd /etc/httpd/conf.d
vim vdir.conf //創建一個配置文件
Alias /test "/opt/test/" //定義一個名字,物理存放在opt
<Directory "/opt/test/"> //定義物理路徑
Options Indexes MultiViews FollowSymLinks //建立一個索引
AuthName "hello" //標誌性信息
authtype basic //認證類型,基本認證
authuserfile /etc/httpd/user //身份驗證賬戶文件
authgroupfile /etc/httpd/group //認證組
require Valid-user //開啟用戶認證
require user test //僅允許用戶登錄
Require group admin //僅允許組登錄
</Directory>
6 .mkdir /opt/test //在opt創建一個文件,要與上面相同
7 . htpasswd -c /etc/httpd/user li //創建一個li用戶
service httpd restart //重啟服務
8 . 瀏覽器輸入192.168.100.10/test 進入測試
這時就需要輸入用戶及密碼才能進入網頁
9 . 下面設置不同主機名訪問
vim host.conf
NameVirtualHost 192.168.100.10:80 //指定一個ip地址與相對應端口
<VirtualHost 192.168.100.10:80> //
ServerAdmin [email protected] //建立一個管理員郵箱
DocumentRoot /opt/benet/ //定義站點目錄
ServerName www.benet.com //服務器名稱
ErrorLog logs/benet.com-error_log //兩個日誌,錯誤日誌
CustomLog logs/benet.com-access_log common //訪問日誌
</VirtualHost>
<VirtualHost 192.168.100.10:80>
ServerAdmin [email protected]
DocumentRoot /opt/accp/
ServerName www.accp.com
ErrorLog logs/accp.com-error_log
CustomLog logs/accp.com-access_log common
</VirtualHost> //定義另一個網頁
10 .mkdir /opt/benet //創建文件
mkdir /opt/accp //
echo "this is benet" > /opt/benet/index.html //定義網頁
echo "this is accp" > /opt/accp/index.html //
11 .rpm -ivh /mnt/Packages/bind-9.8.2-0.17.rc1.el6_4.6.x86_64.rpm //DNS解析
12 .vim /etc/named.conf
listen-on port 53 { 192.168.100.10; }; //監聽端口
....
allow-query { any; }; //允許所有訪問
13 . vim /etc/named.rfc1912.zones
zone "benet.com" IN { //解析benet.com
type master;
file "benet.com.zone"; //工作文件為benet.com.zone
allow-update { none; };
};
zone "accp.com" IN { //
type master;
file "accp.com.zone"; //
allow-update { none; };
};
14 .cd /var/named/
cp -p named.localhost benet.com.zone
vim benet.com.zone
@ IN SOA @ admin.benet.com. ( //管理員主機名
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 192.168.100.10
www IN A 192.168.100.10 //解析網址
同樣設置accp
15 .service named start
service httpd restart //開啟服務
瀏覽器測試 www.benet.com www.accp.com
16 . 下面再進行不同端口測試
vim /etc/httpd/conf.d/host.conf
在最底部添加
NameVirtualHost 192.168.100.10:8080
<VirtualHost 192.168.100.10:8080>
ServerAdmin [email protected]
DocumentRoot /opt/benet01/
ServerName www.benet.com
ErrorLog logs/benet.com-error_log
CustomLog logs/benet.com-access_log common
</VirtualHost>
17 .cd /etc/httpd/conf.d/
mkdir /opt/benet01
echo "this is benet8080" > /opt/benet01/index.html //定義網頁內容
18 . vim /etc/httpd/conf/httpd.conf
Listen 192.168.100.10:80
Listen 192.168.100.10:8080 //監聽8080端口
#Listen 80
18 . service httpd restart //重啟服務
瀏覽器輸入192.168.100.10:8080
測試完成
apache 虛擬目錄 不同主機名 不同端口