คู่มือวิเคราะห์ Server Log สำหรับ Hosting และ VPS
Complete Guide to Server Log Analysis for Hosting & VPS Administration
Server Log เป็นบัญชีบันทึกที่บันทึกกิจกรรมทั้งหมดบนเซิร์ฟเวอร์ของคุณ ตั้งแต่ Request จาก Visitor, Error ต่างๆ, ไปจนถึง Security Threat เมื่อคุณ Host เว็บไซต์บน Hosting หรือ VPS คุณต้องรู้วิธีอ่าน Log เพื่อหา Error, Monitor Performance, ตรวจสอบ Security, และแก้ปัญหาเว็บ วันนี้เราจะสอนวิธีวิเคราะห์ Server Log อย่างละเอียด
สารบัญ
Log File คืออะไร Hosting เก็บ Log อะไรบ้าง
Log File เป็นไฟล์ข้อความที่บันทึกเหตุการณ์ต่างๆ บนเซิร์ฟเวอร์ โดยมี Timestamp เพื่อให้คุณสามารถติดตามเหตุการณ์เหล่านั้นได้อย่างแม่นยำ ทุก Hosting และ VPS ธรรมชาติจะเก็บ Log หลายประเภท:
- Access Log (Apache/Nginx) — บันทึกทุก HTTP Request ที่เข้ามายังเว็บไซต์ของคุณ ไม่ว่าจะเป็น GET, POST, DELETE เป็นต้น
- Error Log (Apache/Nginx) — บันทึก Error ที่เกิดจากเว็บเซิร์ฟเวอร์เอง เช่น Configuration Error, File Not Found (404), Permission Denied เป็นต้น
- PHP Error Log — บันทึก Error, Warning, Notice จากโค้ด PHP ของเว็บไซต์คุณ
- Mail Log — บันทึก Email ที่ส่งผ่านเซิร์ฟเวอร์ของคุณ Bounce Message, SMTP Error เป็นต้น
- FTP Log — บันทึก FTP Connection และ File Transfer Activity
- cPanel/DirectAdmin Log — บันทึก Login Activity และ Configuration Changes
วิธีอ่าน Log จะช่วยคุณ Debug ปัญหาเว็บได้อย่างรวดเร็ว ไม่ต้องลองผิดลองถูกอีกต่อไป เมื่อเว็บพัง Error แรกที่ต้องตรวจคือ PHP Error Log เพราะที่นั่นมักจะมีคำตอบว่าเพราะอะไร
ตำแหน่งไฟล์ Log บน Linux Server และการเข้าถึงผ่าน SSH
หากใช้ cPanel หรือ DirectAdmin โดยปกติ Hosting จะจัดเตรียม File Manager ในกราฟิก ให้คุณโหลด Log ได้ง่าย แต่ถ้าต้องการเข้าถึง Log ผ่าน SSH (Terminal) ก็ต้องรู้ตำแหน่งของไฟล์ต่างๆ:
# Apache Access Log
/var/log/apache2/access.log
/var/log/httpd/access_log
# Apache Error Log
/var/log/apache2/error.log
/var/log/httpd/error_log
# Nginx Access Log
/var/log/nginx/access.log
# Nginx Error Log
/var/log/nginx/error.log
# PHP Error Log (ต่างกันไป ขึ้นกับ Configuration)
/var/log/php-errors.log
/var/log/php.log
/home/username/public_html/error_log
# Mail Log
/var/log/maillog
/var/log/mail.log
หากใช้ DirectAdmin Hosting คุณสามารถเข้า SSH แล้วดู Log โดยใช้ Command cat, tail, less เป็นต้น ตัวอย่าง:
# ดู Log ทั้งหมด
cat /var/log/apache2/access.log
# ดู 20 บรรทัดล่าสุด
tail -20 /var/log/apache2/access.log
# ดู 20 บรรทัดแรก
head -20 /var/log/apache2/access.log
# ดู Log ทีละหน้า (กด space เพื่อเลื่อน)
less /var/log/apache2/access.log
อีกวิธีหนึ่งคือใช้ cPanel File Manager หรือ DirectAdmin File Manager เพื่อ Download ไฟล์ Log มาวิเคราะห์ที่เครื่องของคุณ ซึ่งสะดวกกว่าการแก้ไขผ่าน SSH บ่อยครั้ง
อ่าน Apache/Nginx Access Log ทำความเข้าใจ Combined Log Format
Access Log บันทึกทุก Request ที่เข้ามายังเว็บไซต์ของคุณ ไฟล์เหล่านี้มีรูปแบบ (Format) ที่เป็นมาตรฐาน เรียกว่า "Combined Log Format":
203.0.113.5 - - [29/Jun/2026:10:23:14 +0700] "GET /index.php HTTP/1.1" 200 2326 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
ส่วนต่างๆ ของ Log Line นี้คือ:
- 203.0.113.5 — IP Address ของ Visitor
- - — RFC 1413 Identity (ไม่ใช้งานปกติ)
- - — Authenticated User (ถ้าไม่มี Authentication ก็ใช้ -)
- [29/Jun/2026:10:23:14 +0700] — Timestamp ของ Request
- "GET /index.php HTTP/1.1" — Request Line (Method, Path, HTTP Version)
- 200 — HTTP Status Code (200=Success, 404=Not Found, 500=Error)
- 2326 — ขนาด Response Body (Bytes)
- "-" — Referrer (หน้าที่ Link มาที่เว็บคุณ)
- "Mozilla/5.0..." — User Agent (Browser ที่ใช้)
จากข้อมูลเหล่านี้ คุณสามารถดูได้ว่า Visitor มาจากไหน, ใช้ Browser อะไร, เข้าหน้าไหน, Request สำเร็จหรือไม่ (ดู Status Code) เป็นต้น ถ้าเห็น 404 มาก ก็อาจจะมีบางลิงก์ที่ Error, ถ้าเห็น 500 มากก็อาจมี Server Error อยู่
PHP Error Log การหา Error และ Warning ที่ทำให้เว็บพัง
เมื่อเว็บไซต์ที่เขียนด้วย PHP เกิด Error เช่น Syntax Error, Call Undefined Function, Undefined Variable เป็นต้น ข้อมูลเหล่านั้นจะถูกบันทึกใน PHP Error Log ไฟล์นี้จะหาได้ตามตำแหน่งที่กำหนดใน php.ini File ปกติแล้ว Hosting จะตั้งให้ PHP Error Log อยู่ที่:
/var/log/php-errors.log
/var/log/php.log
/home/username/public_html/error_log
ตัวอย่างของ PHP Error Log Entry:
[29-Jun-2026 10:25:31 Asia/Bangkok] PHP Fatal error: Call to undefined function mysql_connect() in /home/user/public_html/db.php on line 15
[29-Jun-2026 10:26:45 Asia/Bangkok] PHP Warning: Undefined variable: $username in /home/user/public_html/login.php on line 42
[29-Jun-2026 10:27:12 Asia/Bangkok] PHP Parse error: syntax error, unexpected '}' in /home/user/public_html/config.php on line 23
เมื่ออ่าน PHP Error Log จะเห็นบรรทัดที่เกิด Error, ชื่อไฟล์, Type ของ Error (Fatal, Warning, Parse Error เป็นต้น) สิ่งนี้ช่วยให้ Developer แก้ไขไฟล์นั้นได้อย่างรวดเร็ว โดยปกติจะแก้ Syntax Error ตรง Line ที่ระบุให้มี
อีกสิ่งหนึ่ง ถ้าเห็น Warning เยอะๆ แม้ว่าเว็บยังทำงาน แต่ควรแก้ไขด้วยเพราะอาจเกิด Error ที่ใหญ่กว่าได้ในอนาคต
ใช้ Grep และ Awk วิเคราะห์ Log บน Command Line
เมื่อ Log File มีขนาดใหญ่มากๆ (หลักล้าน Lines) การอ่าน Manual ทั้งหมดจะไม่สมจริง ในกรณีนี้ต้องใช้ Shell Command เพื่อช่วยคัดกรอง คำสั่งที่มีประโยชน์ที่สุดคือ grep (Filter), awk (Parse & Process), sort, uniq
# นับจำนวน 404 Error
grep " 404 " /var/log/apache2/access.log | wc -l
# นับจำนวน 500 Error
grep " 500 " /var/log/apache2/access.log | wc -l
# ดู IP ที่ Error มากที่สุด (Top 20)
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -rn | head -20
# ดู URL ที่ 404 มากที่สุด
grep " 404 " /var/log/apache2/access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head -10
# ดู Browser ที่เข้าเว็บคุณ (User Agent)
awk -F'"' '{print $6}' /var/log/apache2/access.log | sort | uniq -c | sort -rn | head -10
# ดู Request ในช่วงวันที่หนึ่งๆ
grep "29/Jun/2026" /var/log/apache2/access.log | wc -l
# ดู Error เฉพาะจาก IP ตัวนึง
grep "203.0.113.5" /var/log/apache2/access.log | grep " 404 "
คำสั่ง awk ช่วยให้คุณหาสิ่งที่ต้องการจาก Log ได้อย่างแม่นยำ โดยการระบุ Field (Column) ที่ต้องการ ตัวอย่างเช่น awk '{print $1}' จะพิมพ์เฉพาะ Field 1 (IP Address) ใน Access Log
AWStats และ GoAccess เครื่องมือ Analyze Log ฟรี
สำหรับ Log Analysis ที่มีขนาดใหญ่ มีเครื่องมือฟรีที่ดีมากสองตัว ได้แก่ AWStats และ GoAccess
AWStats เป็นเครื่องมือ Log Analysis ที่เก่า แต่ยังใช้งานได้ดี ส่วนใหญ่ Hosting ที่มี cPanel จะติดมาด้วย AWStats จะวิเคราะห์ Access Log และสร้างรายงาน HTML ให้คุณดูได้ว่า:
- Unique Visitors per Month/Day
- Page Views per URL
- Browser Distribution
- Operating System Distribution
- Traffic Sources (Referrer)
GoAccess เป็นเครื่องมือที่ใหม่กว่า Real-time ได้มากกว่า AWStats และใช้งาน Command Line ยืดหยุ่นกว่า ถ้าใช้ VPS ของคุณเอง คุณสามารถติดตั้ง GoAccess แล้วดู Real-time Dashboard ได้:
# ติดตั้ง GoAccess
sudo apt-get install goaccess
# วิเคราะห์ Access Log
goaccess /var/log/apache2/access.log -a
# Export เป็น HTML Report
goaccess /var/log/apache2/access.log -a -o html > report.html
GoAccess จะแสดง Dashboard แบบ Real-time ที่แสดง Hits, Visitors, Bandwidth, Top Pages, Top IPs, Browser Distribution เป็นต้น ซึ่งช่วยให้คุณดูสภาพการณ์เว็บได้อย่างชัดเจน
Log File ขนาดใหญ่มาก ทำอย่างไร
ปัญหาทั่วไปของ Log File คือ มันเติบโตอย่างรวดเร็ว Access Log ของเว็บที่มี Traffic สูงจะเติบโตหลายร้อย MB ต่อวัน ไม่งั้น Disk Space จะเต็มไปวัน ดังนั้นจึงต้องมีวิธีจัดการ Log:
1. Logrotate — เป็นระบบ Automatic Log Rotation บน Linux โดยปกติแล้ว Logrotate จะทำงานอัตโนมัติ (ตั้งค่าผ่าน cron) เพื่อเก็บเฉพาะ Log ใหม่ๆ และเก็บ Log เก่าไว้เป็น Archive ตัวอย่างการตั้งค่า Logrotate:
/var/log/apache2/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
if [ -f "var/run/apache2.pid" ]; then
/etc/init.d/apache2 reload > /dev/null
fi
endscript
}
2. Truncate Log — ถ้า Log เต็มจริงๆ คุณสามารถลบเนื้อหา Log ได้เพื่อเพิ่ม Space:
# Truncate Apache Access Log
> /var/log/apache2/access.log
# Truncate PHP Error Log
> /var/log/php-errors.log
3. ลบ Old Log — ลบ Log ที่เก่ากว่าจำนวนวันที่ต้องการ:
# ลบ Log ที่เก่ากว่า 30 วัน
find /var/log -name "*.log" -mtime +30 -delete
วิธีสุดท้ายคือ ติดต่อ Hosting Support เพื่อขอให้เขาจัดการ Log สำหรับเราแทน หลายๆ Hosting จะทำอัตโนมัติ แต่บางแห่งก็ต้องขอ
ตรวจสอบ Hacking Attempt จาก Access Log
นอกจากการหา Error ปกติแล้ว ยังสามารถใช้ Access Log เพื่อตรวจสอบ Security Threat ได้ เช่น Brute Force Attack, SQL Injection Attempt, Path Traversal Attack เป็นต้น ตัวอย่างบางสิ่งที่ต้องสังเกต:
- 404 Storm — ถ้า IP เดียว Request 404 จำนวนมาก (เช่น 100+ ต่อนาที) อาจจะเป็นการ Scan Vulnerability
- SQL Injection Pattern — ดู URL ที่มี
' OR '1'='1,UNION SELECTเป็นต้น - Path Traversal — ดู URL ที่มี
../หรือ..\ - Admin Login Attempt — ถ้าเห็น Request ไปยัง /wp-admin, /admin, /administrator มาก ก็อาจเป็น Brute Force
# ดู IP ที่ Request มากที่สุด (อาจเป็นบอท)
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -rn | head -10
# ดู Request ที่มี "404" มากที่สุด (Vulnerability Scan)
grep " 404 " /var/log/apache2/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10
# ดู Request ที่มี SQL Injection Pattern
grep -E "union|select|where|and|or|drop|insert|update|delete" /var/log/apache2/access.log
# ดู Request ที่มี Path Traversal
grep "\.\.\/" /var/log/apache2/access.log
ถ้าเจอ IP ที่ต้องสงสัย คุณสามารถ Block IP นั้นผ่าน Firewall (.htaccess, iptables, WAF) เพื่อป้องกัน Attack ต่อไป
Real-time Log Monitoring ด้วย Tail -f
หากต้องการสังเกตการณ์ Log เกิดขึ้นแบบ Real-time (Live) คุณสามารถใช้ Command tail -f ได้:
# ดู Access Log แบบ Real-time
tail -f /var/log/apache2/access.log
# ดู PHP Error Log แบบ Real-time
tail -f /var/log/php-errors.log
# ดู Error Log แบบ Real-time
tail -f /var/log/apache2/error.log
Command นี้จะแสดง Log ใหม่ที่เกิดขึ้นทันที ช่วยให้คุณ Debug ปัญหาแบบเรียลไทม์ได้ เมื่อต้องการหยุด ให้กด Ctrl+C
ถ้ารวม tail -f กับ grep ก็สามารถ Monitor เฉพาะสิ่งที่สนใจได้:
# Monitor เฉพาะ Error (Status 404 และ 500)
tail -f /var/log/apache2/access.log | grep -E " (404|500) "
# Monitor เฉพาะ Request จาก IP ตัวหนึ่ง
tail -f /var/log/apache2/access.log | grep "203.0.113.5"
คำถามที่พบบ่อย
gunzip หรือ zcatphp_flag log_errors on และ php_value error_log /path/to/error_log