1、如何配置 Apache 的虛擬主機
Apache 的虛擬主機就是在一台伺服器上運行多個網站,每個虛擬主機都可以綁定獨立的域名,為這些域名可以指定單獨的目錄,訪問這些域名的時候,Apache 會打開對應目錄裡面的東西。配置 Apache 的虛擬主機,只需要去修改 Apache 的配置文件。虛擬主機的英文是 virtual host,所以,Apache 配置虛擬主機的文件可能是 httpd.conf ,也可能是跟 virtual host 這個名字相關的文件,比如 vhost.conf ,具體要使用哪一個配置文件,需要自己去判斷。
2、如何設置apache的虛擬主機
Apache 配置虛擬主機三種方式
一、基於IP1. 假設伺服器有個IP地址為192.168.1.10,使用ifconfig在同一個網路介面eth0上綁定3個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文件,添加三個域名與之一一對應:
192.168.1.11 www.test1.com
192.168.1.12 www.test2.com
192.168.1.13 www.test3.com
3. 建立虛擬主機存放網頁的根目錄,如在/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包含進來,接著在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. 大功告成,測試下每個虛擬主機,分別訪問www.test1.com、www.test2.com、www.test3.com
1. 設置域名映射同一個IP,修改hosts:
192.168.1.10 www.test1.com
192.168.1.10 www.test2.com
192.168.1.10 www.test3.com
2. 跟上面一樣,建立虛擬主機存放網頁的根目錄
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html
3. 在httpd.conf中將附加配置文件httpd-vhosts.conf包含進來,接著在httpd-vhosts.conf中寫入如下配置:
為了使用基於域名的虛擬主機,必須指定伺服器IP地址(和可能的埠)來使主機接受請求。可以用NameVirtualHost指令來進行配置。 如果伺服器上所有的IP地址都會用到, 你可以用*作為NameVirtualHost的參數。在NameVirtualHost指令中指明IP地址並不會使伺服器自動偵聽那個IP地址。 這里設定的IP地址必須對應伺服器上的一個網路介面。
下一步就是為你建立的每個虛擬主機設定<VirtualHost>配置塊,<VirtualHost>的參數與NameVirtualHost指令的參數是一樣的。每個<VirtualHost>定義塊中,至少都會有一個ServerName指令來指定伺服哪個主機和一個DocumentRoot指令來說明這個主機的內容存在於文件系統的什麼地方。
如果在現有的web伺服器上增加虛擬主機,必須也為現存的主機建造一個<VirtualHost>定義塊。其中ServerName和DocumentRoot所包含的內容應該與全局的保持一致,且要放在配置文件的最前面,扮演默認主機的角色。
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. 大功告成,測試下每個虛擬主機,分別訪問www.test1.com、www.test2.com、www.test3.com
1.修改配置文件
將原來的
Listen 80
改為
Listen 80
Listen 8080
2. 更改虛擬主機設置:
<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>
3、如何配置apache虛擬主機的實例小結
Apache配置localhost虛擬主機步驟
1、打開apache目錄下httpd文件(如:C:\wamp\bin\apache\Apache2.2.17\conf\httpd.conf)找到如下模塊:
#Virtual hosts
#Include conf/extra/httpd-vhosts.conf
去掉前面的#,這樣就開啟了httpd-vhosts虛擬主機文件。
2、打開httpd-vhosts文件(如:C:\wamp\bin\apache\Apache2.2.17\conf\extra\httpd-vhosts.conf),配置好localhost虛擬主機,參照httpd-vhosts文件中實例,修改成如下:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "C:/wamp/www/a"
ServerName www.test.com
ServerAlias www.mmy-host.hleclerc-PC.ingenidev
ErrorLog "logs/mmy-host.hleclerc-PC.ingenidev-error.log"
CustomLog "logs/mmy-host.hleclerc-PC.ingenidev-access.log" common
<Directory C:\wamp\www\a>
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
修改配置如下:
DocumentRoot 修改為本地wamp環境下的www目錄(如:C:\wamp\www\a)
ServerName 改為localhost
3、重啟Apache,這樣就配置好了,本地可以用www.test.com來訪問C:/wamp/www/a 這個站點。
注意事項:
1、httpd文件,打開Include conf/extra/httpd-vhosts.conf模塊
2、目錄路徑,如 C:/wamp/www/a
3、訪問許可權,如上Deny from all修改為Allow from all
4、hosts文件,配置虛擬域名host指向
5、httpd-vhosts文件,配置虛擬主機
4、怎麼在apache上安裝虛擬主機
Apache配置虛擬主機方法在windows和linux有些差異,今天給大家演示如何在windows上配置Apache虛擬主機,此方法主要適用於獨立的Apache環境。
1. 打開目錄Apache的安裝目錄/conf/extra/, 找到 httpd-vhosts.conf 文件;
2. 添加一段代碼來指定某一域名的網站,如圖我配置虛擬域名是:
project.com,文件目錄指向的是CI框架;
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "D:/AMP/Apache/htdocs/ci"
ServerName project.com
ErrorLog "logs/project.com-error.log"
CustomLog "logs/project.com-access.log" common
</VirtualHost>
3. 打開 httpd.conf 文件, 添加如下代碼:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
如果已存在,將Include前面的#去掉;
5、如何配置 Apache 的虛擬主機
1、基於ip地址的虛擬主機
復制代碼代碼如下:
Listen 80
<VirtualHost 172.20.30.40>
DocumentRoot /home/httpd/html1
ServerName www.ok1.com
ErrorLog /usr/local/apache/logs/error1_log
CustomLog /usr/local/apache/logs/access1_log combined
</VirtualHost>
<VirtualHost 172.20.30.50>
DocumentRoot /home/httpd/html2
ServerName www.ok2.com
ErrorLog /usr/local/apache/logs/error2_log
CustomLog /usr/local/apache/logs/access2_log combined
</VirtualHost>
2、基於IP 和多埠的虛擬主機配置
復制代碼代碼如下:
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
<VirtualHost 172.20.30.40:80>
DocumentRoot /www/example1-80
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example1.org
</VirtualHost>
<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example2.org
</VirtualHost>
3、單個IP 地址的伺服器上基於域名的虛擬主機配置
復制代碼代碼如下:
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
</VirtualHost>
如果您感覺上面的文章還不夠詳細可以看下下面的文章:
實驗目標:在apache實現基於域名的虛擬主機
實驗用的XAMPP版本為1.7.7,內含apache版本為2.2.21
實驗前准備:
1. 為了測試不同的域名,在Windows/System32/drivers/etc/下覓得hosts文件,在其中添加實驗用的域名若干,如 -
復制代碼代碼如下:
127.0.0.1 test1.net
127.0.0.1 test2.net
如此,則在瀏覽器中輸入該倆域名時,Windows將其解析為127.0.0.1本地地址。即,在瀏覽器中訪問localhost, test1.net, test2.net均可訪問XAMPP的歡迎頁。
2. 在apache目錄下建立目錄,以放置您不同的網站。為保護XAMPP原有的htdocs中的歡迎頁內容,實驗另外建立了與htdocs平級的htdocs1目錄,在其下建立了test1.net, test2.net兩個子目錄用以放置實驗用的網站。如下 -
apache/htdocs1/test1.net - 放置test1.net網站內容
apache/htdocs1/test2.net - 放置test2.net網站內容
在這兩個目錄中各新建hello world一網頁 index.html,內容 -
復制代碼代碼如下:
<HTML>
<HEAD></HEAD>
<BODY>
<H1>hello~, 這是[對應的網站名,用以區別].net</H1></BODY>
</HTML>
實驗步驟:
1. 找到apache/conf/httpd.conf, 將其中的
ServerAdmin
ServerName
DocumentRoot
注釋掉。
2. 在httpd.conf中,找到行
Include "conf/extra/httpd-vhosts.conf"
如被注釋則解注。該文件記載了虛擬主機的參數。[以前虛擬主機參數是直接填寫在httpd.conf中的,為了更好地組織文件,將其分離出去,類似於某些編程語言一樣。因此httpd.conf中include它,即相當於把它的內容填在了httpd.conf中。]
3. 這個httpd-vhosts.conf文件格式基本如下 -
復制代碼代碼如下:
#blah-blah
NameVirtualHost *:80
#blah-blah
#blah-blah
<VirtualHost *:80>
ServerAdmin XXXXXXXX
DocumentRoot "XXXXXXXX"
ServerName XXXXXXX
ServerAlias XXXXXX
ErrorLog "logs/XXXXXX-error.log"
CustomLog "logs/XXXXXXX-error.log" combined
</VirtualHost>
需要修改的,就是<VirtualHost>中的參數了。這個可以參見apache官方文檔。根據實驗域名,可以增加兩個<VirtualHost>:
復制代碼代碼如下:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "C:/xampp/htdocs1/test1.net"
ServerName test1.net
ServerAlias www.test1.net
ErrorLog "logs/test1-error.log"
CustomLog "logs/test1-access.log" combined
<Directory "C:/xampp/htdocs1/test1.net">
order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "C:/xampp/htdocs1/test2.net"
ServerName test2.net
ServerAlias www.test2.net
ErrorLog "logs/test1-error.log"
CustomLog "logs/test1-access.log" combined
<Directory "C:/xampp/htdocs1/test2.net">
order allow,deny
allow from all
</Directory>
</VirtualHost>
注意,如果不在各VirtualHost中定義Directory的可訪問性,你將遇到的是Access Forbidden!就連原來的localhost也是。
4. 由於之前注釋掉了httpd.conf中的ServerName, DocumentRoot等,為了仍然能以localhost訪問原XAMPP歡迎頁,就在增加一個VirtualHost,如下 -
復制代碼代碼如下:
<VirtualHost *:80>
ServerAdmin adm@localhost
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" combined
<Directory "C:/xampp/htdocs">
order allow,deny
allow from all
</Directory>
</VirtualHost>
為了避免出錯,把它放置在第一個Virtualhost位置。
至此,apache基於域名的虛擬主機配置完成。可以通過http://localhost訪問XAMPP歡迎頁,通過http://test1.net和http://test2.net訪問各自的主頁。
#
# 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.apache.org/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>
ServerAdmin [email protected]
DocumentRoot "E:/sky/apache2/htdocs"
ServerName localhost
ServerAlias www.sky.com
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" combined
<Directory "E:/sky/apache2/htdocs">
order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "E:/sky/apache2/htdocs/project1"
ServerName project1.com
ServerAlias www.project1.com
ErrorLog "logs/project1-error.log"
CustomLog "logs/project1-access.log" combined
<Directory "E:/sky/apache2/htdocs/project1">
order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "E:/sky/apache2/htdocs/zendTest/public"
ServerName zendTest.com
ServerAlias www.zendTest.com
DirectoryIndex index.php
<Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "E:/sky/apache2/htdocs/testRewrite"
ServerName testRewrite.com
ServerAlias www.testRewrite.com
# DirectoryIndex index.php
<Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "E:/sky/apache2/htdocs/test"
ServerName test.com
ServerAlias www.test.com
ErrorLog "logs/zendTest-error.log"
CustomLog "logs/zendTest-access.log" combined
<Directory "E:/sky/apache2/htdocs/test">
order allow,deny
allow from all
</Directory>
</VirtualHost>
6、apache怎麼配置localhost的虛擬主機
修改 hosts 文件
1
不管你用的是什麼系統,windows, mac,電腦上都會有一個 hosts 文件,修改這個文件,可以改變主機名所對應的 ip 地址。比如你安裝了 Web 開發環境(MAMP 或 WAMP),啟動環境以後,訪問 http://localhost 你就會打開環境的默認的目錄,這個 localhost 就是在 hosts 文件里定義的,它所指向的 ip 地址是 127.0.0.1 ,這個 ip 地址表示本地的你正在使用的這台電腦,了解詳細的內容,可以搜索一下 :)
修改這個 hosts 文件,我們可以手工的去指定任何主機名指向任何的 ip 地址,當然這個設置隻影響你自己的電腦,比如我們可以隨便造一個域名,比如 hello.com,讓這個域名指向你的電腦,也就是 127.0.0.1 這個 ip 地址。這樣你在瀏覽器中請求 hello.com ,打開的就是你在本地電腦上搭建的環境所指定的目錄里的東西。為了測試 apache 的虛擬主機功能,你可以去虛擬幾個這樣的域名,這樣在創建虛擬主機的時候,你可以讓這些域名打開各自的目錄。下面,我們先去修改這個 hosts 文件。
1. 找到 hosts 文件,windows 系統是在 Windows\System32\Drivers\etc 這個目錄下,Mac 系統是在 /etc/hosts ,找到以後可以使用文本編輯軟體打開它。
2. 在 hosts 文件里,新的一行上,先輸入 ip 地址 127.0.0.1 ,然後空格,再輸入一個主機名,這個主機名可以隨便,比如 hello.com ,這樣訪問 hello.com 就會打開在你的電腦上的網站了。另起一行,再去定義一條,127.0.0.1 hola.com 。
3. 保存修改之後的 hosts 文件,然後啟動在本地上的 Web 環境,打開瀏覽器,在地址欄上輸入 hello.com ,還有 hola.com ,看看打開的是不是你在本地上的內容。現在,你看到的應該跟你使用 http://localhost 打開的是一樣的東西。
END
配置 Apache 虛擬主機
現在,我們虛擬了兩個域名,讓它們指向了本地的電腦,訪問它們,打開的是同一個目錄里的內容,下面這個練習,我們通過修改 Apache 的配置文件,讓訪問不同域名的時候,打開的是不同的目錄。你首先要判斷一下配置 Apache 虛擬主機的配置文件,一般是在 httpd.conf 這個主配置文件裡面,不過,很多環境都會單獨把虛擬主機放在一個配置文件裡面,然後在 httpd.conf 文件里指定一下,包含這個單獨配置虛擬主機的文件。
1. 找到配置虛擬主機的配置文件,如果你用的是 WAMP 搭建的環境,先找到並打開 httpd.conf ,在這個文件里,搜索 # Virtual hosts ,它下面有一行代碼, #Include conf/extra/httpd-vhosts.conf... 你可以去掉它前面的 # ,這樣,Apache 啟動以後,也會去載入這個httpd-vhosts.conf 配置文件,這樣做的好處就是可以保持 Apache 相關設置的整潔。當然,你完全可以在主配置文件 httpd.conf 裡面去設置虛擬主機。
2. 打開 httpd-vhosts.conf 這個文件,沒有這個文件也可以去創建一個,或者直接在 httpd.conf 文件裡面去配置虛擬主機。配置虛擬主機的東西應該像這樣:
3. 應該有兩段這樣的代碼,每一段都是在配置一個虛擬主機,你可以復制其中的一段,然後粘貼到文件的最下面。再根據自己的需要去修改這段代碼。理解它的意思,ServerAdmin 就是網站的管理員的郵箱,DocumentRoot 是虛擬主機的主目錄,也就是訪問這個虛擬主機所打開的那個目錄,這個目錄你可以自己去指定,要注意的是,你需要先去創建這個目錄,然後再在這里去指定這個目錄的位置。ServerName 是主機名,比如之前我們虛擬的那個主機名 hello.com,ServerAlias 是主機的別名,你可以指定一個其它的主機名,訪問它的時候也會打開這個虛擬主機的目錄里的東西。ErrorLog 是錯誤的日誌,也就是發生錯誤的時候,會把錯誤記錄到哪個文件里。CustomLog 是虛擬主機訪問的日誌。
這樣自己新添加的虛擬主機看起來應該像這樣:
3
4. 在瀏覽器里打開 hello.com ,訪問的是這個虛擬主機的配置里 DocumentRoot 所指定的 /Applications/MAMP/htdocs/hello 這個目錄里的東西,你可以在這個目錄裡面放點東西,比如一個 WordPress 或者 Drupal 的網站等等。
註:配置虛擬主機的時候,第一個虛擬主機是默認的,你需要留著,配置自己的虛擬主機,可以從第二個開始配置,也就是第二段 <VirtualHost> 代碼。
7、Apache怎麼配置虛擬主機
echo "NameVirtualHost *:80">>/etc/httpd/conf/httpd.conf
8、linux操作系統Apache配置虛擬主機
/etc/httpd/conf.d/ 在這個目錄中新建一個 後綴為.conf
<VirtualHost *:7744> //774偵聽的埠
DocumentRoot /var/www/html/redmine //網頁文件目錄
ServerName 192.168.0.1 //主機
</VirtualHost>
然後保存重啟httpd服務 !