💡 ไฮไลท์เป็นผู้เขียนอิสระ เรารับค่าสนับสนุนจาก affiliate program บางที่ · เรียนรู้เพิ่มเติม

คู่มือวิเคราะห์ Server Log สำหรับ Hosting และ VPS

Complete Guide to Server Log Analysis for Hosting & VPS Administration

Server Log Analysis Guide

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 หลายประเภท:

วิธีอ่าน 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 นี้คือ:

จากข้อมูลเหล่านี้ คุณสามารถดูได้ว่า 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 ให้คุณดูได้ว่า:

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 เป็นต้น ตัวอย่างบางสิ่งที่ต้องสังเกต:

# ดู 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"
แนะนำAsiaGB.com — Web Hosting & VPS ที่เราใช้และแนะนำ เซิร์ฟเวอร์ในไทยและสิงคโปร์ สตอเรจ SSD จัดการผ่าน DirectAdmin พร้อมทีม Support ภาษาไทย 24 ชั่วโมง uptime 99%

AsiaGB.com — hosting & VPS we use and recommend: TH/SG servers, SSD storage, DirectAdmin, 24h Thai support, 99% uptime.

เยี่ยมชม AsiaGB →

คำถามที่พบบ่อย

ไฟล์ Log ควรเก็บไว้นานแค่ไหน?
ส่วนใหญ่แล้ว Hosting จะเก็บ Log ได้ 7-30 วัน ขึ้นกับ Disk Space และนโยบายของ Provider หากต้องการเก็บนานกว่านั้น ควรขอเพิ่มระยะเวลาจาก Hosting Support หรือติดตั้ง Custom Log Retention
Log File ขยาย .gz คืออะไร?
เป็น Log ที่ถูก Compress ด้วยอัลกอริทึม Gzip หากต้องการดู ต้อง Decompress ก่อนด้วยคำสั่ง gunzip หรือ zcat
จะรู้ได้ไงว่า Hosting มี Hacking Attempt?
ดู Access Log หากเห็น 404 Storm มาจาก IP เดียว หรือ URL ที่มี SQL Injection Pattern ก็ควรสงสัย หรือดู Error Log ถ้ามี Unauthorized Access หรือ Permission Error ก็อาจเป็นการ Hack ทั่วไปเว็บต้องติดตั้ง Security Plugin และ WAF (Web Application Firewall) เพื่อป้องกัน
PHP Error Log ไม่ปรากฎ ต้องทำอย่างไร?
บางที PHP Settings อาจจะปิด Error Logging ไป ต้องแก้ไข php.ini หรือขอ Hosting Support เปิด Error Logging ให้ หรือตั้งค่าผ่าน .htaccess ด้วยบรรทัด php_flag log_errors on และ php_value error_log /path/to/error_log
ดาวน์โหลด Log จาก Hosting ได้ไหม?
ได้ ส่วนใหญ่ Hosting จะให้DownloadผLง File Manager หรือ cPanel/DirectAdmin Dashboard หรือสามารถใช้ SSH/FTP เพื่อดาวน์โหลด Log ไฟล์มาอ่านที่เครื่องของคุณ