您当前的位置:首页 > 计算机 > 服务器 > Apache

PHP Apache - 多站点虚拟主机配置

时间:02-22来源:作者:点击数:
城东书院 www.cdsy.xyz
默认的初始站点配置

Apache 安装后,有一个默认站点,其配置都在 Apache 的主配置文件(httpd.conf)中,主要包括如下几项。

1、站点域名: ServerName

#
# 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 -》 默认配置就是 127.0.0.1
# 现在我们需要自定义一个 ServerName
ServerName 127.0.0.1

2、站点位置(文件夹位置) : DocumentRoot

#
# 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.
#
# /Library/WebServer/Documents -》 默认根目录
# /Users/dengzemiao/Sites -》 自定义根目录
DocumentRoot "/Users/dengzemiao/Sites"

3、站点文件夹的访问权限设置: …

Drectory 设置访问形式如下:

// 三项为固定写法 前面 key 后面 配置参数
<Directory "要设置权限的文件夹路径">
    Options 设置项
    AllowOverride 设置项
    Require 权限设置项
    // DirectoryIndex index.html 配置首页也是可以放进来的,也可以但放出去
<Directory>

// 各项解释如下
Options: 用于设置一些系统选项 ,通常window系统中就用Indexes就可以了。
  Options Indexes // 表示允许列出目录结构(如果没有可显示的网页)

AllowOverride: 用于设置“可覆盖性”,即是否允许在项目文件中覆盖这里的访问权限设置:
  AllowOverride All //表示可覆盖
  AllowOverride None //表示不可覆盖

Require: 用于设置可访问权限,常用的有:

    ● 允许所有来源的访问: 推荐
      Require all granted

    ● 拒绝所有来源的访问: 网站需要临时关闭时使用
      Require all denied

    ● 允许所有但拒绝部分来源的访问:
      <RequireAll>
        Require all granted
        Require not ip 192.168.1.102 192.168.1.103
      </RequireAll>

    ● 拒绝所有但允许部分来源的访问:
      <RequireAny>
        Require all denied
        Require ip 192.168.1.102 192.168.1.103
      </RequireAny>
<Directory "/Users/dengzemiao/Sites">
    #
    # 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 Multiviews
    MultiviewsMatch Any

    #
    # 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 None

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

4、站点默认显示的网页(首页) : DirectoryIndex

# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

// 也可以写成多个首页
// 这样在打开网页的之后会已配置先后顺序一个一个文件名的找你所配置的首页文件是否存在,存在就打开
<IfModule dir_module>
    DirectoryIndex index.php index.html default.php default.html ...
</IfModule>

我们可以理解为:

ServerName: 是对外看的,也就是域名给别人用的。

DocumentRoot:ServerName 其实指向的就是 DocumentRoot 路径,相当于 ServerName = DocumentRoot。

Drectory:能管理这个路径的访问权限,

DirectoryIndex:设置这个路径默认要展示文件。


  • 多站点虚拟主机的含义
    多站点虚拟主机: 是指在一台物理意义上的电脑(服务器)中,配置多个网站站点,并都可以对外提供“web访问服务”,外界看起来就是有多个站点(或多个服务器)的样子!每个站点,也就是类似一个主机,这就是虚拟主机。

  • 配置多站点虚拟主机
    配置多站点虚拟主机可以分两步:

1、在apache的主配置文件(httpd.conf) ,引入多站点的配置文件(虚拟主机配置文件),在 httpd.conf 搜索 “httpd-vhosts.conf”,打开注释:

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

httpd.conf 中还有一个文件 “mod_vhost_alias.so” ,这个文件专门解决多次或者重复配置的站点,也就是可以写一个站点模板,里面都是自适应填充站点内容找到对应文件,这个可以单独去了解一下,这里我就不打开了,但是推荐使用这个。

#LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

