Apache vs Nginx ต่างกันอย่างไร เลือก Web Server ไหนดีสำหรับเว็บของคุณ
Apache vs Nginx: architectural differences, performance, use cases, and which web server to choose.
สารบัญ
Web Server คืออะไร
Web Server คือซอฟต์แวร์ที่รับ HTTP Request จาก Browser และส่ง Response กลับ ไม่ว่าจะเป็นไฟล์ HTML, CSS, JS, รูปภาพ หรือผลลัพธ์จาก PHP/Python ทำหน้าที่เป็นตัวกลางระหว่าง Browser และ Application
A web server is software that receives HTTP requests from browsers and sends back responses — HTML files, CSS, JS, images, or PHP/Python output. It acts as the intermediary between the browser and the application.
- Web Server: Apache, Nginx, LiteSpeed, IIS, Caddy
- รับ Request → Process → ส่ง Response กลับ
- Static File: ส่งไฟล์โดยตรงโดยไม่ต้อง Process
- Dynamic Content: ส่งต่อให้ PHP/Python Process
- Market Share: Nginx 34%, Apache 31% (2024)
Apache คืออะไร
Apache HTTP Server เป็น Web Server ที่เก่าแก่ที่สุดและเคยนิยมที่สุดในโลก เปิดตัวปี 1995 Open Source ฟรี ใช้สถาปัตยกรรม Process-based (Pre-fork MPM) ที่สร้าง Process หรือ Thread แยกสำหรับแต่ละ Connection ตั้งค่าง่ายด้วย .htaccess ต่อ Directory
Apache HTTP Server is the oldest and once most popular web server, launched in 1995. Open source and free, using process-based architecture (Pre-fork MPM) creating separate processes or threads per connection. Easy per-directory configuration via .htaccess.
- เปิดตัวปี 1995: Web Server ดั้งเดิม
- MPM Pre-fork: Process แยกต่อ Connection
- MPM Worker/Event: Thread แทน Process
- htaccess: Config ต่อ Directory ได้ง่าย
- Modules: mod_php, mod_ssl, mod_rewrite
Nginx คืออะไร
Nginx (อ่านว่า Engine-X) เปิดตัวปี 2004 สร้างเพื่อแก้ปัญหา C10K Problem (รับ Connection พร้อมกัน 10,000 ตัว) ใช้สถาปัตยกรรม Event-driven ที่ไม่ Block เหมาะกับ Traffic สูง Static File เร็วกว่า Apache และนิยมใช้เป็น Reverse Proxy
Nginx (pronounced Engine-X) launched in 2004 to solve the C10K Problem (10,000 simultaneous connections). Uses event-driven architecture without blocking — ideal for high traffic. Faster than Apache for static files and popular as a reverse proxy.
- เปิดตัวปี 2004 แก้ C10K Problem
- Event-driven Non-blocking: ไม่สร้าง Process/Thread ต่อ Connection
- Static File: เร็วกว่า Apache 2-5 เท่า
- Reverse Proxy: ยอดนิยม ส่ง Request ไป Backend
- Memory: ใช้น้อยกว่า Apache มาก
สถาปัตยกรรมที่แตกต่างกัน
Apache Pre-fork สร้าง Process ใหม่ต่อ Connection ทำให้ Memory ใช้เยอะ แต่ Process Isolation ดีกว่า Nginx ใช้ Event Loop เดียวจัดการหลาย Connection พร้อมกันโดยไม่ Block ทำให้ Memory น้อย รับ Concurrent Connection สูงกว่ามาก
Apache Pre-fork creates new processes per connection — more memory usage but better process isolation. Nginx uses a single event loop handling many concurrent connections without blocking — less memory, handling far more simultaneous connections.
- Apache Pre-fork: 1 Process/Connection = Memory สูง
- Nginx: Event Loop = Memory ต่ำมาก
- 100 Connection พร้อมกัน: Apache 100 Process vs Nginx 1 Worker
- Nginx Worker: ควรเท่ากับจำนวน CPU Core
- Apache Event MPM: ใหม่กว่า ดีขึ้น แต่ยังแพ้ Nginx
ประสิทธิภาพ Apache vs Nginx
สำหรับ Static File Nginx เร็วกว่า Apache 2-5 เท่า สำหรับ PHP (Dynamic) ความต่างน้อยกว่าเพราะ PHP Execution เป็น Bottleneck หลัก ไม่ใช่ Web Server Nginx ใช้ Memory น้อยกว่า เหมาะ VPS RAM ต่ำ Apache ยืดหยุ่นกว่าในการ Config ต่อ Directory
For static files, Nginx is 2-5× faster than Apache. For PHP (dynamic content), the difference shrinks because PHP execution is the main bottleneck, not the web server. Nginx uses less memory — better for low-RAM VPS. Apache is more flexible for per-directory configuration.
- Static File: Nginx เร็วกว่า 2-5 เท่า
- PHP/Dynamic: ความต่างน้อยกว่า (PHP เป็น Bottleneck)
- Memory: Nginx ใช้น้อยกว่า Apache มาก
- Concurrent Connection: Nginx ดีกว่ามาก
- WordPress: ทั้งคู่ทำได้ Nginx นิยมกว่าในปัจจุบัน
Static vs Dynamic Content
Static File (HTML, CSS, JS, รูป) Nginx ดีกว่าชัดเจน Dynamic Content (PHP, Python) ขึ้นอยู่กับ Configuration มากกว่า Apache ใช้ mod_php รัน PHP ใน Process ตัวเอง Nginx ต้องการ PHP-FPM (FastCGI) แยกต่างหาก ซึ่งที่จริงเร็วกว่า mod_php
For static files (HTML, CSS, JS, images): Nginx clearly wins. For dynamic content (PHP, Python): configuration matters more. Apache uses mod_php running PHP in its own process. Nginx requires separate PHP-FPM (FastCGI), which is actually faster than mod_php.
- Static: Nginx ดีกว่าชัดเจน
- Dynamic: Apache mod_php ง่ายกว่า Setup
- PHP-FPM + Nginx: เร็วกว่า mod_php + Apache
- PHP-FPM + Apache: ใช้ได้เหมือนกัน ดีกว่า mod_php
- แนะนำ: PHP-FPM ทั้งคู่ไม่ว่าจะเลือก Web Server ใด
การ Configuration
Apache .htaccess ให้ Config ต่อ Directory ได้โดยไม่ต้อง Restart Server ดีสำหรับ Shared Hosting ที่แต่ละ User ต้องการ Config แยก Nginx ไม่มี .htaccess ต้อง Config ที่ server.conf แล้ว Reload ยืดหยุ่นน้อยกว่าสำหรับ Shared แต่เร็วกว่า
Apache .htaccess enables per-directory configuration without server restart — ideal for shared hosting where each user needs separate config. Nginx lacks .htaccess; configuration is in server.conf requiring a reload. Less flexible for shared hosting but faster.
- Apache .htaccess: Config ต่อ Directory ไม่ต้อง Restart
- Nginx: Config ที่ nginx.conf → Reload เท่านั้น
- WordPress .htaccess Rules: Apache รอง Support ดีกว่า
- Rewrite Rules: Apache RewriteRule vs Nginx location
- Security Rules: ทั้งคู่ทำได้
Use Case ที่เหมาะกับ Apache หรือ Nginx
Apache เหมาะกับ Shared Hosting, Site ที่ต้องการ .htaccess, Legacy App ที่ Config ไว้กับ Apache Nginx เหมาะกับ High Traffic Site, Static File Serving, Reverse Proxy หน้า Node.js/Python, VPS RAM น้อย และ Modern WordPress
Apache is ideal for shared hosting, sites needing .htaccess, and legacy apps configured for Apache. Nginx suits high-traffic sites, static file serving, reverse proxying Node.js/Python apps, low-RAM VPS, and modern WordPress.
- Apache: Shared Hosting ดีกว่า (.htaccess)
- Nginx: High Traffic, Static, Reverse Proxy
- Nginx: VPS RAM ต่ำ (ใช้น้อยกว่า)
- Nginx + PHP-FPM: WordPress ที่เร็วที่สุด
- ปัจจุบัน: Hosting ส่วนใหญ่ย้ายมา Nginx แล้ว
LiteSpeed ทางเลือกที่สาม
LiteSpeed Web Server เป็น Commercial Web Server ที่ Compatible กับ Apache Config แต่เร็วกว่ามาก มี OpenLiteSpeed เวอร์ชัน Open Source ฟรี LiteSpeed Cache Plugin สำหรับ WordPress ทำให้ WordPress เร็วที่สุดในบรรดา Web Server
LiteSpeed Web Server is a commercial web server compatible with Apache configuration but much faster. OpenLiteSpeed is the free open-source version. The LiteSpeed Cache plugin for WordPress makes it the fastest web server option for WordPress.
- LiteSpeed: Commercial, ราคา License สูง
- OpenLiteSpeed: Open Source ฟรี
- compat กับ .htaccess: ง่ายย้ายจาก Apache
- LiteSpeed Cache Plugin: WordPress เร็วที่สุด
- Hosting ไทยบางเจ้าใช้ LiteSpeed เช่น AsiaGB