1、配置文件说明
1.1 主配置文件目录
vi /etc/httpd/conf/httpd.conf
1.2 配置文件格式
# directive 指令 value 值
ServerRoot "/etc/httpd"
ServerRoot 代表apache服务的根路径,一般不修改。
2、配置项详解
2.1 ServerRoot
服务所在目录的路径,不需要做修改
ServerRoot "/etc/httpd"
2.2 Listen
监听端口
#Listen 12.34.56.78:80
Listen 80
配置语法
Listen [IP-address:]portnumber [protocol]
使用场景
默认监听80端口,浏览器访问127.0.0.1或localhost或服务器ip能得到默认欢迎页面。
也可以同时监听多个端口。
实践
# 1. 修改端口号
Listen 8080
# 2. Listen指令可重复出现多次
Listen 8080
Listen 80
# 注意:修改后必须重启服务才可生效
[root@localhost conf]# systemctl restart httpd.service
2.3 Include
导入配置文件
Include conf.modules.d/*.conf
2.4 IncludeOptional
和include功能相同,都是导入配置文件的。区别是IncludeOptional导入的路径有问题时会被忽略。不会报错。
IncludeOptional conf.d/*.conf
2.5 User和Group
httpd服务子进程启动时的账号和组,这个不用修改
User apache
Group apache
2.6 ServerAdmin
服务运行时的管理员邮箱地址
ServerAdmin root@localhost
2.7 DocumentRoot
站点根目录
在这个文件夹下放数据的
DocumentRoot "/var/www/html"
语法
DocumentRoot directory-path
实践
#DocumentRoot "/var/www/html"
DocumentRoot "/www"
#<Directory "/var/www/html">
<Directory "/www">
2.8 Directory
确定访问目录位置,标签配置。标签内是设置针对该目录的访问权限
<Directory "/var/www/html">
Options Indexes FollowSymLinks # 访问时的展示形式,Indexes索引展示
AllowOverride None # 设置指令是否可以在.htaccess使用
Require all granted # 允许所有人访问
</Directory>
-
Options 访问时展示形式
Options Indexes 当前目录下没有默认页面,就显示目录结构
Options FollowSymLinks 默认设置,允许访问符号链接
Options None 关闭
一般情况下会把Indexes 关掉,不想让别人看到站点目录下有什么文件,只有在做下载站点的时候才会打开。默认是开的。
-
AllowOverride
.htaccess
文件中允许的指令类型 AllowOverride All 全部指令
AllowOverride None 默认值,不允许
AllowOverride directive-type [directive-type] … 具体指令类型
-
Require 访问权限设置
Require all granted 无条件允许访问
Require all denied 无条件拒绝访问
Require method http-method [http-method] … 仅允许给定的HTTP方法访问
Require ip 10 172.20 192.168.2 指定ip地址范围的客户端可以访问
实践
# 1. 去掉Indexes查看效果,注意改完配置后要重启http服务
<Directory "/var/www/html">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
# 2. 去掉FollowSymLinks
<Directory "/var/www/html">
Options None
AllowOverride None
Require all granted
</Directory>
# 3. 使用Require
<Directory "/var/www/html">
Options None
AllowOverride None
Require all denied # 无条件拒绝访问
</Directory>
<Directory "/var/www/html">
Options None
AllowOverride None
Require method POST # 仅允许post请求
</Directory>
2.9 IfModule
以特定模块存在与否为条件的处理指令
# 如果dir_module存在,执行DirectoryIndex
<IfModule dir_module>
DirectoryIndex index.html # 站点默认展示页
</IfModule>
语法
DirectoryIndex disabled | local-url [local-url] …
默认
DirectoryIndex index.html
实践
# 在站点根目录下创建一个index.html
[root@localhost html]# echo 'index' > index.html
2.10 Files
包含适用于匹配文件名的指令
<Files ".ht*">
Require all denied # 以.ht开头的文件拒绝提供访问
</Files>
2.11 ErrorLog
错误日志记录位置
ErrorLog "logs/error_log"
2.12 LogLevel
错误日志记录级别
LogLevel warn
错误级别选项
水平 | 描述 |
---|---|
emerg |
紧急情况 - 系统无法使用。 |
alert |
必须立即采取行动。 |
crit |
关键条件。 |
error |
错误条件。 |
warn |
警告条件。 |
notice |
正常但重要的情况。 |
info |
基本信息 |
debug |
调试级消息 |
2.13 IfModule log_config_module
访问日志配置模块
<IfModule log_config_module>
# 访问日志3种格式: combined,common, combinedio
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio
</IfModule>
# 确定访问日志位置和使用哪种日志格式
CustomLog "logs/access_log" combined
</IfModule>
日志格式说明
标识 | 含义 |
---|---|
%h | 客户端ip |
%l | Remote User, 通常为一个减号(“-”); |
%u | Remote user (from auth; may be bogus if return status (%s) is 401);非为登录访问时,其为一个减号; |
%t | 服务器收到请求时的时间; |
%r | First line of request,即表示请求报文的首行;记录了此次请求的“方法”,“URL”以及协议版本; |
%>s | 响应状态码; |
%b | 响应报文的大小,单位是字节;不包括响应报文的http首部; |
%{Referer}i | 请求报文中首部“referer”的值;即从哪个页面中的超链接跳转至当前页面的; |
%{User-Agent}i | 请求报文中首部“User-Agent”的值;即发出请求的应用程序; |
2.14 IfModule alias_module
文档映射
<IfModule alias_module>
#
# Redirect: Allows you to tell clients about documents that used to
# Example:
# Redirect permanent /foo http://www.example.com/bar
# Alias: Maps web paths into filesystem paths and is used to
# Example:
# Alias /webpath /full/filesystem/path
# ScriptAlias: This controls which directories contain server scripts.
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" # cgi脚本映射
</IfModule>
Redirect 外部重定向
Alias 将url映射到文件系统个位置
ScriptAlias 将url映射到CGI脚本
实践
Redirect
Alias
ScriptAlias(特别注意给你的脚本加执行权限)
2.15 AddDefaultCharset
响应内容的编码格式
AddDefaultCharset UTF-8