我的環境是 <Linux CENTOS 6 + Apache + mysql> 〖方法一〗 1. vi /etc/httpd/httpd.conf
# g7 E: `. ~. v3 f9 `2 G2. 找到 # VirtualHost example:
& [- B1 E7 o) V% z% E- 其下加入你的virtual server 設定,例如:* f+ X/ C; P; @
- #2014/10/02 add
- g3 ?2 b- a ~9 e - <VirtualHost 192.168.1.12:80> #或 <VirtualHost 127.0.0.1:80> #符號為註解
' S: G( p2 K! A e - ServerAdmin [email protected]' _( F( i6 H% A3 F! U
- DocumentRoot /home/coav4u/public_html
$ C% J( p- a1 h8 J" s! Z3 {0 v3 h - ServerName av4u.co' o- Q Y3 z8 ]& G* G1 \
- ErrorLog logs/www.av4u.co-error_log
* D6 f+ G( O* O - CustomLog logs/www.av4u.co-access_log common
2 Z( F* K0 V+ k5 B/ h2 w( u- f - </VirtualHost>3 ]8 b F$ ^9 q: b
- <VirtualHost 192.168.1.12:80>
# B: o* U5 Y6 n/ U. Z: B. \ - ServerAdmin [email protected]
6 ]# t6 |- E* b - DocumentRoot /home/coav4u/public_html
; z% ~7 L9 ]: O1 g# z# U7 G. m, y - ServerName www.av4u.co
% b/ A% l0 [+ `4 G2 H - ErrorLog logs/www.av4u.co-error_log) D% o: j0 I/ U, u9 @! L
- CustomLog logs/www.av4u.co-access_log common
' J& ] c9 K. |; E4 l# E! h - </VirtualHost>
複製代碼 可同時指定2個 Domain name (av4u.com及www.av4u.com)到同一個虛擬空間,這個部分似乎比 nginx 設定稍微麻煩一點
, [8 {) I! [6 O3. 重啟 Apache 才生效
i. j( P2 K, g \ service httpd restart #須以root身分 --->搞定5 u5 }6 r( T, a6 T8 J, l
註: service httpd start #啟動 + b% r! Y: w, N L! H8 l+ P2 Q- z
service httpd start #停止; ~$ h: j' E6 a7 H
〖方法二〗
; E3 p R8 N0 B* v8 m在/etc/httpd/conf.d/新ˋ建一獨立檔(設定一個virtual host)
8 W+ g5 s4 o# Y( F- c例如:
9 Z b: |5 [- r9 ]2 Mvirtual1.conf8 i2 d1 P, `5 R) J
- <VirtualHost 192.168.1.12:80> #或 <VirtualHost 127.0.0.1:80> #符號為註解. _' j: E: F2 E% W
- ServerAdmin [email protected]$ f2 O* @! i! y. l; d- r0 q. i
- DocumentRoot /home/coav4u/public_html3 W$ J( s( X$ E9 Q! U7 Y
- ServerName av4u.co
6 d# ^0 j2 o* p# U+ j! ^ - ErrorLog logs/www.av4u.co-error_log
# r, R1 ^* N# C: s( A - CustomLog logs/www.av4u.co-access_log common, C9 a. p1 z' h) ?1 |9 b' A6 L, c
- </VirtualHost>
複製代碼 virtual2.conf8 F' P8 r" W0 n: d1 A; k& U
- <VirtualHost 192.168.1.12:80>1 _6 a' @5 k9 S) l8 B
- ServerAdmin [email protected]
0 F7 T% y; ]/ u, C# v6 P$ y9 r - DocumentRoot /home/coav4u/public_html$ Q2 R% F( X: A
- ServerName www.av4u.co2 P" a0 }1 O |
- ErrorLog logs/www.av4u.co-error_log
8 J* W# y% v4 f. Q# t - CustomLog logs/www.av4u.co-access_log common
% Z9 M* q, J& ?8 V8 _$ R4 t2 a- g - </VirtualHost>
複製代碼 存檔然後重啟 httpd
$ Q! ^- W1 ~# ~4 S5 L但是出現 error:
. D* z! g% W- B; P% V' o% u [warn] _default_ VirtualHost overlap on port 80, the first has precedence0 t) q w# M) y5 h
SOL:; A4 q8 Y" H5 J" h# d" e
由於 /etc/httpd/conf/httpd.conf 內 NameVirtualHost *:80 而 virtual1.conf及virtual2.conf 內都設定為 <VirtualHost 192.168.1.12:80> 不匹配,正確的設定為:3 p" r1 O) M) _. |1 q, _( S1 \* ^
httpd.conf 及 virtual1.conf及virtual2.conf(所有虛擬主機的設定檔)不是都為 *:80 就是都為 192.168.1.12:80 即7 v" i; T! x# ?7 D4 ^" h3 }
httpd.conf ===> NameVirtualHost *:80 , virtual1.conf及virtual2.conf ===> <VirtualHost *:80> 或者 ! H6 |/ {4 | L; M# J: L, S6 M
httpd.conf ===> NameVirtualHost 192.168.1.12:80 , virtual1.conf及virtual2.conf ===> <VirtualHost 192.168.1.12:80>3 x6 S' }( z2 M1 f' e- F
. d( }; n; Q; T v9 h# V7 G! M* U" ]5 @2 Y* V5 y2 K
3 P) q9 `9 U* |, ~+ Q. p
|
|