生产环境可能你会遇到web服务器产生的log日志文件占满磁盘的现象,下面给出具体的解决方案
安装httpd web服务
[root@bier ~]# yum install httpd -y
启动
[root@bier ~]# /etc/init.d/httpd start
测试
[root@bier ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1542 root 4u IPv6 12105 0t0 TCP *:http (LISTEN)
httpd 1544 apache 4u IPv6 12105 0t0 TCP *:http (LISTEN)
httpd 1545 apache 4u IPv6 12105 0t0 TCP *:http (LISTEN)
httpd 1546 apache 4u IPv6 12105 0t0 TCP *:http (LISTEN)
安装目录介绍
Apache默认将网站的根目录指向/var/www/html 目录
默认的主配置文件是/etc/httpd/conf/httpd.conf
配置存储在的/etc/httpd/conf.d/目录
[root@bier html]#cd /var/www/html
设置访问页面
[root@bier html]# echo "hello web" >/var/www/html/index.html
访问测试
[root@bier html]# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Date: Wed, 31 Aug 2016 19:31:54 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Wed, 31 Aug 2016 19:30:35 GMT
创建日志文件
[root@bier html]# mkdir -p /app/logs
定义日志到指定目录
[root@bier html]# sed -i "s@#CustomLog logs/access_log common@CustomLog /app/logs/access_log common@g" /etc/httpd/conf/httpd.conf
[root@bier html]# grep "access_log common" /etc/httpd/conf/httpd.conf
CustomLog /app/logs/access_log common
重启使日志生效
[root@bier html]# /etc/init.d/httpd restart
创建一个小的文件系统,用于存放上述 access_log 日志
[root@bier html]# dd if=/dev/zero of=/dev/sdb bs=10k count=100
100+0 records in
100+0 records out
1024000 bytes (1.0 MB) copied, 0.00173305 s, 591 MB/s
格式和挂载
[root@bier html]# mkfs -t ext4 /dev/sdb
[root@bier html]# mount -o loop /dev/sdb /app/logs
[root@bier html]#/etc/init.d/httpd restart
[root@bier html]# curl -s 127.0.0.1
hello world!!!
[root@bier html]# cat /app/logs/access_log
127.0.0.1 - - [01/Sep/2016:04:02:23 +0800] "GET / HTTP/1.1" 200 15
写个循环脚本访问 httpd,使得 httpd 日志充满/app/logs 整个空间
for n in `seq 1000000`;do curl -s 127.0.0.1 >/dev/null;done
[root@bier ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.9G 15G 12% /
tmpfs 429M 0 429M 0% /dev/shm
/dev/sda1 194M 29M 155M 16% /boot
/dev/sda2 2.0G 67M 1.9G 4% /swap
/dev/sdb 979K 909K 20K 98% /app/logs
错误的删除方案
[root@bier ~]# rm -f /app/logs/access_log
[root@bier ~]# lsof|grep del #===>还有进程在调用
httpd 3306 root 7w REG 7,0 912339 12 /app/logs/access_log (deleted)
httpd 35409 apache 7w REG 7,0 912339 12 /app/logs/access_log (deleted)
httpd 35828 apache 7w REG 7,0 912339 12 /app/logs/access_log (deleted)
httpd 35829 apache 7w REG 7,0 912339 12 /app/logs/access_log (deleted)
解决方法
1、 请先停掉模拟访问测试脚本
for n in `seq 1000000`;do curl -s 127.0.0.1>/dev/null;done
2、重启 Http 服务
[root@bier html]# /etc/init.d/httpd restart
查看处理结果
[root@bier html]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.9G 15G 12% /
tmpfs 429M 0 429M 0% /dev/shm
/dev/sda1 194M 29M 155M 16% /boot
/dev/sda2 2.0G 67M 1.9G 4% /swap
/dev/sdb 979K 17K 912K 2% /app/logs
理想的处理方案
清空日志而不删除日志
>/app/logs/access_log
转载于:https://blog.51cto.com/chenshoubiao/1845310