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

默认的初始站点配置
安装后,有一个默认站点,其配置都在的主配置文件(httpd.conf)中,主要包括如下几项 。
1、站点域名:
## 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# 现在我们需要自定义一个 ServerNameServerName 127.0.0.1
2、站点位置(文件夹位置) :
## 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、站点文件夹的访问权限设置: …
设置访问形式如下:
// 三项为固定写法 前面 key 后面 配置参数Options 设置项AllowOverride 设置项Require 权限设置项// DirectoryIndex index.html 配置首页也是可以放进来的,也可以但放出去// 各项解释如下Options: 用于设置一些系统选项 ,通常window系统中就用Indexes就可以了 。Options Indexes // 表示允许列出目录结构(如果没有可显示的网页)AllowOverride: 用于设置“可覆盖性”,即是否允许在项目文件中覆盖这里的访问权限设置:AllowOverride All //表示可覆盖AllowOverride None //表示不可覆盖Require: 用于设置可访问权限,常用的有:● 允许所有来源的访问: 推荐Require all granted● 拒绝所有来源的访问: 网站需要临时关闭时使用Require all denied● 允许所有但拒绝部分来源的访问:Require all grantedRequire not ip 192.168.1.102 192.168.1.103● 拒绝所有但允许部分来源的访问:Require all deniedRequire ip 192.168.1.102 192.168.1.103
## 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 MultiviewsMultiviewsMatch 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
4、站点默认显示的网页(首页) :
# DirectoryIndex: sets the file that Apache will serve if a directory# is requested.#DirectoryIndex index.html// 也可以写成多个首页// 这样在打开网页的之后会已配置先后顺序一个一个文件名的找你所配置的首页文件是否存在,存在就打开DirectoryIndex index.php index.html default.php default.html ...
我们可以理解为:
: 是对外看的,也就是域名给别人用的 。
: 其实指向的就是路径,相当于=。
:能管理这个路径的访问权限,
:设置这个路径默认要展示文件 。
1、在的主配置文件(httpd.conf) ,引入多站点的配置文件(虚拟主机配置文件),在 httpd.conf 搜索 “httpd-.conf”,打开注释:
# Virtual hostsInclude /private/etc/apache2/extra/httpd-vhosts.conf
httpd.conf 中还有一个文件 “.so” ,这个文件专门解决多次或者重复配置的站点,也就是可以写一个站点模板,里面都是自适应填充站点内容找到对应文件,这个可以单独去了解一下,这里我就不打开了,但是推荐使用这个 。

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

文章插图
【PHP Apache - 多站点虚拟主机配置】#LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
2、在虚拟主机配置文件 (httpd-.conf)中,再挨个网站进行配置(每个网站一段配置),我们找到 httpd-.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 # # 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 block.## #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# # #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#
# 默认都是 80 端口,如果你的端口不是可以进行修改# 站点1:(第一个站点,被称为默认站点)# 域名ServerName localhost# 域名根目录DocumentRoot "/Users/dengzemiao/Sites"# 域名根目录权限# 运行列出目录(正式服务器需要去掉 Indexes)Options Indexes FollowSymLinks# 运行权限覆盖AllowOverride All# 运行所有人访问Require all granted# 域名根目录默认显示文件DirectoryIndex index.html index.php
# 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 # # 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 block.## #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# # #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# # 自定义多个站点配置# 默认都是 80 端口,如果你的端口不是可以进行修改# 站点1:(第一个站点,被称为默认站点)(一般都会配置一个 localhost 方便访问主目录)# 域名ServerName localhost# 域名根目录DocumentRoot "/Users/dengzemiao/Sites"# 域名根目录权限# 运行列出目录(正式服务器需要去掉 Indexes)Options Indexes FollowSymLinks# 运行权限覆盖AllowOverride All# 运行所有人访问Require all granted# 域名根目录默认显示文件DirectoryIndex index.html index.php# 站点2:# 域名ServerName www.dzm.com# 错误日志ErrorLog "/Users/dengzemiao/Sites/dzm/error.log"# 成功日志CustomLog "/Users/dengzemiao/Sites/dzm/access.log" combined# 域名根目录DocumentRoot "/Users/dengzemiao/Sites/dzm"# 域名根目录权限# 运行列出目录(正式服务器需要去掉 Indexes)Options Indexes FollowSymLinks# 运行权限覆盖AllowOverride All# 运行所有人访问Require all granted# 域名根目录默认显示文件DirectoryIndex index.html index.php# 站点3:# 域名ServerName www.xyq.com# 错误日志ErrorLog "/Users/dengzemiao/Sites/xyq/error.log"# 成功日志CustomLog "/Users/dengzemiao/Sites/xyq/access.log" combined# 域名根目录DocumentRoot "/Users/dengzemiao/Sites/xyq"# 域名根目录权限# 运行列出目录(正式服务器需要去掉 Indexes)Options Indexes FollowSymLinks# 运行权限覆盖AllowOverride All# 运行所有人访问Require all granted# 域名根目录默认显示文件DirectoryIndex index.html index.php
/private/etc
# 自定义多个站点配置# 默认都是 80 端口,如果你的端口不是可以进行修改# 站点1:(第一个站点,被称为默认站点)(一般都会配置一个 localhost 方便访问主目录)# 域名ServerName localhost# 域名根目录DocumentRoot "/Users/dengzemiao/Desktop/Project/php/Sites"# 域名根目录权限# 运行列出目录(正式服务器需要去掉 Indexes)Options Indexes FollowSymLinks# 运行权限覆盖AllowOverride All# 运行所有人访问Require all granted# 域名根目录默认显示文件DirectoryIndex index.html index.php# 站点2:# 域名ServerName www.dzm.com# 域名根目录DocumentRoot "/Users/dengzemiao/Sites/dzm"# 域名根目录权限# 运行列出目录(正式服务器需要去掉 Indexes)Options Indexes FollowSymLinks# 运行权限覆盖AllowOverride All# 运行所有人访问Require all granted# 域名根目录默认显示文件DirectoryIndex index.html index.php# 站点3:# 域名ServerName www.xyq.com# 域名根目录DocumentRoot "/Users/dengzemiao/Sites/xyq"# 域名根目录权限# 运行列出目录(正式服务器需要去掉 Indexes)Options Indexes FollowSymLinks# 运行权限覆盖AllowOverride All# 运行所有人访问Require all granted# 域名根目录默认显示文件DirectoryIndex index.html index.php