จากคนที่ใช้ Apache มา 15 ปี แล้วเปลี่ยนมา Nginx
ผมเริ่มใช้ Apache ตั้งแต่ปี 2008 ตอนนั้น Apache คือ web server ตัวเดียวที่รู้จัก ใช้มาตลอด 10 กว่าปี ไม่เคยคิดจะเปลี่ยน จนวันหนึ่งปี 2019 ลูกค้ามีเว็บ WordPress ที่มี traffic 50,000 visits/วัน Apache กิน RAM 4 GB และ CPU 80% ตลอดเวลา ทั้งที่ server มี RAM 8 GB
ผมลองเปลี่ยนมา Nginx + PHP-FPM ผลลัพธ์ทำให้ตกใจ — RAM ลดเหลือ 1.2 GB, CPU เหลือ 15% response time จาก 2.5 วินาทีเหลือ 0.3 วินาที ทั้งหมดนี้โดยไม่ได้เปลี่ยน hardware เลยแม้แต่ชิ้นเดียว
ตั้งแต่วันนั้น ผมใช้ Nginx เป็นหลักสำหรับทุกโปรเจคใหม่ แต่ก็ยังใช้ Apache อยู่สำหรับบางกรณี บทความนี้จะเปรียบเทียบทั้งสองตัวอย่างตรงไปตรงมา ไม่มี bias
เปรียบเทียบ Nginx vs Apache แบบละเอียด
| Feature | Nginx | Apache |
|---|---|---|
| Architecture | Event-driven, async | Process/Thread-based |
| RAM Usage | ต่ำมาก (10K connections = ~2.5 MB) | สูง (10K connections = ~300 MB+) |
| Static Files | เร็วมาก (2-3x Apache) | ช้ากว่า |
| Dynamic Content | ต้องใช้ PHP-FPM แยก | mod_php built-in (ง่ายกว่า) |
| .htaccess | ไม่รองรับ | รองรับ (ยืดหยุ่นมาก) |
| Config Syntax | C-like blocks | XML-like directives |
| Reverse Proxy | ดีเยี่ยม (ออกแบบมาเพื่อสิ่งนี้) | ได้ แต่ซับซ้อนกว่า |
| Load Balancing | Built-in ฟรี | ต้องใช้ mod_proxy_balancer |
| Market Share 2026 | ~34% (อันดับ 1) | ~29% (อันดับ 2) |
| ใช้โดย | Netflix, Cloudflare, GitHub | WordPress.com, Apple |
| Learning Curve | ปานกลาง | ต่ำ (มี .htaccess) |
| Module System | ต้อง compile ใหม่ | โหลด module แบบ dynamic |
Architecture: ทำไม Nginx ถึงเร็วกว่า
Apache: Process/Thread Model
Apache สร้าง process หรือ thread ใหม่สำหรับทุก connection ถ้ามี 1,000 connections พร้อมกัน = 1,000 processes/threads แต่ละตัวกิน RAM 5-10 MB ดังนั้น 1,000 connections = 5-10 GB RAM
Apache มี 3 MPM (Multi-Processing Module):
- prefork — 1 process ต่อ 1 connection (เก่าสุด กิน RAM เยอะสุด แต่เสถียร)
- worker — ใช้ threads แทน process (กิน RAM น้อยกว่า)
- event — เหมือน worker แต่จัดการ keep-alive ดีกว่า (แนะนำถ้าใช้ Apache)
Nginx: Event-Driven Model
Nginx ใช้ event loop เหมือน Node.js — worker process เดียวจัดการได้หลายพัน connections พร้อมกัน โดยไม่ต้องสร้าง process/thread ใหม่ ทำให้กิน RAM น้อยมากและ scale ได้ดีกว่ามาก
# Nginx: 1 master + worker processes ตามจำนวน CPU core
# 4 core CPU = 4 worker processes
# แต่ละ worker จัดการได้ 10,000+ connections
# รวม = 40,000+ connections ด้วย RAM แค่ ~50 MB
# Apache prefork: 1,000 connections = 1,000 processes
# RAM: 1,000 x 10 MB = 10 GB
# ถ้ามี 10,000 connections = ต้องมี RAM 100 GB (เป็นไปไม่ได้)
ติดตั้งและ Config: Nginx
# ติดตั้ง Nginx
sudo apt update
sudo apt install nginx -y
# เช็คสถานะ
sudo systemctl status nginx
# Config หลักอยู่ที่
# /etc/nginx/nginx.conf — config หลัก
# /etc/nginx/sites-available/ — site configs
# /etc/nginx/sites-enabled/ — symlinks ไป available
# ตัวอย่าง config สำหรับ static site
sudo nano /etc/nginx/sites-available/mysite
# /etc/nginx/sites-available/mysite
server {
listen 80;
server_name example.com www.example.com;
root /var/www/mysite;
index index.html;
# Gzip compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;
gzip_min_length 1000;
# Static files caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
location / {
try_files $uri $uri/ =404;
}
}
# Enable site
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
sudo nginx -t # ทดสอบ config
sudo systemctl reload nginx
Nginx + PHP-FPM (สำหรับ WordPress, Laravel)
# ติดตั้ง PHP-FPM
sudo apt install php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring -y
# Config Nginx สำหรับ PHP
server {
listen 80;
server_name example.com;
root /var/www/wordpress;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
# Upload limit
client_max_body_size 64M;
}
ติดตั้งและ Config: Apache
# ติดตั้ง Apache
sudo apt update
sudo apt install apache2 -y
# เช็คสถานะ
sudo systemctl status apache2
# Config หลักอยู่ที่
# /etc/apache2/apache2.conf — config หลัก
# /etc/apache2/sites-available/ — site configs
# /etc/apache2/sites-enabled/ — symlinks
# เปิด modules ที่จำเป็น
sudo a2enmod rewrite ssl headers expires proxy proxy_http
# /etc/apache2/sites-available/mysite.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/mysite
<Directory /var/www/mysite>
AllowOverride All
Require all granted
</Directory>
# Gzip compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript
</IfModule>
# Security headers
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
CustomLog ${APACHE_LOG_DIR}/mysite_access.log combined
</VirtualHost>
# Enable site
sudo a2ensite mysite.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
Nginx เป็น Reverse Proxy หน้า Apache
วิธีที่ดีที่สุดคือใช้ทั้งสองตัวร่วมกัน — Nginx รับ traffic ข้างหน้า serve static files และ SSL termination แล้ว proxy dynamic requests ไปให้ Apache ข้างหลัง ได้ข้อดีของทั้งสองตัว
# Nginx reverse proxy config
server {
listen 80;
server_name example.com;
# Static files — Nginx serve เอง (เร็วมาก)
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2|svg)$ {
root /var/www/mysite;
expires 30d;
access_log off;
}
# Dynamic requests — ส่งไป Apache
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# Apache ฟังที่ port 8080 (ไม่เปิดรับจากภายนอก)
# แก้ /etc/apache2/ports.conf
# Listen 8080
ผมใช้ setup นี้กับลูกค้าที่มี WordPress + .htaccess rules เยอะ ไม่ต้องแปลง .htaccess เป็น Nginx config ให้ยุ่งยาก แค่ให้ Apache จัดการ PHP + .htaccess ส่วน Nginx จัดการ static files + SSL + caching
Performance Benchmark จริง
ผมทดสอบกับ VPS 2 vCPU, 4 GB RAM, SSD รัน WordPress + WooCommerce ใช้ wrk benchmark tool
| Metric | Nginx + PHP-FPM | Apache + mod_php | Nginx → Apache |
|---|---|---|---|
| Requests/sec (static) | 15,200 | 5,800 | 14,500 |
| Requests/sec (PHP) | 850 | 720 | 810 |
| RAM Usage (idle) | 180 MB | 450 MB | 520 MB |
| RAM Usage (1K conn) | 350 MB | 1.8 GB | 1.2 GB |
| Time to First Byte | 45 ms | 120 ms | 55 ms |
| Max Concurrent | 10,000+ | ~2,000 | ~5,000 |
Nginx ชนะ static files แบบขาดลอย 2-3 เท่า สำหรับ dynamic content (PHP) ต่างกันไม่มาก เพราะ bottleneck อยู่ที่ PHP ไม่ใช่ web server แต่ RAM usage ต่างกันมาก Nginx ใช้ RAM น้อยกว่า Apache 3-5 เท่า
เมื่อไหร่ควรใช้ Nginx เมื่อไหร่ควรใช้ Apache
ใช้ Nginx เมื่อ:
- ต้องการ performance สูง traffic เยอะ
- ทำ reverse proxy หรือ load balancer
- Serve static files เป็นหลัก (CDN, file server)
- ต้องการประหยัด RAM (VPS เล็ก)
- ใช้กับ microservices, Docker, Kubernetes
- ทำ API gateway
ใช้ Apache เมื่อ:
- ต้องใช้ .htaccess (shared hosting, WordPress plugins ที่แก้ .htaccess)
- ต้องการ dynamic module loading
- ทีมคุ้นเคยกับ Apache อยู่แล้ว
- ใช้ mod_rewrite rules ที่ซับซ้อน
- Hosting provider ให้มาเป็น Apache
ใช้ทั้งสองตัว (Nginx + Apache) เมื่อ:
- มี WordPress + .htaccess rules เยอะ แต่ต้องการ performance
- ต้องการ Nginx เป็น SSL termination + caching หน้า Apache
- ย้ายจาก Apache มา Nginx ทีละส่วน
SSL/TLS Setup สำหรับทั้งสองตัว
# ติดตั้ง Certbot (Let's Encrypt)
sudo apt install certbot -y
# สำหรับ Nginx
sudo apt install python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
# สำหรับ Apache
sudo apt install python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com
# Auto-renew (Certbot ตั้งให้อัตโนมัติแล้ว)
sudo certbot renew --dry-run
FAQ คำถามที่พบบ่อย
Q: WordPress ควรใช้ Nginx หรือ Apache?
ถ้า traffic น้อยกว่า 10,000 visits/วัน ใช้อะไรก็ได้ ไม่ต่างกันมาก ถ้ามากกว่านั้น แนะนำ Nginx + PHP-FPM หรือ Nginx reverse proxy หน้า Apache ผมใช้ Nginx + PHP-FPM กับ WordPress ทุกเว็บที่ดูแล ไม่เคยมีปัญหา performance
Q: Shared hosting ใช้ Nginx ได้ไหม?
ส่วนใหญ่ shared hosting ใช้ Apache + .htaccess ถ้าต้องการ Nginx ต้องใช้ VPS หรือ dedicated server ราคา VPS ตอนนี้ถูกมาก เริ่มต้นแค่ 200-300 บาท/เดือน คุ้มกว่า shared hosting มาก
Q: LiteSpeed ดีกว่า Nginx ไหม?
LiteSpeed เร็วกว่า Apache มาก และเทียบเท่า Nginx ในหลายกรณี ข้อดีคือรองรับ .htaccess เหมือน Apache ข้อเสียคือ version ฟรี (OpenLiteSpeed) มีข้อจำกัด version เต็มต้องจ่ายเงิน ถ้าต้องการ .htaccess + performance LiteSpeed เป็นทางเลือกที่ดี
Q: Caddy ล่ะ ดีไหม?
Caddy เป็น web server รุ่นใหม่ที่ auto HTTPS ให้เลย config ง่ายมาก เหมาะกับโปรเจคเล็ก แต่ ecosystem ยังไม่ใหญ่เท่า Nginx/Apache ผมใช้ Caddy กับ side project ส่วนตัว แต่ production ยังใช้ Nginx
Q: เปลี่ยนจาก Apache มา Nginx ยากไหม?
ไม่ยากครับ สิ่งที่ต้องทำคือ: แปลง .htaccess เป็น Nginx config (ใช้ tool ช่วยได้), เปลี่ยนจาก mod_php เป็น PHP-FPM, แก้ VirtualHost เป็น server block ใช้เวลาประมาณ 1-2 ชั่วโมงสำหรับเว็บทั่วไป ผมมี checklist ที่ใช้ทุกครั้งที่ย้าย ไม่เคยมีปัญหา
Nginx Performance Tuning
Config เหล่านี้ช่วยเพิ่ม performance ของ Nginx ได้อย่างมาก ผมใช้กับ production server ทุกเครื่อง
# /etc/nginx/nginx.conf — Performance tuning
# Worker processes = จำนวน CPU cores
worker_processes auto;
# จำนวน connections ต่อ worker
events {
worker_connections 4096;
multi_accept on;
use epoll;
}
http {
# Buffer sizes
client_body_buffer_size 16K;
client_header_buffer_size 1k;
client_max_body_size 64m;
large_client_header_buffers 4 8k;
# Timeouts
client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;
# Gzip compression
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json
application/javascript application/xml+rss
application/atom+xml image/svg+xml;
gzip_min_length 1000;
# File cache
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# TCP optimizations
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# Security
server_tokens off;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
}
Apache Performance Tuning
# /etc/apache2/mods-available/mpm_event.conf
# ใช้ event MPM (ดีที่สุดสำหรับ Apache)
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>
# เปิด event MPM
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2
ถ้ายังใช้ Apache อยู่ อย่างน้อยเปลี่ยนจาก prefork MPM มาเป็น event MPM จะได้ performance ดีขึ้น 2-3 เท่าทันที โดยเฉพาะเว็บที่มี concurrent connections เยอะ
Checklist: เลือก Web Server ให้เหมาะกับงาน
| Use Case | แนะนำ | เหตุผล |
|---|---|---|
| WordPress blog เล็กๆ | Apache หรือ Nginx | อะไรก็ได้ traffic น้อย |
| WordPress traffic สูง | Nginx + PHP-FPM | ประหยัด RAM กิน resource น้อย |
| Shared hosting | Apache | ต้องใช้ .htaccess |
| Reverse proxy / Load balancer | Nginx | ออกแบบมาเพื่อสิ่งนี้ |
| API Gateway | Nginx | Performance สูง config ง่าย |
| Static file server / CDN | Nginx | เร็วกว่า 2-3 เท่า |
| Legacy app ที่ใช้ .htaccess | Nginx → Apache | ได้ทั้ง performance + compatibility |
| Docker / Kubernetes | Nginx | Image เล็ก boot เร็ว |
| Node.js / Python app | Nginx (reverse proxy) | SSL termination + static files |
สรุป: ในปี 2026 Nginx เป็นตัวเลือกที่ดีกว่าสำหรับ use case ส่วนใหญ่ แต่ Apache ก็ยังมีที่ยืนสำหรับงานที่ต้องการ .htaccess หรือ dynamic module loading ทางที่ดีที่สุดคือเรียนรู้ทั้งสองตัว แล้วเลือกใช้ให้เหมาะกับงาน ไม่มีตัวไหนที่ดีที่สุดสำหรับทุกกรณี
Migration Checklist: ย้ายจาก Apache มา Nginx
ถ้าตัดสินใจย้ายจาก Apache มา Nginx ผมมี checklist ที่ใช้ทุกครั้ง ทำตามนี้แล้วจะไม่มีปัญหา ใช้เวลาประมาณ 1-2 ชั่วโมงสำหรับเว็บทั่วไป
- ติดตั้ง Nginx + PHP-FPM — apt install nginx php8.3-fpm แล้วทดสอบว่า PHP ทำงานได้
- แปลง VirtualHost เป็น server block — ใช้เครื่องมือออนไลน์ช่วยแปลง Apache config เป็น Nginx config ได้
- แปลง .htaccess เป็น Nginx config — rewrite rules, redirect, access control ต้องแปลงทั้งหมด
- ทดสอบบน port อื่นก่อน — ให้ Nginx ฟังที่ port 8080 ทดสอบให้เรียบร้อยก่อนเปลี่ยน
- ตั้ง SSL ใหม่ — ใช้ certbot --nginx ติดตั้ง Let's Encrypt certificate
- เปลี่ยน port — หยุด Apache แล้วให้ Nginx ฟังที่ port 80 และ 443
- ทดสอบทุก URL — เช็คว่า redirect ทำงานถูกต้อง ไม่มี 404 หรือ 500 error
- Monitor 24 ชั่วโมง — ดู error log ว่ามีปัญหาอะไรไหม ถ้ามีก็แก้ทันที
ผมย้ายเว็บจาก Apache มา Nginx มาแล้วกว่า 50 เว็บ ไม่เคยมีปัญหาใหญ่ ถ้าทำตาม checklist นี้ ส่วนใหญ่ปัญหาที่เจอคือ rewrite rules ที่แปลงไม่ครบ ซึ่งแก้ได้ง่ายจาก error log ของ Nginx
🎬 ดูคลิปเพิ่มเติม: @icafefx
📚 บทความแนะนำจาก iCafeForex
เทรด Forex เบื้องต้น | วิธีเลือก Timeframe | Fibonacci Retracement