2、在虚拟主机配置文件 (httpd-vhosts.conf)中,再挨个网站进行配置(每个网站一段配置),我们找到 httpd-vhosts.conf 文件打开,我们把里面的所有东西都注释掉,自己来配置。

# Virtual Hosts
#
# Required modules: mod_log_config

# 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:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# 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 webmaster@dummy-host.example.com
#     DocumentRoot "/usr/docs/dummy-host.example.com"
#     ServerName dummy-host.example.com
#     ServerAlias www.dummy-host.example.com
#     ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
#     CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
# </VirtualHost>

# <VirtualHost *:80>
#     ServerAdmin webmaster@dummy-host2.example.com
#     DocumentRoot "/usr/docs/dummy-host2.example.com"
#     ServerName dummy-host2.example.com
#     ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
#     CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
# </VirtualHost>
  • 自定义多个站点配置格式:
# 默认都是 80 端口,如果你的端口不是可以进行修改
# 站点1:(第一个站点,被称为默认站点)
<VirtualHost *:80>
    # 域名
    ServerName localhost
    # 域名根目录
    DocumentRoot "/Users/dengzemiao/Sites"
    # 域名根目录权限
    <Directory "/Users/dengzemiao/Sites">
        # 运行列出目录(正式服务器需要去掉 Indexes)
        Options Indexes FollowSymLinks
        # 运行权限覆盖
        AllowOverride All
        # 运行所有人访问
        Require all granted
    </Directory>
    # 域名根目录默认显示文件
    DirectoryIndex index.html index.php
</VirtualHost>
  • 我在 httpd-vhosts.conf 里面配置了三个站点:
# Virtual Hosts
#
# Required modules: mod_log_config

# 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:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# 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 webmaster@dummy-host.example.com
#     DocumentRoot "/usr/docs/dummy-host.example.com"
#     ServerName dummy-host.example.com
#     ServerAlias www.dummy-host.example.com
#     ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
#     CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
# </VirtualHost>

# <VirtualHost *:80>
#     ServerAdmin webmaster@dummy-host2.example.com
#     DocumentRoot "/usr/docs/dummy-host2.example.com"
#     ServerName dummy-host2.example.com
#     ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
#     CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
# </VirtualHost>

# 自定义多个站点配置
# 默认都是 80 端口,如果你的端口不是可以进行修改
# 站点1:(第一个站点,被称为默认站点)(一般都会配置一个 localhost 方便访问主目录)
<VirtualHost *:80>
    # 域名
    ServerName localhost
    # 域名根目录
    DocumentRoot "/Users/dengzemiao/Sites"
    # 域名根目录权限
    <Directory "/Users/dengzemiao/Sites">
        # 运行列出目录(正式服务器需要去掉 Indexes)
        Options Indexes FollowSymLinks
        # 运行权限覆盖
        AllowOverride All
        # 运行所有人访问
        Require all granted
    </Directory>
    # 域名根目录默认显示文件
    DirectoryIndex index.html index.php
</VirtualHost>

# 站点2:
<VirtualHost *:80>
    # 域名
    ServerName www.dzm.com
    # 错误日志
    ErrorLog "/Users/dengzemiao/Sites/dzm/error.log"
    # 成功日志
    CustomLog "/Users/dengzemiao/Sites/dzm/access.log" combined
    # 域名根目录
    DocumentRoot "/Users/dengzemiao/Sites/dzm"
    # 域名根目录权限
    <Directory "/Users/dengzemiao/Sites/dzm">
        # 运行列出目录(正式服务器需要去掉 Indexes)
        Options Indexes FollowSymLinks
        # 运行权限覆盖
        AllowOverride All
        # 运行所有人访问
        Require all granted
    </Directory>
    # 域名根目录默认显示文件
    DirectoryIndex index.html index.php
</VirtualHost>

