我的環境是 <Linux CENTOS 6 + Apache + mysql> 〖方法一〗 1. vi /etc/httpd/httpd.conf
) @$ f% A8 E& S# ]% ~- V6 ]- ^2. 找到 # VirtualHost example:% W% \; Q+ Z$ P. p- n6 T
- 其下加入你的virtual server 設定,例如:
/ ?, `5 S3 c% [ - #2014/10/02 add0 p3 E5 \, \! M' F! v& Q( ^1 Z5 O
- <VirtualHost 192.168.1.12:80> #或 <VirtualHost 127.0.0.1:80> #符號為註解) |1 P, i- `5 `5 @( E
- ServerAdmin [email protected]
, e- L+ Q2 L3 m9 t2 _- z0 j - DocumentRoot /home/coav4u/public_html
0 e( c5 K4 j, Q* }; m3 t - ServerName av4u.co. S: a8 x4 i+ e( M5 B
- ErrorLog logs/www.av4u.co-error_log
/ a8 r. G( r7 Y, O+ u8 b0 g - CustomLog logs/www.av4u.co-access_log common4 @! y% o7 P; d# x$ i
- </VirtualHost>( M4 j1 I- j3 e) ?4 n. {6 M
- <VirtualHost 192.168.1.12:80>1 d4 p# v; O- M" F
- ServerAdmin [email protected]
6 e; ^' L4 o( K9 T - DocumentRoot /home/coav4u/public_html
# S6 u, u4 I8 \7 H' Y - ServerName www.av4u.co0 S/ U" f* B2 t; B
- ErrorLog logs/www.av4u.co-error_log
% V3 F) T8 u) m9 t2 e - CustomLog logs/www.av4u.co-access_log common3 a9 X% o1 N5 L) R
- </VirtualHost>
複製代碼 可同時指定2個 Domain name (av4u.com及www.av4u.com)到同一個虛擬空間,這個部分似乎比 nginx 設定稍微麻煩一點* B$ w5 i8 n% o+ o
3. 重啟 Apache 才生效! _) Q6 W- e5 I. Z+ v0 R
service httpd restart #須以root身分 --->搞定& \+ V- y/ ?8 O$ K$ h- ^: U
註: service httpd start #啟動 / M- T: l# ~0 u+ q- L/ F8 I& h
service httpd start #停止
# R& `% d! E# I# }: P# v: Q〖方法二〗; I* t& W) L* f; x- l/ u) o) g
在/etc/httpd/conf.d/新ˋ建一獨立檔(設定一個virtual host)
1 _7 x! b+ A1 r$ \: a& W4 J例如:
7 k& S, f2 i9 F# a! v4 tvirtual1.conf9 N# n- z: [' H; H9 r3 c
- <VirtualHost 192.168.1.12:80> #或 <VirtualHost 127.0.0.1:80> #符號為註解
- c& |9 R5 q, Y" Q# |! [/ m. d - ServerAdmin [email protected]
* m+ H, b2 G+ y; d& m - DocumentRoot /home/coav4u/public_html
+ i: \7 b; A3 c* R! _4 h# p - ServerName av4u.co
/ G0 S* }6 ~; j( K* O8 Y/ d4 ? - ErrorLog logs/www.av4u.co-error_log
9 a8 ?) k) ~" ~3 j - CustomLog logs/www.av4u.co-access_log common
8 E* R: N. Q: M; P - </VirtualHost>
複製代碼 virtual2.conf
" `; d& C- r% T2 D- Q- <VirtualHost 192.168.1.12:80>$ g- @2 i5 Q, |0 A1 V
- ServerAdmin [email protected]
5 r- Q4 u0 h/ ~( @8 t; |0 X - DocumentRoot /home/coav4u/public_html% K w7 B* l4 s, @0 w' B) h, v( X5 |+ O
- ServerName www.av4u.co+ ?# r7 m* W& _ p' D q
- ErrorLog logs/www.av4u.co-error_log6 `% I w% Y( n! w4 K
- CustomLog logs/www.av4u.co-access_log common3 Z1 o# M- @, F) R: z2 a
- </VirtualHost>
複製代碼 存檔然後重啟 httpd$ W, x* h* h9 T$ j
但是出現 error:
' N7 {9 h h n; m; K [warn] _default_ VirtualHost overlap on port 80, the first has precedence
: k$ @5 Y2 Q, T( U) T KSOL:: V. N! h6 z4 W( U
由於 /etc/httpd/conf/httpd.conf 內 NameVirtualHost *:80 而 virtual1.conf及virtual2.conf 內都設定為 <VirtualHost 192.168.1.12:80> 不匹配,正確的設定為:
7 @$ u& O6 p$ Thttpd.conf 及 virtual1.conf及virtual2.conf(所有虛擬主機的設定檔)不是都為 *:80 就是都為 192.168.1.12:80 即
$ W& K U9 j) Y5 i$ Z8 D, V6 khttpd.conf ===> NameVirtualHost *:80 , virtual1.conf及virtual2.conf ===> <VirtualHost *:80> 或者 1 Z+ B. J: F0 m! d
httpd.conf ===> NameVirtualHost 192.168.1.12:80 , virtual1.conf及virtual2.conf ===> <VirtualHost 192.168.1.12:80>
# m% A9 r0 _3 \) \' {& c% W7 W E% m2 g$ U, [- M$ K7 ~
6 u! I/ f8 ?3 t: u" {* k: e `$ r5 _, d
7 J; V2 [/ y: r, B c+ T3 [ |
|