web server performance

Apache vs Nginx บน VPS เลือกแบบไหน?

มิถุนายน 2026 · อ่าน 5 นาที

Apache และ Nginx เป็นสอง web server ที่ครองตลาดมานาน แต่ทำงานต่างกันในระดับสถาปัตยกรรม ความเข้าใจความแตกต่างช่วยเลือกให้ตรงกับงานได้


Apache: Process-based Architecture

Apache ใช้โมเดล process-based หรือ thread-based แต่ละ connection ได้รับ process/thread ของตัวเอง ทำให้ง่ายต่อการ configure per-directory ด้วย .htaccess

ข้อดี

ข้อเสีย

Nginx: Event-driven Architecture

Nginx ใช้ event-driven, asynchronous architecture เซิร์ฟเวอร์เดียวรับหลาย connection พร้อมกันโดยไม่ต้องสร้าง process ใหม่ต่อ connection

ข้อดี

ข้อเสีย


เปรียบเทียบแบบตาราง

เกณฑ์ApacheNginx
ArchitectureProcess/Thread-basedEvent-driven
RAM ที่ใช้สูงกว่าต่ำกว่า
Static filesดีเร็วกว่ามาก
.htaccessรองรับไม่รองรับ
Reverse proxyทำได้เหมาะกว่า
PHP supportmod_php ในตัวผ่าน PHP-FPM

ควรเลือกแบบไหน?

เลือก Apache ถ้า

เลือก Nginx ถ้า


สรุป

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 โดยตรง