# 站点3:
<VirtualHost *:80>
    # 域名
    ServerName www.xyq.com
    # 错误日志
    ErrorLog "/Users/dengzemiao/Sites/xyq/error.log"
    # 成功日志
    CustomLog "/Users/dengzemiao/Sites/xyq/access.log" combined
    # 域名根目录
    DocumentRoot "/Users/dengzemiao/Sites/xyq"
    # 域名根目录权限
    <Directory "/Users/dengzemiao/Sites/xyq">
        # 运行列出目录(正式服务器需要去掉 Indexes)
        Options Indexes FollowSymLinks
        # 运行权限覆盖
        AllowOverride All
        # 运行所有人访问
        Require all granted
    </Directory>
    # 域名根目录默认显示文件
    DirectoryIndex index.html index.php
</VirtualHost>
  • 然后保存重启 Apache
  • 但是配置好之后还是不会生效的,我们还需要去做域名解析,也就是修改本地 hosts 文件,打开路径找到 hosts 文件
/private/etc
  • 将 hosts 文件拷贝到桌面在编辑,编辑好之后保存在拖进去覆盖
  • 然后重启 Apache 服务器,输入配置的站点,就打开成功了。

  • 注意:上面每个站点的目录都是在 /Users/dengzemiao/Sites 这个文件夹下面,这个文件夹是在 Apache 服务器配置文件里面我们之前配置好的根目录文件夹,我在这个根目录下面新建每一个文件夹我都可以设置成一个新的站点
  • 重点注意:我在MAC上将这个新站点目录不指向这个根目录下,而是指定到随意一个桌面文件夹下,会打不开站点,无法生效。
# 自定义多个站点配置
# 默认都是 80 端口,如果你的端口不是可以进行修改
# 站点1:(第一个站点,被称为默认站点)(一般都会配置一个 localhost 方便访问主目录)
<VirtualHost *:80>
    # 域名
    ServerName localhost
    # 域名根目录
    DocumentRoot "/Users/dengzemiao/Desktop/Project/php/Sites"
    # 域名根目录权限
    <Directory "/Users/dengzemiao/Desktop/Project/php/Sites">
        # 运行列出目录(正式服务器需要去掉 Indexes)
        Options Indexes FollowSymLinks
        # 运行权限覆盖
        AllowOverride All
        # 运行所有人访问
        Require all granted
    </Directory>
    # 域名根目录默认显示文件
    DirectoryIndex index.html index.php
</VirtualHost>

# 站点2:
<VirtualHost *:80>
    # 域名
    ServerName www.dzm.com
    # 域名根目录
    DocumentRoot "/Users/dengzemiao/Sites/dzm"
    # 域名根目录权限
    <Directory "/Users/dengzemiao/Sites/dzm">
        # 运行列出目录(正式服务器需要去掉 Indexes)
        Options Indexes FollowSymLinks
        # 运行权限覆盖
        AllowOverride All
        # 运行所有人访问
        Require all granted
    </Directory>
    # 域名根目录默认显示文件
    DirectoryIndex index.html index.php
</VirtualHost>

# 站点3:
<VirtualHost *:80>
    # 域名
    ServerName www.xyq.com
    # 域名根目录
    DocumentRoot "/Users/dengzemiao/Sites/xyq"
    # 域名根目录权限
    <Directory "/Users/dengzemiao/Sites/xyq">
        # 运行列出目录(正式服务器需要去掉 Indexes)
        Options Indexes FollowSymLinks
        # 运行权限覆盖
        AllowOverride All
        # 运行所有人访问
        Require all granted
    </Directory>
    # 域名根目录默认显示文件
    DirectoryIndex index.html index.php
</VirtualHost>
  • 这里我就将 localhost 的目录不是设置指向根目录下的文件夹了,重启服务器打开 localhost,就会发现没有权限,这个权限问题我后面在去研究吧,但是一般都是会放在根目录下的,这个可以当做扩展功能学习。
  • 多站点到这里就配置结束了!!!
城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门