CentOS 7.6 Apache的权限控制与日志分割

一、权限控制

在apache应用过程中,管理员经常需要对apache下的目录做一些限制,不希望所有用户都能访问该目录下的文件,只对指定用户访问,此时我们就要用到apache的权限功能。

1、基于ip地址的控制访问

第一步:修改虚拟主机的配置文件

[root@sheng yjs]# vim /etc/httpd/conf/extra/vhosts.conf 

<VirtualHost 192.168.245.140:80>
DocumentRoot "/var/www/html/cloud"
ServerName www.cloud.com
ErrorLog "logs/www.cloud.com.error_log"
CustomLog "logs/www.cloud.com.access_log" common
<Directory "/var/www/html">
Require not ip 192.168.245.10   <----指定不允许访问的ip地址
        Require all granted
</Directory>
</VirtualHost>

<VirtualHost 192.168.245.135:80>
DocumentRoot "/var/www/html/yjs"
ServerName www.yjs.com
ErrorLog "logs/www.yjs.com.error_log"
CustomLog "logs/www.yjs.com.access_log" common
<Directory "/var/www/html">
        Require all granted
</Directory>
</VirtualHost>

第二步:重启httpd服务

[root@sheng ~]# systemctl restart httpd

第三步:客户机验证访问
在这里插入图片描述
在这里插入图片描述

2、基于身份验证的访问

第一步:创建一个httpd服务的测试用户test01和test02

[root@sheng ~]# htpasswd -c /etc/httpd/conf/pwd test01
New password: 
Re-type new password: 
Adding password for user test01

[root@sheng ~]# htpasswd /etc/httpd/conf/pwd test02
New password: 
Re-type new password: 
Adding password for user test02

[root@sheng ~]# cat /etc/httpd/conf/pwd
test01:$apr1$e/FqG0wW$uVZiNljK46dqaOebKO2z5/
test02:$apr1$qFO0vi4T$EAezZIF2guS84dzSnLIoO1
htpasswd命令:用于为指定用户生成基于网页用户身份认证的密码,由httpd-tools软件包提供。支持3种加密算法:MD5、SHA和系统上的crypt()函数,不指定算法时,默认为md5。

-c:创建passwdfile,如果已有用户了要继续添加用户必须要去掉-c选项,否则会覆盖原有用户!
-b:直接输入用户名和密码,不需要交互
-m 使用MD5加密(默认)
-d 使用CRYPT加密(默认)
-s 使用SHA加密
-D 删除指定的用户

如果要修改用户密码,需要先删除用户再添加

第二步:修改虚拟主机的配置文件(配置在httpd.conf主配置文件也可以,这里为了演示)

[root@sheng ~]# vim /etc/httpd/conf/extra/vhosts.conf
<VirtualHost 192.168.245.135:80>
DocumentRoot "/var/www/html/yjs"
ServerName www.yjs.com
ErrorLog "logs/www.yjs.com.error_log"
CustomLog "logs/www.yjs.com.access_log" common
<Directory "/var/www/html">
        AuthName "DocumentRoot"   <---指定认证的网页目录
        AuthType basic    <---指定认证的类型是基本验证
        AuthUserFile /etc/httpd/conf/pwd   <---指定认证的用户密码文件
        Require valid-user   <---指定认证的用户
</Directory>
</VirtualHost>

第三步:重启httpd服务

[root@sheng ~]# systemctl restart httpd

第四步:客户机验证访问
在这里插入图片描述
在这里插入图片描述

设置超时时间

二、日志分割

1、rotatelogs工具

rotatelogs是apache自带的日志轮询工具,安装完apache服务就有这个命令了

语法
rotatelogs [ -l ] logfile [ rotationtime [ offset ]] | [ filesizeM ]

选项
-l
使用本地时间代替GMT时间作为时间基准。

logfile
它加上基准名就是日志文件名。如果logfile中包含”%”,则它会被视为用于strftime()的格式字符串;否则它会被自动加上以秒为单位的”.nnnnnnnnnn”后缀。这两种格式都表示新的日志开始使用的时间。

rotationtime
日志文件滚动的以秒为单位的间隔时间。

offset
相对于UTC的时差的分钟数。如果省略,则假定为”0″并使用UTC时间。如果要使用北京时间,需要设定为480,因为北京时间=UTC+8小时,480秒

