文章目录
一、Apache配置剖析
1.1 Apache连接保持
Apache连接保持相关参数
-
KeepAlive
- 是否打开连接保持, OFF关闭, ON打开
-
KeepAlive Timeout
- 一次连接多次请求之间的最大间隔时间,两次请求超过该时间连接断开
-
MaxKeepAliveRequests
- 一次长连接能够传输的最大请求数量
部署keepalive优化连接
[root@promote ~]# yum install -y httpd
[root@promote ~]# vim /etc/httpd/conf/httpd.conf
KeepAlive on
#KeepAlive off
KeepAliveTimeout 600
[root@promote ~]# systemctl restart httpd.service
1.2 Apache访问控制
1.2.1 Apache访问控制概述
- 作用
- 控制对网站资源的访问
- 为特定的网站目录添加访问授权
- 常用访问控制方式
- 客户机地址限制
- 用户授权限制
1.2.2 基于客户端地址的访问控制
-
使用Require配置项实现访问控制,按先后顺序限制
-
可用< Location >、< Directory >、< Files >、< Limit >配置段中
-
Require配置项的常见语法
Require all granted
Require all denied
Require local
Require [not] host <主机名或域名列表>
Require [not] ip <IP地址或网段列表>
'//使用not禁止访问时要将其置于<RequireAll></RequireAll>容器中,并在容器中指定相对应的限制策略'
示例:
[root@localhost ~]# cd /etc/httpd/conf
[root@localhost conf]# ls
httpd.conf magic
[root@localhost conf]# mkdir abc
[root@localhost conf]# ls
abc httpd.conf magic
[root@localhost conf]# cd abc
[root@localhost abc]# vim vhost.conf
<VirtualHost *:80>
...省略内容
<Directory "/var/www/html">
<RequireALL> '***只要里面有require all和not ,外面就要加标签,否则重启httpd服务会报错***'
Require not ip 192.168.100.100 '//表示不允许ip192.168.100.100访问'
Require all granted
<RequireALL>
</Directory>
</VirtualHost>
...省略内容
1.2.3 用户授权限制–创建用户认证数据库
- 创建用户认证数据库
[root@localhost ~]# cd /usr/local/httpd/
[root@localhost httpd]# htpasswd -c /etc/httpd/conf/.user jerry '-c 新建使用,再次建用户则不需要; 新建密码文件'
New password:
Re-type new password:
Adding password for user webadmin
[root@localhost httpd]# cat /usr/local/httpd/confl.user '确认用户数据库文件'
webadmin:$apr1$L53Ws/Y2$3L4xhs4zZKDbJb.9p 1Ng.
- 添加用户授权配置
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
......
<Directory "/usr/local/httpd/htdocs">
......
AuthName "DocumentRoot" '受保护的领域名称'
AuthType Basic '认证类型'
AuthUserFile /etc/httpd/conf/.user '用户认证账号文件'
Require valid-user '要求通过认证才能访问'
</Directory>
[root@localhost ~ ]# systemctl restart httpd
实验示例:
'#安装软件'
[root@localhost ~]# yum -y install httpd
'#查看是否有htpasswd'
[root@localhost ~]# which htpasswd
/usr/bin/htpasswd
'#创建密码'
[root@localhost httpd]# htpasswd -c /etc/httpd/conf/.user jerry
'#查看一下密码'
[root@localhost httpd]# cat /usr/local/httpd/confl.user
'#修改配置文件'
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
...... '将Require all granted注释掉'
AuthName "DocumentRoot" '受保护的领域名称'
AuthType Basic '认证类型'
AuthUserFile /etc/httpd/conf/.user '用户认证账号文件'
Require valid-user '要求通过认证才能访问'
</Directory>
'#启动服务,关闭核心防护,关闭防火墙'
[root@localhost httpd]# systemctl start httpd
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
浏览器验证:
二、Apache日志管理
2.1 日志分割
- 随着网站的访问量增大,默认情况下Apache的 单个日志文件也会越来越大
- 日志文件占用磁盘空间很大
- 查看相关信息不方便
- 对日志文件进行分割
- Apache自带rotatelogs分割工具实现
- 第三方工具cronolog分割
2.2.1 rotatelogs分割工具
-
配置网站的日志文件转交给rotatelogs分割处理
-
配置格式为
ErrorLog "| rotatelogs 命令的绝对路径 -l 日志文件路径/网站名-error_%Y%m%d.log 86400" '//which rotatelogs命令查看绝对路径,%Y%m%d表示年月日,86400表示一天的秒数'
CustomLog "| rotatelogs 命令的绝对路径 -l 日志文件路径/网站名-access_%Y%m%d.log 86400" combined
例如
[root@localhost logs]vim /etc/httpd/conf/httpd.conf
ErrorLog "| /usr/sbin/rotatelogs -l logs/error_%Y%m%d.log 86400"
实际生产环境中,一个服务器绝大多数对应N个子域名站点,为了方便统一管理,可以用虚拟主机的方式进行配置,并用网站名标识日志文件
- 日志文件的产生
- 服务安装后,不会生成日志文件不会产生
- 服务启动后,生成日志文件
- 访问服务后,日志文件会生成内容
日志分割实验:
实验环境
一台centos 7.6虚拟机,一台win 10虚拟机
实验过程
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# which rotatelogs
/usr/sbin/rotatelogs '日志的路径'
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Listen 14.0.0.44:80 '修改监听端口为虚拟机的地址,端口号为80 '
#Listen 80 '将ipv6的端口注释掉'
.......
ServerName www.yyc.com:80 '域名设置为www.yyc.com'
[root@localhost ~]# ls /var/log/httpd/ '没开启httpd服务时,是空目录'
[root@localhost ~]# systemctl start httpd.service '开启httpd服务'
[root@localhost ~]# ls /var/log/httpd/ '开启httpd服务后,就会生成两个日志文件'
access_log error_log
[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# cat access_log '没有人访问,日志就是空的'
打开一个win10客户机,将地址配置为如下:
DNS服务器设置为虚拟机的IP地址
打开网页搜索14.0.0.44,会出现apache的测试网页
[root@localhost httpd]# cat access_log '刷新几次网页后,会出现海量日志'
14.0.0.100 - - [15/Jul/2020:00:45:34 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363"
14.0.0.100 - - [15/Jul/2020:00:45:34 +0800] "GET /noindex/css/open-sans.css HTTP/1.1" 200 5081 "http://14.0.0.44/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363"
......
做日志分割:182行修改 217行,修改双引号内内容
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf
182 ErrorLog "| /usr/sbin/rotatelogs -l logs/www.yyc.com.error_%Y%m%d.log 86400"
217 CustomLog "| /usr/sbin/rotatalogs -l logs/www.yyc.com.access_%Y%m%d.log 86400" combined
[root@localhost httpd]# systemctl restart httpd
[root@localhost httpd]# httpd -t '检查语法'
Syntax OK
[root@localhost httpd]# ls /var/log/httpd/ '此时没有error_log'
access_log error_log www.yyc.com.error_20200715.log
[root@localhost httpd]# ls /var/log/httpd/ '进入win10刷新一下就会自动生成'
access_log error_log www.yyc.com.access_20200715.log www.yyc.com.error_20200715.log
[root@localhost httpd]# date '查看当前日期'
2020年 07月 15日 星期三 01:27:12 CST
[root@localhost httpd]# date -s 08/05/20 '可以修改'
2020年 08月 05日 星期三 00:00:00 CST
[root@localhost httpd]# systemctl restart httpd '重启httpd服务'
[root@localhost httpd]# ls /var/log/httpd/ '进入日志目录查看'
access_log www.yyc.com.access_20200715.log www.yyc.com.error_20200805.log
error_log www.yyc.com.error_20200715.log
[root@localhost httpd]# ls /var/log/httpd/ 'win10刷新网页,就会出现设置的时间的日志文件'
access_log www.yyc.com.access_20200715.log www.yyc.com.error_20200715.log
error_log www.yyc.com.access_20200805.log www.yyc.com.error_20200805.log
2.2.2 第三方工具cronolog
- 源码编译安装cronolog工具
- 配置网站日志文件转交给cronolog分割处理
- 配置格式
ErrorLog "l cronolog命令的绝对路径 日志文件路径/网站名-error-%Y%m%d.log'
......
CustomLog "I cronolog命令的绝对路径 日志文件路径/网站名-%Y%m%d.log" combinec
第三方工具cronolog实验:
实验环境
一台centos 7.6虚拟机,一台win 10虚拟机
实验过程
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
cronolog-1.6.2-14.el7.x86_64.rpm rh
[root@localhost opt]# rpm -ivh cronolog-1.6.2-14.el7.x86_64.rpm
[root@localhost opt]# which cronolog
/usr/sbin/cronolog
[root@localhost opt]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log www.yyc.com.access_20200715.log www.yyc.com.error_20200715.log
error_log www.yyc.com.access_20200805.log www.yyc.com.error_20200805.log
[root@localhost httpd]# rm -rf www*
[root@localhost httpd]# ls
access_log error_log
改规则:
[root@localhost httpd]# vim /etc/httpd/conf/httpd.conf
ErrorLog "| /usr/sbin/cronolog logs/www.yyc.com.error_%Y%m%d.log"
......
CustomLog "| /usr/sbin/cronolog logs/www.yyc.com.access_%Y%m%d.log " combined '不能加-l,不然 ls /var/log/httpd/不会产生log,刷新网页 ls /var/log/httpd/不会产生error_log'
[root@localhost httpd]# systemctl restart httpd
[root@localhost httpd]# ls /var/log/httpd/
access_log error_log www.yyc.com.error_20200805.log
[root@localhost httpd]# ls /var/log/httpd/ 'win10刷新网页,会自动产生log日志'
access_log error_log www.yyc.com.access_20200805.log www.yyc.com.error_20200805.log
[root@localhost httpd]# date -s 05/20/20 '修改日期'
2020年 05月 20日 星期三 00:00:00 CST
[root@localhost httpd]# systemctl restart httpd
[root@localhost httpd]# ls /var/log/httpd/ 'win10刷新网页,会自动生成修改后的日期日志'
access_log www.yyc.com.access_20200520.log www.yyc.com.error_20200520.log
error_log www.yyc.com.access_20200805.log www.yyc.com.error_20200805.log
2.2 AWStats日志分析
2.2.1 AWStats日志分析系统介绍
- perl语言(骆驼语言)开发的一款开源日志分析系统
- 可用来分析Apache,Samba,vsftpd,IIS等服务器的访问日志
- 信息结合crond等计划任务服务,可对日志内容定期进行分析
2.2.2 部署AWStats环境准备
-
环境部署
-
VMware软件
-
一台centos7虚拟机
-
一台Windows虚拟机
-
-
环境准备
-
WindowsDNS解析地址指向centos7
-
centos7安装bind和httpd
-
部署AWStats过程
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
配置dns服务
[root@localhost ~]# yum install bind httpd -y
[root@localhost ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "yyc.com" IN {
type master;
file "yyc.com.zone";
allow-update { none; };
};
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost yyc.com.zone
[root@localhost named]# vim yyc.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 14.0.0.44 '原有删掉加一行'
[root@localhost named]# systemctl start named
- win 10 验证域名解析
安装apache服务
[root@localhost named]# vim /etc/httpd/conf/httpd.conf
Listen 14.0.0.44:80
#Listen 80
......
ServerName www.yyc.com:80
[root@localhost named]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.html
<h1>this is test web</h1>
- 验证httpd服务
安装AWStats
[root@localhost html]# rz -E '脱入软件包'
rz waiting to receive.
[root@localhost html]# ls
awstats-7.6.tar.gz index.html
[root@localhost opt]# tar zxvf awstats-7.6.tar.gz
[root@localhost opt]# ls
awstats-7.6 awstats-7.6.tar.gz rh
[root@localhost opt]# mv awstats-7.6 /usr/local/awstats
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ls
awstats bin etc games include lib lib64 libexec sbin share src
[root@localhost local]# cd awstats/
[root@localhost awstats]# ls
docs README.md tools wwwroot
[root@localhost awstats]# cd tools/ 'tools工具'
[root@localhost tools]# ls
awstats_buildstaticpages.pl awstats_updateall.pl httpd_conf nginx xslt
awstats_configure.pl dolibarr logresolvemerge.pl urlaliasbuilder.pl
awstats_exportlib.pl geoip_generator.pl maillogconvert.pl webmin
进行安装:
[root@localhost tools]# ./awstats_configure.pl '进行安装'
Config file path ('none' to skip web server setup):
> /etc/httpd/conf/httpd.conf '声明路径,会把配置直接写入文件'
......
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y '输入y'
......
Your web site, virtual server or profile name:
> www.yyc.com
'其他全部是回车'
(多了一个文件/etc/awstats/awstats.www.yyc.com.conf,后面要改,http://localhost/awstats/awstats.pl?config=www.yyc.com
这是域名)
[root@localhost tools]# vim /etc/httpd/conf/httpd.conf
'大G到文末,awstatsd 的文件目录和配置都被写进去了'
<Directory "/usr/local/awstats/wwwroot">
Options None
AllowOverride None
# Order allow,deny '添加注释'
# Allow from all '添加注释'
Require all granted '添加一行'
</Directory>
[root@localhost tools]# cd /etc/awstats/
[root@localhost awstats]# ls
awstats.www.yyc.com.conf
[root@localhost awstats]# vim /etc/awstats/awstats.www.yyc.com.conf
LogFile="/var/log/httpd/access_log" '改名访问日志文件位置access_log'
...
DirData="/var/lib/awstats" 'awstats默认不存在(需要创建)'
[root@localhost awstats]# cd /var/lib/
[root@localhost lib]# ls '查看文件,并没有配置文件里的awstats目录,需要自己创建'
AccountsService colord fwupdate libvirt ntp rpm-state tuned
alsa cs games lldpad os-prober rsyslog udisks2
alternatives dav gdm logrotate PackageKit samba unbound
authconfig dbus geoclue machines plymouth selinux upower
bluetooth dhclient gssproxy misc polkit-1 setroubleshoot vmware
boltd dnsmasq hyperv mlocate postfix sss xkb
certmonger flatpak initramfs net-snmp pulse stateless yum
chrony fprint ipa-client NetworkManager rpcbind systemd
color fwupd iscsi nfs rpm tpm
[root@localhost lib]# mkdir awstats
[root@localhost lib]# ls
AccountsService color fwupd iscsi nfs rpm tpm
alsa colord fwupdate libvirt ntp rpm-state tuned
alternatives cs games lldpad os-prober rsyslog udisks2
authconfig dav gdm logrotate PackageKit samba unbound
awstats dbus geoclue machines plymouth selinux upower
bluetooth dhclient gssproxy misc polkit-1 setroubleshoot vmware
boltd dnsmasq hyperv mlocate postfix sss xkb
certmonger flatpak initramfs net-snmp pulse stateless yum
chrony fprint ipa-client NetworkManager rpcbind systemd
[root@localhost lib]# systemctl restart httpd '重启apache服务'
进入win10网页搜索:http://www.yyc.com/awstats/awstats.pl?config=www.yyc.com
(复制刚刚的域名,将localhost改为www.yyc.com),此时没有任何访问记录
[root@localhost lib]# cd /usr/local/awstats
[root@localhost awstats]# ls
docs README.md tools wwwroot
[root@localhost awstats]# cd tools/
[root@localhost tools]# ls
awstats_buildstaticpages.pl awstats_updateall.pl httpd_conf nginx xslt
awstats_configure.pl dolibarr logresolvemerge.pl urlaliasbuilder.pl
awstats_exportlib.pl geoip_generator.pl maillogconvert.pl webmin
[root@localhost tools]# ./awstats_updateall.pl now '更新数据'
再刷新网页,就会有访问记录
###此时用户访问网页,记录需要人工输入 ./awstats_updateall.pl now 命令进行更新数据,非常麻烦。
计划性任务更新数据
[root@localhost tools]# crontab -e '不加用户就是当前用户'
*/5 * * * * /usr/local/awstats/tools/awstats_updateall.pl now '每五分钟更新一次'
优化网页地址
[root@localhost tools]# cd /var/www/html/
[root@localhost html]# ls
index.html
[root@localhost html]# vim count.html
<html>
<head>
<meta http-equiv=refresh content="0;url=http://www.yyc.com/awstats/awstats.pl?config=www.yyc.com">
<head>
<body></body>
</html>
'实现自动跳转功能'
打开网页搜索www.yyc.com/count.html ,会自动跳转至http://www.yyc.com/awstats/awstats.pl?config=www.yyc.com页面,方便很多