Apache配置多主机和虚拟目录

相当广泛的需求吧,一个开发机上需要同时发布多个项目=.=,找到一篇文章,根据该文折腾了一下,挺好使的,此处做一下笔记

虚拟目录

假设一个网站的网站根目录在 D:\Program Files\Web ,现在又有一个新的web应用,其目录在 D:\Program Files\demo ,为了此处能够同时访问根目录下的web应用和demo下的web应用,此处可使用虚拟目录实现。
打开http.conf,搜索节点,然后在节点中添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#下面是虚拟目录声明格式
#Alias用来定义虚拟目录及虚拟目录路径,其中虚拟目录名称用于URL访问的路径别名,可以和虚拟目录名称不同
#<Directory/>节点用于定义目录的访问权限等
#
#Alias 虚拟目录名称 虚拟目录路径
#<Directory 虚拟目录路径>
# Options Indexes FollowSymLinks
# AllowOverride All
# Order allow,deny
# Allow from all
#</Directory>

#下面是具体的示例,/DemoSite是目录别名 "D:/Program Files/demo"是虚拟目录的实际路径
Alias /DemoSite "D:/Program Files/demo"

<Directory "D:/Program Files/demo">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

重启Apache服务,在浏览器输入:http://localhost/demo 即可访问
和参考文档中相比,配置默认端口故URL中不输入URL
附上一个完整的节点参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<IfModule alias_module>
#
# Redirect: Allows you to tell clients about documents that used to
# exist in your server's namespace, but do not anymore. The client
# will make a new request for the document at its new location.
# Example:
# Redirect permanent /foo http://www.example.com/bar

#
# Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path
#
# If you include a trailing / on /webpath then the server will
# require it to be present in the URL. You will also likely
# need to provide a <Directory> section to allow access to
# the filesystem path.

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "D:/Program Files (x86)/xampp/cgi-bin/"

Alias /betme "D:/PHP/laravel/laravel2016/betMe/public"

<Directory "D:/PHP/laravel/laravel2016/betMe/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</IfModule>

多主机绑定

即一个端口绑定多个域名,通过域名访问不同的目录。
打开http.conf文件,在文件的末尾添加以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#多主机头配置无需放在特定的节点下面,一般直接在配置文件底部添加即可
#NameVirtualHost addr[:port] 为一个基于域名的虚拟主机指定一个IP地址(和端口)
#声明主机头必须加这条指令,否者主机头配置不会生效
#VirtualHost节点下面ServerName就是要绑定的域名,DocumentRoot表示此域名指向的目录
#本机测试的话请在hosts中进行域名绑定如 127.0.0.1 www.mysite1.com

NameVirtualHost *:8080
<VirtualHost *:8080>
ServerName www.mysite1.com
DocumentRoot "D:\Program Files\Apache2.2\htdocs"
</VirtualHost>

<VirtualHost *:8080>
ServerName www.mysite2.com
DocumentRoot "D:\Code\MySite"
</VirtualHost>

配置完成后,重启Apache浏览器输入www.mysite1.com:8080,就会自动定向到D:\Program Files\Apache2.2\htdocs站点了
输入www.mysite2.com:8080就会自动定向到D:\Code\MySite站点,如此就可以实现在一个服务器上同时运行多个站点

引用

Apache配置虚拟目录和多主机头:http://www.cnblogs.com/lzrabbit/archive/2013/03/05/2944804.html