Apache 所支援的虛擬主機有兩種:
1.IP-based 2.Name-based
從網路上搜尋虛擬主機設定方法,會分這兩類的介紹
很多網友做虛擬主機的設定,好像都要搭配 DNS server 來做,讓紅也一度以為非用 DNS Server 不可?
搜了搜資料,原來還是可以不用 DNS Server 的!目前紅所實做的是 Name-based 的 virtual hosts ,Name-based 是指多個虛擬主機在同一個 IP 上
實驗環境:Debian4.0r0
apache package and version:apache2-mpm-itk
Package descript:multiuser MPM for Apache 2.2
The ITK Multi-Processing Module (MPM) works in about the same way as the classical "prefork" module (that is, without threads), except that it allows you to constrain each individual vhost ato a particular system user. This allows you to run several different web sites on a single server without worrying that they will be able to read each others' files.
例如:
a01.edu.tw 初始的apache 網頁根目錄
b02.edu.tw 指向 / home / b02 / public_html 目錄
都同時指向 100.100.100.10 的 IP ,但是兩個 DNS 分別指向不同的網頁根目錄
對於a01.edu.tw 的設定,用原始設定的檔案,/etc/apache2/sites-available/default 設定檔內容:
#主目錄設定檔中才能有 NameVirtualHost * 其他的虛擬主機設定中需刪除
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
對於b02.edu.tw 的設定,紅使用DNS同名的檔名作為辨別, /etc/apache2/sites-available/b02.edu.tw.conf 設定檔內容(在apache2.conf中有設定會自動掃描 /etc/apache2/sites-available/ 裡所有的設定檔,所以只要把設定好的檔案放入即可):
<VirtualHost *>
ServerAdmin webmaster@localhost
Servername b02.edu.tw
DocumentRoot /home/b02/public_html
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /home/red/public_html>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
=============================================================================
Name-based 的虛擬主機要在<VirtualHost> 中進行設定,以上的兩個例子就可以在同一個IP同時可用兩組以上的DNS 去指向,若要三組就再複製使用囉
以下規納幾個重點:
1.Name-based 與IP-based 一次只能使用其中一種,兩組設定不能並存
2.IP-based 可以同時處理多個 SSL 站台連線,Name-based 似乎不行?(這方面要去確認一下)(感謝 Ryanny 幫忙查證文件:下方訊息回覆處)
3. name-based 的 <VirtualHost> 一定要寫成 <VirtualHost *> ,如果不止監聽80 port 可以在 * 後加要監聽的 port ,例:<VirtualHost *:443>
4.主目錄設定檔中沒有 Servername xxxx 的設定
=============================================================================
在 /etc/apache2/sites-available/ 中的兩個設定檔寫好之後,最後要設定符號鍵結
Debian 中 /etc/apache2/sites-available/default 預設已經有在 /etc/apache2/sites-enable/ 中鍵結
現在要再手動鍵結新的虛擬主機
#cd /etc/apache2/sites-enable/
將鍵結建立在 /etc/apache2/sites-enable/ 中
#ln -s /etc/apache2/sites-available/a01.edu.tw ./100-a01.edu.tw
(2007.06.12 錯誤修正,上方的指令,原為 ln 先前錯打為 ls)
Apache 重開後,就可以使用新一個的虛擬主機了
#/usr/sbin/apache2ctl restart
本篇資料參考:給你看網頁 seety 的apache 虛擬主機設定
紅的測試有成功,才將此過程記錄下來,若有錯誤,歡迎指教告知錯誤~~ @@"





1. Comment by Ryanny
11/六月/2007 at 11:48 下午
關於你的問題:
2.IP-based 可以同時處理多個 SSL 站台連線,Name-based 似乎不行?(這方面要去確認一下)
資料如下:
Name-based virtual hosting cannot be used with SSL secure servers because of the nature of the SSL protocol.
所以基本上,Name-based 是不能與 SSL 撘配。
參考網址:
http://httpd.apache.org/docs/2.0/vhosts/name-based.html
2. Comment by Red
13/六月/2007 at 10:32 下午
感謝 Ryanny 幫忙查證資料~~