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+ o; t) [& Q; r* W  {# Q, p
2. 找到 # VirtualHost example:+ G; P* k: G3 Z" ~
  1. 其下加入你的virtual server 設定,例如:7 e& F; q* h; _$ t2 L) j% Y. P$ c
  2. #2014/10/02 add
    ' U& I& y& |! l
  3. <VirtualHost 192.168.1.12:80>  #或 <VirtualHost 127.0.0.1:80>  #符號為註解4 g' h. \% {# d' }$ [; g
  4.     ServerAdmin [email protected]/ A$ J+ g; `8 s4 i6 c  _6 S; m
  5.     DocumentRoot /home/coav4u/public_html
    , {% Y9 y  l) b7 B
  6.     ServerName av4u.co
    / z9 T. K8 _1 Y* H! f- h
  7.     ErrorLog logs/www.av4u.co-error_log
      H( C; I4 _* `$ J: D
  8.     CustomLog logs/www.av4u.co-access_log common  U0 T8 Y; N1 g7 }3 R) w3 p
  9. </VirtualHost>
    8 G" t( ~, X& \: H8 k- J. l# L
  10. <VirtualHost 192.168.1.12:80>
    1 b% O7 _1 a2 O; C# u# Y
  11.     ServerAdmin [email protected]2 i% n, T# Y( U' I/ a7 @: B
  12.     DocumentRoot /home/coav4u/public_html7 H2 _5 P8 Y9 u3 L9 `6 t9 R6 J
  13.     ServerName www.av4u.co
    # h+ ^7 ~8 W$ P1 N: h+ o9 ?- j; E& K
  14.     ErrorLog logs/www.av4u.co-error_log
    ( L; y2 |% i. T% ?$ `# V; \& b
  15.     CustomLog logs/www.av4u.co-access_log common$ _$ t/ }% t' G0 C
  16. </VirtualHost>
複製代碼
    可同時指定2個 Domain
name (av4u.com及www.av4u.com)到同一個虛擬空間,這個部分似乎比 nginx 設定稍微麻煩一點$ _# M& |! t$ V) T# D$ p# h- n
3. 重啟 Apache 才生效
; J2 A) {6 f& ?8 r    service httpd restart #須以root身分   --->搞定2 h$ @- y6 u- ]/ N+ y9 S' o
   註: service httpd start  #啟動        

8 H% @) |- N$ z5 X# G        service httpd start  #停止% k; h$ i+ I  C4 j' ?1 Z# ^5 W/ F7 y* G
方法二7 D) A4 Q  D; y0 k/ [( j
在/etc/httpd/conf.d/新ˋ建一獨立檔(設定一個virtual host)4 k) b- [- h* Y2 F
例如:
. _' y8 E* N+ ~virtual1.conf
2 \- F$ v: ~! j: Y4 C- ?* \# @
  1. <VirtualHost 192.168.1.12:80>  #或 <VirtualHost 127.0.0.1:80>  #符號為註解- C+ V$ p5 F! v6 F$ r5 W8 B& D
  2.     ServerAdmin [email protected]
    ) |& {" u( {7 k, Z4 |7 D* j
  3.     DocumentRoot /home/coav4u/public_html  P. _. O# q  y! K& `7 W4 H, B
  4.     ServerName av4u.co
    & A' ^$ V- B/ b; c6 M  b
  5.     ErrorLog logs/www.av4u.co-error_log
    ' v4 q! T/ o! P
  6.     CustomLog logs/www.av4u.co-access_log common
    , q: z  g2 T2 S
  7. </VirtualHost>
複製代碼
virtual2.conf
2 z: }+ k. @7 A/ ~; v
  1. <VirtualHost 192.168.1.12:80>8 C7 M& `) s) y' |; p- y  Y
  2.     ServerAdmin [email protected]
    5 e' B! j8 F% H( o0 K
  3.     DocumentRoot /home/coav4u/public_html
    5 Q" F' V' q3 N& u
  4.     ServerName www.av4u.co* {2 o4 _7 Z" G" O' A& Z, G
  5.     ErrorLog logs/www.av4u.co-error_log
    ; U5 s* |5 O  W3 x2 c
  6.     CustomLog logs/www.av4u.co-access_log common+ Q  d$ }; q6 m% f
  7. </VirtualHost>
複製代碼
存檔然後重啟 httpd

2 K4 `- x' E( l3 C$ V2 I但是出現 error:
( Q6 I/ U8 i9 u, {2 k" C, V' i [warn] _default_ VirtualHost overlap on port 80, the first has precedence
$ V$ K5 b* y- }- ^: pSOL:
; V1 d) A  w  l' e. E5 g由於 /etc/httpd/conf/httpd.conf 內 NameVirtualHost *:80 而 virtual1.conf及virtual2.conf 內都設定為 <VirtualHost 192.168.1.12:80> 不匹配,正確的設定為:9 c% L+ m$ ~9 r) T1 {% r% C
httpd.conf 及 virtual1.conf及virtual2.conf(所有虛擬主機的設定檔)不是都為 *:80 就是都為 192.168.1.12:80 即
& W# }  G6 ]5 fhttpd.conf ===> NameVirtualHost *:80  ,  virtual1.conf及virtual2.conf ===> <VirtualHost *:80>  或者 + b# |6 ]5 q7 F
httpd.conf ===> NameVirtualHost 192.168.1.12:80  ,  virtual1.conf及virtual2.conf ===> <VirtualHost 192.168.1.12:80>
: d: R. |  `+ H. ?0 B
4 g& Z8 z: q. d0 R8 ]! K
* `( N. Y8 ?0 b" s: R

5 v+ T/ m4 L/ A1 o& j2 p7 x




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