52AV手機A片王|52AV.ONE

標題: Apache 如何設定多個虛擬伺服器(Virtual Server) [打印本頁]

作者: IT_man    時間: 2014-10-3 10:29
標題: Apache 如何設定多個虛擬伺服器(Virtual Server)
我的環境是  <Linux CENTOS 6 + Apache + mysql>
〖方法一〗
1. vi /etc/httpd/httpd.conf
1 N* L! }9 j' P" [% n( k2. 找到 # VirtualHost example:
# P2 o! `, [, g
  1. 其下加入你的virtual server 設定,例如:5 C+ o; \$ v1 u% n
  2. #2014/10/02 add
    # S1 r8 V, \1 ]
  3. <VirtualHost 192.168.1.12:80>  #或 <VirtualHost 127.0.0.1:80>  #符號為註解
    * c: T( h: H* A0 @! p# h8 M/ g  ?
  4.     ServerAdmin root@av4u.com0 e$ k/ {6 l1 k" \0 U! n! }4 N
  5.     DocumentRoot /home/coav4u/public_html4 U, ^# Q' c7 O- d( W
  6.     ServerName av4u.co
    ! n6 l  Z& U% o6 e$ k  s
  7.     ErrorLog logs/www.av4u.co-error_log+ D# R% h  i& Y% k' P
  8.     CustomLog logs/www.av4u.co-access_log common
    . ~! d8 q/ \5 [# [* o) h" q
  9. </VirtualHost>
    ; ~; x" {  w9 r3 U  P& {/ E
  10. <VirtualHost 192.168.1.12:80>
    5 k0 v# r! J) x& f. H; p+ `3 A! E
  11.     ServerAdmin root@av4u.com
    6 t3 v! P& K0 S: |; d
  12.     DocumentRoot /home/coav4u/public_html: C5 C0 v2 I, w* [' U- `4 E
  13.     ServerName www.av4u.co
    ; y- Q5 p, L! m8 F" \" H" i
  14.     ErrorLog logs/www.av4u.co-error_log3 g1 F0 \0 S6 q& L0 j4 P
  15.     CustomLog logs/www.av4u.co-access_log common" O! L, g6 j6 P6 c
  16. </VirtualHost>
複製代碼
    可同時指定2個 Domain
name (av4u.com及www.av4u.com)到同一個虛擬空間,這個部分似乎比 nginx 設定稍微麻煩一點& U, k% f, I! s) T3 F5 t5 C" q, M2 j$ k6 Q
3. 重啟 Apache 才生效" E; i2 V5 a) ]. H
    service httpd restart #須以root身分   --->搞定
4 t6 E) y4 }& u0 G  [' P8 u   註: service httpd start  #啟動        

. M, X, a2 Y' o. n2 M4 A" U        service httpd start  #停止
. J  R$ W. H3 D& e' D% {方法二0 [; b0 {+ k5 J  a
在/etc/httpd/conf.d/新ˋ建一獨立檔(設定一個virtual host)
& q, g" n( H6 _4 A/ _" R, [  N例如:
1 E. }8 |0 f# C3 Y5 Vvirtual1.conf
( Q9 @. S. n8 \9 a4 B
  1. <VirtualHost 192.168.1.12:80>  #或 <VirtualHost 127.0.0.1:80>  #符號為註解7 P/ N; I% X+ \
  2.     ServerAdmin root@av4u.com
    8 g* c" ?. S8 Y- C' T9 x
  3.     DocumentRoot /home/coav4u/public_html
    + `& v7 ^5 M/ c* x) p2 p; N* r1 \
  4.     ServerName av4u.co3 x% {, N/ p/ L1 o3 U
  5.     ErrorLog logs/www.av4u.co-error_log) u) W6 o* d6 W/ x' K* F
  6.     CustomLog logs/www.av4u.co-access_log common* L' x$ e/ u' s9 g4 f
  7. </VirtualHost>
複製代碼
virtual2.conf
' {, U" v; g& K
  1. <VirtualHost 192.168.1.12:80>9 k& K9 c# u: j4 l
  2.     ServerAdmin root@av4u.com) ]9 P2 J2 z& M' e0 e$ i
  3.     DocumentRoot /home/coav4u/public_html  N3 Z5 D* r' a3 p! z3 J* z: |
  4.     ServerName www.av4u.co3 Y* C1 U% z( d$ g; k
  5.     ErrorLog logs/www.av4u.co-error_log
    & L$ x' I" T; {# W* L
  6.     CustomLog logs/www.av4u.co-access_log common3 f( y; f& t" \
  7. </VirtualHost>
複製代碼
存檔然後重啟 httpd

2 \; L; ]4 k8 d8 A/ F8 \+ {6 W但是出現 error:
; y- _% D, n# q8 m% S4 H2 l [warn] _default_ VirtualHost overlap on port 80, the first has precedence& C! E" A% k* b4 t9 ~2 J
SOL:
$ a+ H) V# L5 w: q1 l由於 /etc/httpd/conf/httpd.conf 內 NameVirtualHost *:80 而 virtual1.conf及virtual2.conf 內都設定為 <VirtualHost 192.168.1.12:80> 不匹配,正確的設定為:
( k  J! x% V- S7 F1 uhttpd.conf 及 virtual1.conf及virtual2.conf(所有虛擬主機的設定檔)不是都為 *:80 就是都為 192.168.1.12:80 即
7 Y* e+ J- F- f  Mhttpd.conf ===> NameVirtualHost *:80  ,  virtual1.conf及virtual2.conf ===> <VirtualHost *:80>  或者
+ h  z5 ^% L: m# S5 ^9 ?4 zhttpd.conf ===> NameVirtualHost 192.168.1.12:80  ,  virtual1.conf及virtual2.conf ===> <VirtualHost 192.168.1.12:80>- g, C# M* Q& t& d: y/ H) o6 @6 ~, a

! V; y% g+ Y; q
; [2 g: ]5 s( J- u

+ S& L* ]. X% w6 C% B




歡迎光臨 52AV手機A片王|52AV.ONE (https://www.52av.one/) Powered by Discuz! X3.2