標籤:apache
生產環境可能你會遇到web伺服器產生的log記錄檔佔滿磁碟的現象,下面給出具體的解決方案
安裝httpd web服務
[[email protected] ~]# yum install httpd -y
啟動
[[email protected] ~]# /etc/init.d/httpd start
測試
[[email protected] ~]# 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/目錄
[[email protected] html]#cd /var/www/html
設定訪問頁面
[[email protected] html]# echo "hello web" >/var/www/html/index.html
訪問測試
[[email protected] 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
建立記錄檔
[[email protected] html]# mkdir -p /app/logs
定義日誌到指定目錄
[[email protected] html]# sed -i "[email protected]#CustomLog logs/access_log [email protected] /app/logs/access_log [email protected]" /etc/httpd/conf/httpd.conf
[[email protected] html]# grep "access_log common" /etc/httpd/conf/httpd.conf
CustomLog /app/logs/access_log common
重啟使日誌生效
[[email protected] html]# /etc/init.d/httpd restart
建立一個小的檔案系統,用於存放上述 access_log 日誌
[[email protected] 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
格式和掛載
[[email protected] html]# mkfs -t ext4 /dev/sdb
[[email protected] html]# mount -o loop /dev/sdb /app/logs
[[email protected] html]#/etc/init.d/httpd restart
[[email protected] html]# curl -s 127.0.0.1
hello world!!!
[[email protected] 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
[[email protected] ~]# 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
錯誤的刪除方案
[[email protected] ~]# rm -f /app/logs/access_log
[[email protected] ~]# 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 服務
[[email protected] html]# /etc/init.d/httpd restart
查看處理結果
[[email protected] 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
本文出自 “比爾營運筆記” 部落格,請務必保留此出處http://chenshoubiao.blog.51cto.com/6159058/1845310
類比web伺服器產生的日誌佔滿磁碟故障解決方案