Apache vs Nginx บน VPS เลือกแบบไหน?
Apache และ Nginx เป็นสอง web server ที่ครองตลาดมานาน แต่ทำงานต่างกันในระดับสถาปัตยกรรม ความเข้าใจความแตกต่างช่วยเลือกให้ตรงกับงานได้
Apache: Process-based Architecture
Apache ใช้โมเดล process-based หรือ thread-based แต่ละ connection ได้รับ process/thread ของตัวเอง ทำให้ง่ายต่อการ configure per-directory ด้วย .htaccess
ข้อดี
- รองรับ
.htaccess— เปลี่ยน config ได้ระดับ directory โดยไม่ต้อง restart - module ครอบคลุม ทำได้หลายอย่างจาก config เดียว
- WordPress, PHP app ส่วนใหญ่ทดสอบมาบน Apache ก่อน
- เอกสาร tutorial เก่าแก่และมีมาก
ข้อเสีย
- ใช้ RAM มากกว่า Nginx โดยเฉพาะเมื่อมี connection จำนวนมาก
- C10K problem — เมื่อ concurrent connection หลักพันขึ้นไป performance ตก
- การ serve static file ช้ากว่า Nginx
Nginx: Event-driven Architecture
Nginx ใช้ event-driven, asynchronous architecture เซิร์ฟเวอร์เดียวรับหลาย connection พร้อมกันโดยไม่ต้องสร้าง process ใหม่ต่อ connection
ข้อดี
- กิน RAM น้อยกว่าอย่างมีนัยสำคัญเมื่อ load สูง
- Serve static file (CSS, JS, รูป) เร็วกว่า Apache มาก
- เหมาะกับการทำ reverse proxy หน้า backend app
- Config syntax สะอาดและเข้าใจง่าย
ข้อเสีย
- ไม่รองรับ
.htaccessต้อง config ทุกอย่างใน server block - ต้อง reload config เมื่อเปลี่ยนแปลง (ไม่ใช่ restart แต่ต้องทำ)
เปรียบเทียบแบบตาราง
| เกณฑ์ | Apache | Nginx |
|---|---|---|
| Architecture | Process/Thread-based | Event-driven |
| RAM ที่ใช้ | สูงกว่า | ต่ำกว่า |
| Static files | ดี | เร็วกว่ามาก |
| .htaccess | รองรับ | ไม่รองรับ |
| Reverse proxy | ทำได้ | เหมาะกว่า |
| PHP support | mod_php ในตัว | ผ่าน PHP-FPM |
ควรเลือกแบบไหน?
เลือก Apache ถ้า
- ใช้ shared-hosting style app ที่พึ่งพา
.htaccess - ต้องการ module เฉพาะเช่น mod_rewrite ที่ยุ่งยากกับ Nginx
- มีแอปเก่าที่เขียน config สำหรับ Apache โดยเฉพาะ
เลือก Nginx ถ้า
- ต้องการ performance สูงพร้อม RAM น้อย
- ทำ reverse proxy หน้า Node.js, Python, Go หรือแอปอื่น
- มี traffic สูง หรือ concurrent connection จำนวนมาก
- เซิร์ฟ static file จำนวนมาก
สรุป
Nginx เป็นตัวเลือกที่ดีกว่าในแง่ performance สำหรับ VPS RAM น้อย แต่ Apache ยังคงมีข้อได้เปรียบในด้านความยืดหยุ่นสำหรับแอปที่ใช้ .htaccess บางคนก็ใช้ทั้งคู่ โดยให้ Nginx เป็น frontend รับ request แล้วส่งต่อไป Apache ก็ได้
ใช้ทั้งคู่: Nginx + Apache
บางองค์กรใช้ Nginx เป็น frontend reverse proxy รับ traffic จาก internet แล้วส่งต่อให้ Apache ที่รันบน port 8080 ได้ประโยชน์ทั้งสอง — Nginx รับ static file และ SSL termination, Apache รองรับ .htaccess และ legacy app
# Nginx ฟัง port 80/443, ส่งต่อไป Apache port 8080
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# Static files serve ตรงจาก Nginx
location ~* \.(css|js|jpg|png|gif|ico|woff2)$ {
root /var/www/html;
expires 1y;
}
}
แก้ปัญหาที่พบบ่อย
Nginx: "502 Bad Gateway" หลังเปลี่ยน PHP-FPM socket
ชื่อ socket ใน config ไม่ตรงกับที่ PHP-FPM จริงๆ รัน:
# ตรวจว่า PHP-FPM socket อยู่ที่ไหน
ls /run/php/
# อาจเห็น php8.3-fpm.sock หรือ php8.1-fpm.sock
# แก้ใน nginx site config ให้ตรง
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
nginx -t && systemctl reload nginx
Apache: .htaccess ไม่ทำงาน / mod_rewrite ไม่ redirect
ต้องเปิด AllowOverride All ใน VirtualHost config:
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
# เปิด mod_rewrite
a2enmod rewrite
systemctl restart apache2
Nginx: configuration เปลี่ยนแล้วไม่มีผล
# ทดสอบ config ก่อน reload
nginx -t
# ถ้าผ่านถึง reload
systemctl reload nginx
Apache: Memory สูงผิดปกติ
Apache ใช้ prefork MPM ซึ่งสร้าง process มาก ลองเปลี่ยนเป็น event MPM:
a2dismod mpm_prefork
a2enmod mpm_event php8.3-fpm
systemctl restart apache2
คำถามที่พบบ่อย
สลับจาก Apache ไป Nginx กลางทางได้ไหม?
ได้ แต่ต้อง convert config ทั้งหมด — Apache VirtualHost เป็น Nginx server block, .htaccess rewrite rules เป็น Nginx location directives แนะนำ test บน staging ก่อน
VPS RAM 1 GB ควรใช้ตัวไหน?
Nginx ชัดเจน RAM น้อยกว่า Apache อย่างมีนัยสำคัญ สำหรับ WordPress บน VPS RAM น้อย ใช้ Nginx + PHP-FPM ประหยัดกว่า Apache + mod_php ประมาณ 200–400 MB
Nginx ทำ virtual hosting หลาย domain ได้ไหม?
ได้ สร้างหลาย server block แยกกันใน /etc/nginx/sites-available/ แล้ว symlink ไป sites-enabled/ คล้ายกับ Apache VirtualHost
ต้องการ .htaccess แต่อยากได้ performance ของ Nginx ทำได้ไหม?
ไม่ได้โดยตรง Nginx ไม่รองรับ .htaccess ถ้า app พึ่ง .htaccess มากใช้ Apache หรือ Nginx+Apache stack แทน หรือ migrate rules มาใส่ใน Nginx config โดยตรง