我的環境是 <Linux CENTOS 6 + Apache + mysql> 〖方法一〗 1. vi /etc/httpd/httpd.conf8 F' g$ \* f8 A) q/ |1 o0 ]& Z
2. 找到 # VirtualHost example:* j @' W/ q) o. | M8 N8 h1 x& s4 ]1 I
- 其下加入你的virtual server 設定,例如:5 }" S% u! N8 j& c. d" i
- #2014/10/02 add7 O$ \* Z6 h$ H. }+ u7 ]0 m. e
- <VirtualHost 192.168.1.12:80> #或 <VirtualHost 127.0.0.1:80> #符號為註解/ S2 n& |2 P Z& H
- ServerAdmin [email protected]: z( w% J: I; S5 ?! i
- DocumentRoot /home/coav4u/public_html2 j% C2 V6 b; w
- ServerName av4u.co2 P! p- P3 G+ f% ?6 V8 c
- ErrorLog logs/www.av4u.co-error_log. z8 h! q: x$ A6 |' N
- CustomLog logs/www.av4u.co-access_log common
) t6 e2 c; x- `$ x1 T* L - </VirtualHost>* O. n7 J0 P/ I [% J1 [
- <VirtualHost 192.168.1.12:80>5 L& o8 @2 U( q9 Z' e! H
- ServerAdmin [email protected]
' [3 ~7 ~/ {5 P* ^, o2 k% k0 N - DocumentRoot /home/coav4u/public_html4 z( z7 x$ E' N k7 [# M
- ServerName www.av4u.co
. b/ n5 V. F8 O: q# d; K - ErrorLog logs/www.av4u.co-error_log) l8 h3 ?8 P+ n, A
- CustomLog logs/www.av4u.co-access_log common
( c, |. Y+ `! ]) Q0 ^: |6 i - </VirtualHost>
複製代碼 可同時指定2個 Domain name (av4u.com及www.av4u.com)到同一個虛擬空間,這個部分似乎比 nginx 設定稍微麻煩一點5 [# @/ @% v& F# h
3. 重啟 Apache 才生效- _# j3 r; X% o8 q3 W
service httpd restart #須以root身分 --->搞定
9 T$ f. W3 Y! j6 [: l3 u 註: service httpd start #啟動
# n% Y2 V4 A3 a- S$ j service httpd start #停止
5 t1 ?6 H! }# o' C8 L6 v5 Q〖方法二〗7 U: V, x [ ` Q
在/etc/httpd/conf.d/新ˋ建一獨立檔(設定一個virtual host)& c! `( [( i* M5 e9 d7 y1 |
例如:
. m3 U8 c+ |7 ^virtual1.conf2 q9 f( Y5 `+ O$ W7 T# N" n
- <VirtualHost 192.168.1.12:80> #或 <VirtualHost 127.0.0.1:80> #符號為註解
7 |2 b" {0 q( g' c+ x \& y T6 Y - ServerAdmin [email protected]
! g' v+ p. f; ^8 X% R2 _, n7 J' ~ - DocumentRoot /home/coav4u/public_html
. Z% Z3 t$ ^1 b - ServerName av4u.co9 x6 ]2 H9 p0 R, ^5 M! J, U
- ErrorLog logs/www.av4u.co-error_log
! w6 }. o$ p9 F1 a( }' m, W( l - CustomLog logs/www.av4u.co-access_log common9 `- R. w( {/ o, a1 T9 y3 I
- </VirtualHost>
複製代碼 virtual2.conf
0 _! O' b: R; q- <VirtualHost 192.168.1.12:80>
+ @6 [) Q( T' _7 b - ServerAdmin [email protected]
* n! c( v C0 Y - DocumentRoot /home/coav4u/public_html% D( [9 F) R' Y+ ] y u
- ServerName www.av4u.co
- \( d% [6 S# D - ErrorLog logs/www.av4u.co-error_log C" Q% E0 a: g2 a4 q
- CustomLog logs/www.av4u.co-access_log common, c- Y% I: V* T; B* H, f0 q
- </VirtualHost>
複製代碼 存檔然後重啟 httpd
* c9 J( G, N1 V3 G% m但是出現 error:" R8 Z; M- l7 u% M
[warn] _default_ VirtualHost overlap on port 80, the first has precedence/ I2 Q+ s3 Z. b& j) G% L+ w: U
SOL:
7 P+ t: a; E6 ]. H3 z: x6 l0 q由於 /etc/httpd/conf/httpd.conf 內 NameVirtualHost *:80 而 virtual1.conf及virtual2.conf 內都設定為 <VirtualHost 192.168.1.12:80> 不匹配,正確的設定為:* X! v* j/ m e1 ?/ J
httpd.conf 及 virtual1.conf及virtual2.conf(所有虛擬主機的設定檔)不是都為 *:80 就是都為 192.168.1.12:80 即
- c l2 j; l8 v& T0 j6 s$ Hhttpd.conf ===> NameVirtualHost *:80 , virtual1.conf及virtual2.conf ===> <VirtualHost *:80> 或者
3 F% n9 \+ I; Y: ^& thttpd.conf ===> NameVirtualHost 192.168.1.12:80 , virtual1.conf及virtual2.conf ===> <VirtualHost 192.168.1.12:80>7 [3 R% y1 g0 _) f {6 {; u
0 U# e h" X4 W i0 S0 {
7 j6 ~/ [% S+ l" N9 h7 q
' l) y4 l& M3 A6 Z9 k. n: w+ { |
|