分享三種Apache配置虛擬主機(jī)的方式
一、基于IP
1. 假設(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
2. 修改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
3. 建立虛擬主機(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
4. 在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>
5. 大功告成,測(cè)試下每個(gè)虛擬主機(jī),分別訪問(wèn)www.test1.com、www.test2.com、www.test3.com
二、基于主機(jī)名
1. 設(shè)置域名映射同一個(gè)IP,修改hosts:
192.168.1.10 www.test1.com
192.168.1.10 www.test2.com
192.168.1.10 www.test3.com
2. 跟上面一樣,建立虛擬主機(jī)存放網(wǎng)頁(yè)的根目錄
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html
3. 在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)偵聽那個(gè)IP地址。 這里設(shè)定的IP地址必須對(duì)應(yīng)服務(wù)器上的一個(gè)網(wǎng)絡(luò)接口。
下一步就是為你建立的每個(gè)虛擬主機(jī)設(shè)定<VirtualHost>配置塊,<VirtualHost>的參數(shù)與NameVirtualHost指令的參數(shù)是一樣的。每個(gè)<VirtualHost>定義塊中,至少都會(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è)<VirtualHost>定義塊。其中ServerName和DocumentRoot所包含的內(nèi)容應(yīng)該與全局的保持一致,且要放在配置文件的最前面,扮演默認(rèn)主機(jī)的角色。
NameVirtualHost *:80 <VirtualHost *:80> ServerName * DocumentRoot /www/ </VirtualHost> <VirtualHost *: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 *:80> ServerName www.test2.com DocumentRoot /www/test2/ <Directory "/www/test2"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:80> ServerName www.test3.com DocumentRoot /www/test3/ <Directory "/www/test3"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost>
4. 大功告成,測(cè)試下每個(gè)虛擬主機(jī),分別訪問(wèn)www.test1.com、www.test2.com、www.test3.com
三、基于端口
1. 修改配置文件
將原來(lái)的Listen 80改為L(zhǎng)isten 80、Listen 8080
2. 更改虛擬主機(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ī)的方法,希望對(duì)大家學(xué)習(xí)有所幫助。
- apache虛擬主機(jī)中設(shè)置泛域名解析的方法
- Apache虛擬主機(jī)的配置和泛域名解析實(shí)現(xiàn)代碼
- apache虛擬主機(jī)三種配置方式小結(jié)
- apache服務(wù)器一個(gè)ip(如:127.0.0.1)和多個(gè)域名(虛擬主機(jī))的綁定
- Apache 配置多端口 多虛擬主機(jī) 局域網(wǎng)訪問(wèn)示例
- 如何配置apache虛擬主機(jī)的實(shí)例小結(jié)
- 本地機(jī)apache配置基于域名的虛擬主機(jī)詳解
- windows apache多端口虛擬主機(jī)配置方法
- apache中偽靜態(tài)配置和使用(Apache虛擬主機(jī)下Discuz偽靜態(tài))
- Apache實(shí)現(xiàn)本地建立泛域名虛擬主機(jī)的方法
相關(guān)文章
教你一招實(shí)現(xiàn)Linux中的文本比對(duì)
這篇文章主要給阿加介紹了關(guān)于Linux下文本比對(duì)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11詳解阿里云CentOS Linux服務(wù)器上用postfix搭建郵件服務(wù)器
本篇文章主要介紹了詳解阿里云CentOS Linux服務(wù)器上用postfix搭建郵件服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12Linux環(huán)境下快速搭建ftp服務(wù)器方法介紹
這篇文章主要介紹了Linux環(huán)境下快速搭建ftp服務(wù)器方法介紹,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01關(guān)于linux服務(wù)器下vsftpd的一些問(wèn)題
針對(duì)我自己本身出現(xiàn)的問(wèn)題,將高人與我的QQ聊天記錄整理了一份,然后再加上實(shí)際的操作.終于讓我大徹大悟.現(xiàn)在與大家狠狠的分享這位高人的成果.2010-06-06解決xampp自啟動(dòng)和mysql.sock問(wèn)題
Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’2010-10-10