|
在Apache中配置基于主機(jī)名的虛擬主機(jī)
在同一臺pc(ip:192.168.1.1)中配置三個虛擬主機(jī):
www.1.com
www.2.com
www.3.com
訪問不同的虛擬主機(jī),可以顯示不同的頁面。
注意:以下的設(shè)置全部在Apache服務(wù)器上??!
1.
打開/etc/hosts,加入如下幾句:
192.168.1.1 www.1.com
192.168.1.1 www.2.com
192.168.1.1 www.3.com
2.
為每個虛擬主機(jī)增加一個web目錄:
比如我在/usr/local/apache2/htdocs/下新建了三個文件夾,分別對應(yīng)三個虛擬主機(jī):
文件夾1 --> www.1.com
文件夾2 --> www.2.com
文件夾3 --> www.3.com
然后在這三個目錄中各自新建一個測試的index.php,只要不一樣就行了:
1:
<html>
<body>
<?php echo "You are in 1"; ?>
</body>
</html>
2:
<html>
<body>
<?php echo "You are in 2"; ?>
</body>
</html>
3:
<html>
<body>
<?php echo "You are in 3"; ?>
</body>
</html>
3.
編輯apache的httpd.conf配置文件,我的是/usr/local/apache2/conf/httpd.conf,找到如下語句:
# Virtual hosts,把它下面的那一句前的#去掉(即取消注釋)
然后打開它提示的XXX/extra/httpd-vhosts.conf配置文件,修改成如下所示:
#
# Virtual Hosts # # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd./docs/2.2/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any <VirtualHost> block. # <VirtualHost *:80> ServerName * DocumentRoot "/usr/local/apache2/htdocs" </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@www.1.com DocumentRoot "/usr/local/apache2/htdocs/1" ServerName www.1.com ErrorLog "logs/www.1.com-error_log" CustomLog "logs/www.1.com-access_log" common <Directory "/usr/local/apache2/htdocs/1"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:80>
ServerAdmin webmaster@www.2.com DocumentRoot "/usr/local/apache2/htdocs/2" ServerName www.2.com ErrorLog "logs/www.2.com-error_log" CustomLog "logs/www.2.com-access_log" common <Directory "/usr/local/apache2/htdocs/2"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@www.3.com DocumentRoot "/usr/local/apache2/htdocs/3" ServerName www.3.com ErrorLog "logs/www.3.com-error_log" CustomLog "logs/www.3.com-access_log" common <Directory "/usr/local/apache2/htdocs/3"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> 其實就是保留 NameVirtualHost *:80 這一句,然后把剩下的刪除,再加上最后的4段,其中后三段是完全類似的,而第一段是必須要寫的!
到這里,我們已經(jīng)可以在服務(wù)器本地訪問上述網(wǎng)站了:
在Apache服務(wù)器的瀏覽器中輸入:www.1.com/index.php, www.2.com/index.php, www.3.com/index.php 可以依次訪問上述的三個不同的網(wǎng)站。
下面來配置一下如何在局域網(wǎng)內(nèi)訪問:
有兩種方法:
1.
如果局域網(wǎng)里已經(jīng)有了DNS服務(wù)器了,那么只要打開本地的 /etc/resolv.conf 文件,然后加入那個DNS服務(wù)器的ip就可以了,比如DNS的ip是192.168.1.100,那么加入: nameserver 192.168.1.100(不過那個DNS里要有對應(yīng)的記錄,沒有的話在那個DNS里添上就可以了)
2.
如果局域網(wǎng)里沒有DNS服務(wù)器,那么我們也可以直接在本地的host里手動加入,就像我們之前在Apache服務(wù)器上做的:
打開本地的 /etc/hosts 文件,加入如下幾句:
192.168.1.1 www.1.com
192.168.1.1 www.2.com
192.168.1.1 www.3.com
就可以了!在本地的瀏覽器中輸入:www.1.com/index.php, www.2.com/index.php, www.3.com/index.php 同樣可以依次訪問在Apache上的上述的三個不同的網(wǎng)站。
完成!
|
|
|