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服务 !