filesizeM
指定以filesizeM文件大小滚动,而不是按照时间或时差滚动。

(1)配置错误日志按天分割

第一步:查看rotatelogs的所在位置

[root@sheng httpd]# which rotatelogs 
/usr/sbin/rotatelogs

第二步:清空其他日志文件只保留最基础的访问日志和错误日志

[root@sheng ~]# cd /var/log/httpd/
[root@sheng httpd]# ls
access_log  error_log

第三步:修改主配置文件,设置按天分割日志,格式%Y%m%d=年月日.log

[root@sheng httpd]# vim /etc/httpd/conf/httpd.conf 
#ErrorLog "logs/error_log"   <---将默认设置注释
ErrorLog "| /usr/sbin/rotatelogs -l logs/www.yjs.com.error_%Y%m%d.log 86400"

第四步:重启服务之后立即可以看到产生的错误日志

[root@sheng httpd]# systemctl restart httpd
[root@sheng httpd]# ll
总用量 12
-rw-r--r-- 1 root root    0 8月   4 16:22 access_log
-rw-r--r-- 1 root root 7169 8月   5 15:00 error_log
-rw-r--r-- 1 root root  671 8月   6 09:45 www.yjs.com.error_20200806.log
(2)配置访问日志按2小时分割

第一步:查看当前系统时间

[root@sheng httpd]# date
2020年 08月 06日 星期四 10:12:12 CST

第二步:修改主配置文件,搜索到CustomLog,将默认设置注释,下面添加一行
%Y%m%d%H=年月日小时

[root@sheng httpd]# vim /etc/httpd/conf/httpd.conf
CustomLog "| /usr/sbin/rotatelogs logs/www.kgc.com.access_%Y%m%d%H.log 7200" combined

[root@sheng httpd]# ls
access_log  error_log  www.kgc.com.access_2020080602.log

注意:如果没有-l,他会默认以UTC时间作为标准,所以显示是早上2点

CustomLog "| /usr/sbin/rotatelogs logs/www.kgc.com.access_%Y%m%d%H.log 7200 480" combined

[root@sheng httpd]# ls
access_log  www.kgc.com.access_2020080602.log  www.yjs.com.error_2020080600.log
error_log   www.kgc.com.access_2020080610.log

注意:如果在7200后面加上480,再进行访问,这个时候日志的时间就显示的是北京时间了

那么再来加上-l选项尝试一下,不要加上480就可以了

CustomLog "| /usr/sbin/rotatelogs -l logs/www.kgc.com.access_%Y%m%d%H.log 7200" combined

[root@sheng httpd]# ls
access_log  error_log  www.kgc.com.access_2020080610.log
CustomLog "| /usr/sbin/rotatelogs -l logs/www.kgc.com.access_%Y%m%d%H.log 7200 480" combined  <---这种方式虽然服务可以启动但是并不会产生访问日志

所以,要使用北京时间来切割日志有2种方式:

CustomLog "| /usr/sbin/rotatelogs -l logs/www.kgc.com.access_%Y%m%d%H.log 7200" combined

CustomLog "| /usr/sbin/rotatelogs logs/www.kgc.com.access_%Y%m%d%H.log 7200 480" combined

2、cronolog工具(第三方工具,推荐使用)

cronolog作为日志过滤程序,可用来切割linux日志文件,通过对输入的日志按文件名模板和当前日期重新编排,来按格式生成所需日志。cronolog 旨在和一个Web服务器一起使用,如Apache、Nginx

第一步:rpm安装一下cronolog软件(yum也可以安装)

[root@sheng opt]# rpm -ivh cronolog-1.6.2-14.el7.x86_64.rpm 
[root@sheng opt]# which cronolog 
/usr/sbin/cronolog

第二步:修改httpd主配置文件,按小时分割错误日志

[root@sheng httpd]# vim /etc/httpd/conf/httpd.conf
ErrorLog "| /usr/sbin/cronolog logs/www.yjs.com.error_%Y%m%d%H.log"

第三步:重启服务,验证错误日志已产生

[root@sheng httpd]# systemctl restart httpd
[root@sheng httpd]# ls
access_log  error_log  www.yjs.com.error_2020080610.log
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值