apache怎么配置虚拟主机(Apache有哪两种虚拟主机的方式)
本文目录一览:
如何配置 Apache 的虚拟主机
打开httpd.conf文件
去掉LoadModule vhost_alias_module modules/mod_vhost_alias.so
这句前面的#号
和
Include conf/extra/httpd-vhosts.conf这句前面的#号
Include conf/extra/httpd-vhosts.conf这句在httpd.conf文件底部
然后打开Apache2.2\conf\extra下的httpd-vhosts.conf文件,加入如下代码
#配置自己的虚拟主机
VirtualHost *:80
#网站目录,如果在www目录下的php目录,下面 #就填写F:/www/php
DocumentRoot "D:/WALP/www"
#域名
ServerName
#这里配置欢迎首页面
DirectoryIndex index.html index.htm index.php
Directory /
Options FollowSymLinks
#不允许别人修改我们的页面
AllowOverride None
#设置访问权限
order allow,deny
Allow from all
/Directory
/VirtualHost
把ServerName改成你自己的,最后重启Apache。
apache虚拟主机怎样配置
Apache的虚拟主机就是在一台服务器上运行多个网站,每个虚拟主机都可以绑定独立的域名,为这些域名可以指定单独的目录,访问这些域名的时候,Apache会打开对应目录里面的东西。配置Apache的虚拟主机,只需要去修改Apache的配置文件。虚拟主机的英文是virtualhost,所以,Apache配置虚拟主机的文件可能是httpd.conf,也可能是跟virtualhost这个名字相关的文件,比如vhost.conf,具体要使用哪一个配置文件,需要自己去判断。
基于apache怎么配置虚拟主机
Apache 配置虚拟主机三种方式
一、基于IP
1. 假设服务器有个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
192.168.1.12
192.168.1.13
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
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
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
DocumentRoot /www/test3/
Directory "/www/test3"
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow From All
/Directory
/VirtualHost
复制代码
5. 大功告成,测试下每个虚拟主机,分别访问、、
二、基于主机名
1. 设置域名映射同一个IP,修改hosts:
192.168.1.10
192.168.1.10
192.168.1.10
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
DocumentRoot /www/test1/
Directory "/www/test1"
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerName
DocumentRoot /www/test2/
Directory "/www/test2"
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerName
DocumentRoot /www/test3/
Directory "/www/test3"
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
/Directory
/VirtualHost
复制代码
4. 大功告成,测试下每个虚拟主机,分别访问、、
三、基于端口
1. 修改配置文件
将原来的
Listen 80
改为
Listen 80
Listen 8080
2. 更改虚拟主机设置:
复制代码
VirtualHost 192.168.1.10:80
DocumentRoot /var/www/test1/
ServerName
/VirtualHost
VirtualHost 192.168.1.10:8080
DocumentRoot /var/www/test2
ServerName
/VirtualHost
如何配置apache虚拟主机的实例小结
实验目标:在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
H1hello~, 这是[对应的网站名,用以区别].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 adm@test1.net
DocumentRoot "C:/xampp/htdocs1/test1.net"
ServerName test1.net
ServerAlias
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 adm@test2.net
DocumentRoot "C:/xampp/htdocs1/test2.net"
ServerName test2.net
ServerAlias
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基于域名的虚拟主机配置完成。可以通过访问XAMPP欢迎页,通过和访问各自的主页。
#
# 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:;
# 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 kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs"
ServerName localhost
ServerAlias
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" combined
Directory "E:/skydao/apache2/htdocs"
order allow,deny
allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerAdmin kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs/project1"
ServerName project1.com
ServerAlias
ErrorLog "logs/project1-error.log"
CustomLog "logs/project1-access.log" combined
Directory "E:/skydao/apache2/htdocs/project1"
order allow,deny
allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerAdmin kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs/zendTest/public"
ServerName zendTest.com
ServerAlias
DirectoryIndex index.php
Directory /
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerAdmin kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs/testRewrite"
ServerName testRewrite.com
ServerAlias
# DirectoryIndex index.php
Directory /
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
/Directory
/VirtualHost
VirtualHost *:80
ServerAdmin kongdaoxian@gmail.com
DocumentRoot "E:/skydao/apache2/htdocs/test"
ServerName test.com
ServerAlias
ErrorLog "logs/zendTest-error.log"
CustomLog "logs/zendTest-access.log" combined
Directory "E:/skydao/apache2/htdocs/test"
order allow,deny
allow from all
/Directory
/VirtualHost
关于apache怎么配置虚拟主机和Apache有哪两种虚拟主机的方式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发表评论
暂时没有评论,来抢沙发吧~