这里所有的操作都是基于win10进行的。
准备工作
要实现apache部署多站点,首先我们要找到 httpd.conf 文件 并找到如下两行代码,把其注释 # 去除。
//在我的电脑上其在184行,各有差异,可通过搜索定位
# LoadModule vhost_alias_module modules/mod_vhost_alias.so
//在我的电脑上在523行,各有差异
# Include conf/extra/httpd-vhosts.conf
找到以下部分代码(原单站点配置项,并将其删除或注释掉)
DocumentRoot "F:\test\public"
<Directory "F:\test\public">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Includes ExecCGI
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
接下来打开 httpd-vhosts.conf 文件。并针对性的对其进行修改
同端口不同IP
将如下代码加入到文件中
<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "F:\test\ht1"
ServerName 127.0.0.1
<directory "F:\test\ht1">
Options FollowSymLinks
AllowOverride All
Require all granted
</directory>
#ServerAlias www.dummy-host.example.com
#ErrorLog "logs/dummy-host.example.com-error.log"
#CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "F:\test\ht2"
ServerName 127.0.0.2
<directory "F:\test\ht2">
Options FollowSymLinks
AllowOverride All
Require all granted
</directory>
#ServerAlias www.dummy-host.example.com
#ErrorLog "logs/dummy-host.example.com-error.log"
#CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
同IP不同端口
Listen 8080
<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "F:\test\ht1"
ServerName 127.0.0.1
<directory "F:\test\ht1">
Options FollowSymLinks
AllowOverride All
Require all granted
</directory>
#ServerAlias www.dummy-host.example.com
#ErrorLog "logs/dummy-host.example.com-error.log"
#CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
<VirtualHost *:8080>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "F:\test\ht2"
ServerName 127.0.0.1
<directory "F:\test\ht2">
Options FollowSymLinks
AllowOverride All
Require all granted
</directory>
#ServerAlias www.dummy-host.example.com
#ErrorLog "logs/dummy-host.example.com-error.log"
#CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
多域名访问
我们以 同端口不同IP 的配置实现多域名访问
<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "F:\test\ht1"
ServerName test1.test.com
<directory "F:\test\ht1">
Options FollowSymLinks
AllowOverride All
Require all granted
</directory>
#ServerAlias www.dummy-host.example.com
#ErrorLog "logs/dummy-host.example.com-error.log"
#CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "F:\test\ht2"
ServerName test2.test.com
<directory "F:\test\ht2">
Options FollowSymLinks
AllowOverride All
Require all granted
</directory>
#ServerAlias www.dummy-host.example.com
#ErrorLog "logs/dummy-host.example.com-error.log"
#CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
修改 C:\Windows\System32\drivers\hosts文件 如下
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
127.0.0.1 test1.test.com
127.0.0.1 test2.test.com
结束