溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

配置apache虛擬主機(jī)的方法

發(fā)布時(shí)間:2020-10-24 14:39:17 來(lái)源:億速云 閱讀:157 作者:小新 欄目:建站服務(wù)器

小編給大家分享一下配置apache虛擬主機(jī)的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

Apache 配置虛擬主機(jī)的三種方法

記事本打開(kāi)httpd.conf文件 ,該文件在apache的目錄下,如: D:\AppServ\Apache2.2\conf,修改如下兩處:

LoadModule vhost_alias_module modules/mod_vhost_alias.so   //去掉前面的#,意思是啟用apache的虛擬主機(jī)功能,第203行  
Include conf/extra/httpd-vhosts.conf  //去掉#的意思是從httpd-vhosts.conf這個(gè)文件導(dǎo)入虛擬主機(jī)配置

配置虛擬主機(jī)后 不能用localhost 訪問(wèn)

只需要把httpd.conf文件的ServerName localhost:80 那行注釋掉 就可以了

一、基于IP

假設(shè)服務(wù)器有個(gè)IP地址為192.168.1.10,使用ifconfig在同一個(gè)網(wǎng)絡(luò)接口eth0上綁定3個(gè)IP:

[root@localhost root]# ifconfig eth0:1 192.168.1.11
[root@localhost root]# ifconfig eth0:2 192.168.1.12
[root@localhost root]# ifconfig eth0:3 192.168.1.13

修改hosts文件,添加三個(gè)域名與之一一對(duì)應(yīng):

192.168.1.11   www.test1.com
192.168.1.12   www.test2.com
192.168.1.13   www.test3.com

建立虛擬主機(jī)存放網(wǎng)頁(yè)的根目錄,如在/www目錄下建立test1、test2、test3文件夾,其中分別存放1.html、2.html、3.html

/www/test1/1.html
/www/test2/2.html
/www/test3/3.html

在httpd.conf中將附加配置文件httpd-vhosts.conf包含進(jìn)來(lái),接著在httpd-vhosts.conf中寫入如下配置:

<VirtualHost 192.168.1.11:80>
  ServerName www.test1.com
  DocumentRoot /www/test1/
  <Directory "/www/test1">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   </Directory>
</VirtualHost>

<VirtualHost 192.168.1.12:80>
  ServerName www.test1.com
  DocumentRoot /www/test2/
  <Directory "/www/test2">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   </Directory>
</VirtualHost>

<VirtualHost 192.168.1.13:80>
  ServerName www.test1.com
  DocumentRoot /www/test3/
  <Directory "/www/test3">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   </Directory>
</VirtualHost>

大功告成,測(cè)試下每個(gè)虛擬主機(jī),分別訪問(wèn)www.test1.com、www.test2.com、www.test3.com

二、基于主機(jī)名

設(shè)置域名映射同一個(gè)IP,修改hosts:

127.0.0.1  gm.998gx.com
127.0.0.1  www.998gx.com
127.0.0.1  r.998gx.com
127.0.0.1  localhost

跟上面一樣,建立虛擬主機(jī)存放網(wǎng)頁(yè)的根目錄

/www/dxGM/index.php
/www/dxskadmin/index.php
/www/88qp/index.php

在httpd.conf中將附加配置文件httpd-vhosts.conf包含進(jìn)來(lái),接著在httpd-vhosts.conf中寫入如下配置:

為了使用基于域名的虛擬主機(jī),必須指定服務(wù)器IP地址(和可能的端口)來(lái)使主機(jī)接受請(qǐng)求。可以用NameVirtualHost指令來(lái)進(jìn)行配置。 如果服務(wù)器上所有的IP地址都會(huì)用到, 你可以用*作為NameVirtualHost的參數(shù)。在NameVirtualHost指令中指明IP地址并不會(huì)使服務(wù)器自動(dòng)偵聽(tīng)那個(gè)IP地址。 這里設(shè)定的IP地址必須對(duì)應(yīng)服務(wù)器上的一個(gè)網(wǎng)絡(luò)接口。

下一步就是為你建立的每個(gè)虛擬主機(jī)設(shè)定配置塊,的參數(shù)與NameVirtualHost指令的參數(shù)是一樣的。每個(gè)定義塊中,至少都會(huì)有一個(gè)ServerName指令來(lái)指定伺服哪個(gè)主機(jī)和一個(gè)DocumentRoot指令來(lái)說(shuō)明這個(gè)主機(jī)的內(nèi)容存在于文件系統(tǒng)的什么地方。

如果在現(xiàn)有的web服務(wù)器上增加虛擬主機(jī),必須也為現(xiàn)存的主機(jī)建造一個(gè)定義塊。其中ServerName和DocumentRoot所包含的內(nèi)容應(yīng)該與全局的保持一致,且要放在配置文件的最前面,扮演默認(rèn)主機(jī)的角色。

<VirtualHost *:80> 
    DocumentRoot "D:/phpstudy/WWW/dxGM" 
    ServerName gm.998gx.com 
</VirtualHost>

<VirtualHost *:80> 
    DocumentRoot "D:/phpstudy/WWW/88qp" 
    ServerName www.998gx.com 
</VirtualHost>

<VirtualHost *:80> 
    DocumentRoot "D:/phpstudy/WWW/dxskadmin" 
    ServerName r.998gx.com 
</VirtualHost>

<VirtualHost *:80> 
    DocumentRoot "D:/phpstudy/WWW" 
    ServerName localhost 
</VirtualHost>

測(cè)試下每個(gè)虛擬主機(jī),分別訪問(wèn)gm.998gx.com、www.998gx.com、r.998gx.com

三、基于端口

修改配置文件
  將原來(lái)的

Listen 80
改為
  Listen 80
   Listen 8080

更改虛擬主機(jī)設(shè)置:

<VirtualHost 192.168.1.10:80>
    DocumentRoot /var/www/test1/
    ServerName www.test1.com
</VirtualHost>

<VirtualHost 192.168.1.10:8080>
    DocumentRoot /var/www/test2
    ServerName www.test2.com
</VirtualHost>

以上是配置apache虛擬主機(jī)的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI