How to setup Virtual Host/Directory in Apache 2.2 ?
Virtual Host/Direcroty(虛擬主機/目錄)在 Apache 中是個很常用的功能
基本上常用的 Web Server (如 IIS)都有支援
他的好處是可以用不同的 DNS 去對應不同的目錄
這樣一來,一台機器就可以架上數十個甚至上百個站
Apache 的 Virtual Host 分成兩種:
IP-based virtual hosts use the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host. With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.
Apache 2.2 在設定上檢查更為嚴謹Name-based virtual hosting is usually simpler, since you need only configure your DNS server to map each hostname to the correct IP address and then configure the Apache HTTP Server to recognize the different hostnames. Name-based virtual hosting also eases the demand for scarce IP addresses. Therefore you should use name-based virtual hosting unless there is a specific reason to choose IP-based virtual hosting. Some reasons why you might consider using IP-based virtual hosting:
- Some ancient clients are not compatible with name-based virtual hosting. For name-based virtual hosting to work, the client must send the HTTP Host header. This is required by HTTP/1.1, and is implemented by all modern HTTP/1.0 browsers as an extension. If you need to support obsolete clients and still use name-based virtual hosting, a possible technique is discussed at the end of this document.
- Name-based virtual hosting cannot be used with SSL secure servers because of the nature of the SSL protocol.
- Some operating systems and network equipment implement bandwidth management techniques that cannot differentiate between hosts unless they are on separate IP addresses.
如果沒設定好 ServerName 可能會造成 Apache 啟動不了
而 FreeBSD 在 hosts 裡面預設沒定義 localhost
所以如果你設定的機器沒有被指派 DNS 的話
就要自己設定本機才有效的 hosts
# vi /etc/hosts設定好 hosts 就可以到 Apache 設定檔中設定 ServerName
127.0.0.1 localhost
127.0.0.2 virtual
設定 Virtual Directory 的重點:
- 編輯 /etc/usr/local/apache22 下的 httpd.conf
把 #Include conf/extra/httpd-vhosts.conf 的 # 拿掉 - 並修改 extra 下的 httpd-vhosts-conf
(如果只有一個 Virtual Host,請把其中一組拿掉或 mark 起來)
設定好 <VirtualHost></VirtualHost>中資料即可
請參考下面的設定 - 修改完以上設定請記得重開 Apache
# /usr/local/etc/rc.d/apache22 restart
如果你只有一個目錄放置網站,那可以直接使用 httpd.conf 的設定,不需使用 Virtual Directory;假使有多個網站一起掛在同一台機器上(不論 IP 有多少),請使用 Virtual Directory,這時候 httpd.conf 中的
另外自從 Apache 2.2 起,設定 Virtual Directory 需要設定目錄權限
NameVirtualHost 127.0.0.2:80
<VirtualHost 127.0.0.2:80><Directory /home/wwwroot2>
Order allow,deny
Allow from all
</Directory>
DocumentRoot /home/wwwroot2
ServerName virtual
ErrorLog logs/dummy-virtual-error_log
CustomLog logs/dummy-virtual-access_log common
</VirtualHost>
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80><Directory /home/wwwroot>
AllowOverride All
Options Indexes SymLinksIfOwnerMatch IncludesNoExec
</Directory>
DocumentRoot /home/wwwroot
ServerName localhost
ErrorLog logs/dummy-localhost-error_log
CustomLog logs/dummy-localhost-access_log common
</VirtualHost>
請注意上述綠色的部份
另外還要注意一下上面的 ErrorLog 跟 CustomLog 目錄
目錄是以 logs 開頭的
實際上對應到的目錄是 /var/log/logs
理論上 FreeBSD 預設沒這個目錄
所以可能要手動將上面的路徑改一下
或 mkdir logs
操作環境:
FreeBSD 6.2-Release i386
Apache 2.2.3
PHP 5.1.6
參考資料:
how to set a virtual host directory in apache2
樂咖黑電腦學習網 SEO Google Apps :: client denied by server configuration: C:/web/hoyo/index.php
Apache Virtual Host documentation
Comments