Apache 安装后,有一个默认站点,其配置都在 Apache 的主配置文件f)中,主要包括如下几项。
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.
#
#ample: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 grantedRequire not ip 192.168.1.102 192.168.1.103</RequireAll>● 拒绝所有但允许部分来源的访问:<RequireAny>Require all deniedRequire 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# .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
</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的主配置文件(f) ,引入多站点的配置文件(虚拟主机配置文件),在 f 搜索 “f”,打开注释:
# Virtual hosts
Include /private/etc/apache2/f
#LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
2、在虚拟主机配置文件 (f)中,再挨个网站进行配置(每个网站一段配置),我们找到 f 文件打开,我们把里面的所有东西都注释掉,自己来配置。
# 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:.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 webmasterample
# DocumentRoot "/usr/ample"
# ample
# ServerAlias ample
# ErrorLog "/private/var/log/ample-error_log"
# CustomLog "/private/var/log/ample-access_log" common
# </VirtualHost># <VirtualHost *:80>
# ServerAdmin webmasterample
# DocumentRoot "/usr/ample"
# ample
# ErrorLog "/private/var/log/ample-error_log"
# CustomLog "/private/var/log/ample-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>
# 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:.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 webmasterample
# DocumentRoot "/usr/ample"
# ample
# ServerAlias ample
# ErrorLog "/private/var/log/ample-error_log"
# CustomLog "/private/var/log/ample-access_log" common
# </VirtualHost># <VirtualHost *:80>
# ServerAdmin webmasterample
# DocumentRoot "/usr/ample"
# ample
# ErrorLog "/private/var/log/ample-error_log"
# CustomLog "/private/var/log/ample-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# 错误日志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># 域名q# 错误日志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
注意:上面每个站点的目录都是在 /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# 域名根目录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># 域名q# 域名根目录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>
本文发布于:2024-02-03 03:22:39,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170690175948336.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |