apache配置文件
配置文件: 主配置文件:/etc/httpd/conf/httpd.conf 模块配置文件:/etc/httpd/conf.modules.d/*.conf
辅助配置文件:/etc/httpd/conf.d/*.conf
日志目录 /var/log/httpd/error_log #错误日志 /var/log/httpd/access_log #访问日志 站点文档目录 /var/www/html
常用配置
修改apache端口号
1.Listen:[ip]:PORT 省略ip表示监听本机所有ip;Listen可以出现多次
持久连接和非持久连接
2.持久连接 Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其他请求的完成 如何断开? 数量限制:100 时间限制:可配置(http2.4可以配置毫秒) 副作用:对并发访问量较大的服务器,持久连接功能会使有些请求得不到响应 折中:较短的持久连接时间 3.非持久连接 http1.0默认是非持久连接 http1.1默认是持久连接 KeepAlive On|Off MaxKeepAlivedRequests # KeepAliveTimeout # 4.测试 telnet 192.168.254.12 80 GET /URL HTTP/1.1 Host: HOSTNAME or IP
MPM
Multipath Process Module:多路处理模块
vim /etc/httpd/conf.modules.d/00-mpm.conf
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so prefork,worker,event 默认为/usr/sbin/httpd,其使用prefork 查看模块列表 查看静态编译的模块 # httpd -l Compiled in modules: 服务启动必须有这些模块 core.c mod_so.c http_core.c 查看静态编译及动态装载的模块 # httpd -M
站点访问控制
6.站点访问控制 可基于两种类型的路径指明对哪些资源进行访问控制 文件系统控制: <Directory ""> </Directory> <File ""> </File> <FileMatch ""> </FileMatch> URL路径: <location ""> </location> ... 访问控制机制: 基于来源地址: 基于账号:
基于"来源地址"的访问控制
Directory中"基于来源地址"实现访问控制 (1)Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews None All Indexes: 如果没有默认主页面也找不到自定义页面会显示索引页面 FollowSymLinks: 允许跟踪符号链接文件 (2)基于来源地址的访问控制机制 Require all granted
Require all deny 来源地址:
Require ip IPADDR
Require not ip IPADDR IPADDR: 192.168 192.168.0.0 192.168.0.0/24 192.168.0.0/255.255.255.0
示例:
<Directory "/var/www/admin">
AllowOverride None
Options None
#Require all granted
Require not ip 192.168.254.0/24
</Directory>
基于"用户"的访问控制
基于用户访问控制 <Directory "/var/test"> AllowOverride None Options None AuthType Basic AuthName "this is admin page" AuthUserFile "/etc/httpd/conf.d/.htpasswd" Require user admin1 admin2 admin3 </Directory> 创建访问授权账号 -c:自动创建htpasswd文件,因此,仅应该在添加第一个用户时使用 -m:md5加密 -D:删除用户 htpasswd -c -m /etc/httpd/conf.d/.htpasswd admin1 htpasswd -m /etc/httpd/conf.d/.htpasswd admin1
基于"组"的访问控制
12.基于组访问控制 <Directory "/var/test"> AllowOverride None Options None AuthType Basic AuthName "this is admin page" AuthUserFile "/etc/httpd/conf.d/.htpasswd" AuthGroupFile "/etc/httpd/conf.d/.htgroup" Require group webadmins </Directory>
虚拟主机
有三种实现方案: 基于IP: 为每个虚拟主机准备至少一个ip 基于port: 为每个虚拟主机准备至少一个port 基于hostname: 为每个虚拟主机准备至少一个hostname 可以混合使用上述三种方式中任意方式 note:一般虚拟主机不要和中心主机混用,所以要使用虚拟主机,请先禁用虚拟主机 禁用中心主机:注释DocumentRoot 每个虚拟主机都有专门配置: <VirtualHost "IP:PORT"> ServerName: DocumentRoot </VirtualHost>
示例:
<VirtualHost 192.168.254.12:80>
ServerName www2.cl7.com
DocumentRoot "/var/www/www2"
ErrorLog logs/www2-error_log
CustomLog logs/www2-access_log combiend
<Directory "/var/www/html1">
Options None
AllowOverride None
<RequireAll>
Require not ip 192.168.1.0/24
Require all granted
</RequireAll>
</Directory>
</VirtualHost>
定义默认主页面
DirectoryIndex index.html index.html.var
别名
Alias /admin/ "/var/www/admin/"
设置默认字符集
AddDefaultCharset UTF-8|GBK|GB2312|GB18030