Apache搭建多个站点方法详解

简介: www.111cn.net 编辑:Bolshevik 来源:转载 Apache的虚拟主机是一种允许在同一台机器上配置多个不同站点的web服务器环境的,就是iis一样可以创建多站点了,但是apache需要在编辑状态操作,不能像windows iis直接点击几下就好了,下面我来给各位介绍配置方法。
www.111cn.net 编辑:Bolshevik 来源:转载
Apache的虚拟主机是一种允许在同一台机器上配置多个不同站点的web服务器环境的,就是iis一样可以创建多站点了,但是apache需要在编辑状态操作,不能像windows iis直接点击几下就好了,下面我来给各位介绍配置方法。

最平常的大概有3种方法。

第一种:单IP不同端口

第二种:多IP同端口(独立IP的虚拟空间)

第三种:域名绑定根目录的方式(共享IP的虚拟空间)


Apache的核心配置文件名是”httpd.conf”,其所存放的路径在Apache目录下的conf文件夹下。修改它只需要使用记事本(建议使用其他编辑器,带行数的那种,方便修改),生效的话只需要保存httpd.conf,重启apache即可。

下面多站点支持的话,修改httpd.conf的第187~264行(不同的httpd.conf可能有差异),也就是在ServerAdmin和ServerName那里,大部分是注释。下面是主要修改的地方。

注意:如果是服务器请备份httpd.conf后再修改文件。

 代码如下 复制代码

# 'Main' server configuration
#
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# <VirtualHost> definition.  These values also provide defaults for
# any <VirtualHost> containers you may define later in the file.
#
# All of these directives may appear inside <VirtualHost> containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
#

#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. admin@your-domain.com
#
ServerAdmin admin@example.com

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80

#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other 
# <Directory> blocks below.
#
<Directory />
    AllowOverride All
    Require all denied
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "g:/www"
<Directory "g:/www">
    #
    # 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

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

第一种一般是测试环境,毕竟加了端口,如何绑定域名,访问的时候域名后面也需加端口。

例子分别通过80和8080访问不同的根目录。

大概在50几行有个Listen 80,在下面添加8080端口。

 代码如下 复制代码

Listen 80
Listen 8080<VirtualHost *:80> 
    ServerAdmin admin@myxzy.com 
    ServerName localhost:80 
    DocumentRoot "g:/www1" 
     <Directory "g:/www1"> 
     Options  Indexes FollowSymLinks 
     AllowOverride All 
     Require all granted 
   </Directory>   
</VirtualHost> 
<VirtualHost *:8080> 
    ServerAdmin admin@myxzy.com
    ServerName localhost:8080  
    DocumentRoot "g:/www2" 
   <Directory "g:/www2"> 
     Options Indexes FollowSymLinks 
     AllowOverride All 
     Require all granted 
   </Directory>       
</VirtualHost>

第二种多IP同端口。

IP地址1:192.168.2.2

IP地址2:192.168.1.68

端口同是80端口。

 代码如下 复制代码

<VirtualHost 192.168.1.68:80> 
    ServerAdmin admin@myxzy.com 
    ServerName localhost:80 
    DocumentRoot "g:/www1" 
     <Directory "g:/www1"> 
     Options FollowSymLinks 
     AllowOverride All 
     Require all granted 
   </Directory>   
</VirtualHost> 
<VirtualHost 192.168.2.2:80> 
    ServerAdmin admin@myxzy.com
    ServerName localhost:80 
    DocumentRoot "g:/www2" 
   <Directory "g:/www2"> 
     Options FollowSymLinks 
     AllowOverride All 
     Require all granted 
   </Directory>       
</VirtualHost>

第三种同IP不同域名和根目录(域名的话修改本地host演示)。

 代码如下 复制代码


<VirtualHost 192.168.2.2:80> 
    ServerAdmin admin@myxzy.com 
    ServerName www.111cn.net 
    DocumentRoot "g:/www1" 
     <Directory "g:/www1"> 
     Options FollowSymLinks 
     AllowOverride All 
     Require all granted 
   </Directory>   
</VirtualHost> 
<VirtualHost 192.168.2.2:80> 
    ServerAdmin admin@myxzy.com
    ServerName www.111cn.net
    DocumentRoot "g:/www2" 
   <Directory "g:/www2"> 
     Options FollowSymLinks 
     AllowOverride All 
     Require all granted 
   </Directory>       
</VirtualHost>

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
目录
相关文章
|
Linux Apache Windows
Apache配置多个站点(虚拟主机)最简单的办法
包含配置文件 先找到你的Apache配置目录 Mac和Linux用户的配置目录一般是在: /etc/apach2 Windows用户的配置目录一般是和Apache安装目录在一起。
2097 0
|
7月前
|
安全 网络协议 Linux
在安卓Termux上搭建apache服务创建个人站点并且结合cpolar内网穿透
在安卓Termux上搭建apache服务创建个人站点并且结合cpolar内网穿透工具实现公网访问。
在安卓Termux上搭建apache服务创建个人站点并且结合cpolar内网穿透
|
4月前
|
消息中间件 Kafka Apache
Apache Flink消费Kafka数据时,可以通过设置`StreamTask.setInvokingTaskNumber`方法来实现限流
Apache Flink消费Kafka数据时,可以通过设置`StreamTask.setInvokingTaskNumber`方法来实现限流
71 1
|
9月前
|
Linux 网络安全 API
Linux系统之使用apache部署webserver下载站点
Linux系统之使用apache部署webserver下载站点
66 0
|
5月前
|
缓存 网络协议 Linux
linux安装apache并配置userid站点
linux安装apache并配置userid站点
48 0
|
7月前
|
存储 Apache 文件存储
Apache On Windows 如何使用阿里云NAS作为WEB站点目录
在Windows操作系统运行Apache WEB服务器的用户如何使用阿里云NAS共享作为WEB 站点目录。
209 0
|
Apache
给Apache虚拟主机增加端口的方法
给Apache虚拟主机增加端口的方法
105 0
|
云安全 消息中间件 监控
Apache Log4j2 高危漏洞应急响应处置方法汇总整理
Apache Log4j2 高危漏洞应急响应处置方法汇总整理
482 0
Apache Log4j2 高危漏洞应急响应处置方法汇总整理
|
Apache
org.apache.commons.lang.StringUtils的常用方法
org.apache.commons.lang.StringUtils的常用方法
875 0
Apache httpclient的execute方法调试
Apache httpclient的execute方法调试
Apache httpclient的execute方法调试

热门文章

最新文章

推荐镜像

更多