IT_man 發表於 2014-10-3 10:29:29

Apache 如何設定多個虛擬伺服器(Virtual Server)

我的環境是<Linux CENTOS 6 + Apache + mysql>〖方法一〗1. vi /etc/httpd/httpd.conf
2. 找到 # VirtualHost example:
其下加入你的virtual server 設定,例如:
#2014/10/02 add
<VirtualHost 192.168.1.12:80>#或 <VirtualHost 127.0.0.1:80>#符號為註解
    ServerAdmin root@av4u.com
    DocumentRoot /home/coav4u/public_html
    ServerName av4u.co
    ErrorLog logs/www.av4u.co-error_log
    CustomLog logs/www.av4u.co-access_log common
</VirtualHost>
<VirtualHost 192.168.1.12:80>
    ServerAdmin root@av4u.com
    DocumentRoot /home/coav4u/public_html
    ServerName www.av4u.co
    ErrorLog logs/www.av4u.co-error_log
    CustomLog logs/www.av4u.co-access_log common
</VirtualHost>   可同時指定2個 Domain name (av4u.com及www.av4u.com)到同一個虛擬空間,這個部分似乎比 nginx 設定稍微麻煩一點
3. 重啟 Apache 才生效
    service httpd restart #須以root身分   --->搞定
   註: service httpd start#啟動      
      service httpd start#停止
〖方法二〗
在/etc/httpd/conf.d/新ˋ建一獨立檔(設定一個virtual host)
例如:
virtual1.conf
<VirtualHost 192.168.1.12:80>#或 <VirtualHost 127.0.0.1:80>#符號為註解
    ServerAdmin root@av4u.com
    DocumentRoot /home/coav4u/public_html
    ServerName av4u.co
    ErrorLog logs/www.av4u.co-error_log
    CustomLog logs/www.av4u.co-access_log common
</VirtualHost>virtual2.conf
<VirtualHost 192.168.1.12:80>
    ServerAdmin root@av4u.com
    DocumentRoot /home/coav4u/public_html
    ServerName www.av4u.co
    ErrorLog logs/www.av4u.co-error_log
    CustomLog logs/www.av4u.co-access_log common
</VirtualHost>存檔然後重啟 httpd
但是出現 error:
_default_ VirtualHost overlap on port 80, the first has precedence
SOL:
由於 /etc/httpd/conf/httpd.conf 內 NameVirtualHost *:80 而 virtual1.conf及virtual2.conf 內都設定為 <VirtualHost 192.168.1.12:80> 不匹配,正確的設定為:
httpd.conf 及 virtual1.conf及virtual2.conf(所有虛擬主機的設定檔)不是都為 *:80 就是都為 192.168.1.12:80 即
httpd.conf ===> NameVirtualHost *:80,virtual1.conf及virtual2.conf ===> <VirtualHost *:80>或者
httpd.conf ===> NameVirtualHost 192.168.1.12:80,virtual1.conf及virtual2.conf ===> <VirtualHost 192.168.1.12:80>



頁: [1]
查看完整版本: Apache 如何設定多個虛擬伺服器(Virtual Server)