apache多站点解决方案(同端口不同IP,同IP不同端口,多域名)

这里所有的操作都是基于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

结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冯伪猿

觉得文章不错?打赏支持一下吧。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值