Apache Web Server — คู่มือติดตั้ง Config และ Tuning ฉบับสมบูรณ์ 2026

22/02/2026 | อ.บอม (Bom) | SiamCafe.net Since 1997

Apache Web Server คู่มือฉบับสมบูรณ์ 2026

Apache HTTP Server หรือที่เรียกสั้นๆ ว่า Apache เป็น Web Server ที่ครองตลาดมาตั้งแต่ปี 1995 แม้ว่า NGINX จะมาแรงในช่วง 10 ปีที่ผ่านมา แต่ Apache ยังคงเป็น Web Server ที่ใช้งานมากที่สุดในโลก คิดเป็นประมาณ 31% ของ Website ทั้งหมดตามสถิติ Netcraft 2026

จากประสบการณ์ดูแล Server มากว่า 28 ปี ผมพบว่า Apache ยังคงเป็นตัวเลือกที่ดีที่สุดสำหรับหลายสถานการณ์ โดยเฉพาะเมื่อต้องการ .htaccess, mod_rewrite หรือรัน PHP Application อย่าง WordPress บทความนี้จะพาคุณตั้ง Apache ตั้งแต่ติดตั้ง จน Config VirtualHost, SSL, Tuning MPM และ Security Hardening ครบทุกขั้นตอน

1. Apache คืออะไร และทำไมยังสำคัญในปี 2026

Apache HTTP Server เป็น Open Source Web Server ที่พัฒนาโดย Apache Software Foundation เขียนด้วยภาษา C ทำงานบน Linux, Windows, macOS และ BSD จุดเด่นที่ทำให้ Apache ยังอยู่ได้คือ:

2. ติดตั้ง Apache บน Ubuntu 24.04/22.04

# Update package list
sudo apt update

# ติดตั้ง Apache
sudo apt install -y apache2

# ตรวจสอบ Version
apache2 -v
# Server version: Apache/2.4.62 (Ubuntu)

# ตรวจสอบ Status
sudo systemctl status apache2
# ● apache2.service - The Apache HTTP Server
#      Active: active (running)

# เปิด Firewall
sudo ufw allow 'Apache Full'
sudo ufw status

เปิด Browser ไปที่ http://your-server-ip จะเห็น Apache Default Page แสดงว่าติดตั้งสำเร็จ

โครงสร้างไฟล์ Config ของ Apache

/etc/apache2/
├── apache2.conf          # Config หลัก
├── ports.conf            # Port ที่ Listen (80, 443)
├── envvars               # Environment Variables
├── sites-available/      # VirtualHost Config (ทั้งหมด)
│   ├── 000-default.conf  # Default HTTP
│   └── default-ssl.conf  # Default HTTPS
├── sites-enabled/        # Symlink ไปที่ sites-available (ตัวที่เปิดใช้)
├── mods-available/       # Module ทั้งหมด
├── mods-enabled/         # Module ที่เปิดใช้
└── conf-available/       # Config เสริม

Apache ใช้ระบบ a2ensite/a2dissite และ a2enmod/a2dismod ในการเปิด/ปิด Site และ Module ทำให้จัดการง่ายกว่าการแก้ Config File ตรงๆ

3. Config VirtualHost — โฮสต์หลายเว็บบน Server เดียว

# สร้าง Config สำหรับเว็บใหม่
sudo nano /etc/apache2/sites-available/mysite.conf
<VirtualHost *:80>
    ServerName mysite.com
    ServerAlias www.mysite.com
    DocumentRoot /var/www/mysite
    
    <Directory /var/www/mysite>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
    CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
</VirtualHost>
# สร้างโฟลเดอร์เว็บ
sudo mkdir -p /var/www/mysite
echo "<h1>Hello mysite.com</h1>" | sudo tee /var/www/mysite/index.html

# เปิดใช้ Site
sudo a2ensite mysite.conf

# ปิด Default Site
sudo a2dissite 000-default.conf

# ตรวจสอบ Config
sudo apache2ctl configtest
# Syntax OK

# Reload Apache
sudo systemctl reload apache2
💡 เคล็ดลับ: ใช้ apache2ctl configtest ทุกครั้งก่อน Reload เพื่อเช็คว่า Config ไม่มี Syntax Error ป้องกัน Apache ล่มจากการแก้ Config ผิด

4. SSL/TLS — HTTPS ด้วย Let's Encrypt

# ติดตั้ง Certbot
sudo apt install -y certbot python3-certbot-apache

# เปิด mod_ssl และ mod_headers
sudo a2enmod ssl headers rewrite

# ออก Certificate
sudo certbot --apache -d mysite.com -d www.mysite.com

# Certbot จะ:

ตรวจสอบ Auto-Renew

# ทดสอบ Renew
sudo certbot renew --dry-run

# ดู Timer
sudo systemctl list-timers | grep certbot
# NEXT                         UNIT
# Mon 2026-02-23 00:00:00 ICT  certbot.timer

Config SSL ที่แนะนำ (A+ Rating)

<VirtualHost *:443>
    ServerName mysite.com
    DocumentRoot /var/www/mysite
    
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/mysite.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/mysite.com/privkey.pem
    
    # Modern SSL Config
    SSLProtocol -all +TLSv1.2 +TLSv1.3
    SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384
    SSLHonorCipherOrder off
    SSLSessionTickets off
    
    # HSTS
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
    
    # Security Headers
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
</VirtualHost>

5. mod_rewrite — URL Rewriting

mod_rewrite เป็น Module ที่ทรงพลังที่สุดของ Apache ใช้เปลี่ยน URL, Redirect, สร้าง Clean URL สำหรับ CMS ต่างๆ

# เปิด mod_rewrite
sudo a2enmod rewrite
sudo systemctl restart apache2

ตัวอย่าง .htaccess ที่ใช้บ่อย

# WordPress Permalink
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

# Block Bad Bots
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (SemrushBot|AhrefsBot|MJ12bot) [NC]
RewriteRule .* - [F,L]
⚠️ ข้อควรระวัง: .htaccess ถูก Parse ทุก Request ทำให้ช้าลง ถ้าเป็น VPS/Dedicated Server ให้ย้าย RewriteRule ไปอยู่ใน VirtualHost Config แทน แล้วตั้ง AllowOverride None จะเร็วขึ้น 10-20%

6. MPM — Multi-Processing Module

MPM คือหัวใจของ Apache ที่กำหนดวิธีรับ Connection จาก Client มี 3 แบบ:

MPMวิธีทำงานเหมาะกับRAM ต่อ Connection
Prefork1 Process = 1 Connectionmod_php (เก่า)~30-50 MB
Worker1 Process = หลาย ThreadPHP-FPM~5-10 MB
EventEvent Loop + Thread PoolPHP-FPM (แนะนำ)~2-5 MB
# ดู MPM ปัจจุบัน
apache2ctl -V | grep MPM
# Server MPM: event

# ถ้าเป็น Prefork อยู่ เปลี่ยนเป็น Event
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2

Tuning MPM Event สำหรับ Production

# /etc/apache2/mods-available/mpm_event.conf
<IfModule mpm_event_module>
    StartServers             3
    MinSpareThreads         75
    MaxSpareThreads        250
    ThreadLimit             64
    ThreadsPerChild         25
    MaxRequestWorkers      400
    MaxConnectionsPerChild  10000
</IfModule>

สูตรคำนวณ: Server มี RAM 4GB ลบ RAM สำหรับ OS + PHP-FPM (~1.5GB) เหลือ 2.5GB สำหรับ Apache แต่ละ Thread ใช้ ~5MB ดังนั้น MaxRequestWorkers = 2500/5 = 500 แต่ตั้งไว้ 400 เพื่อเหลือ Buffer

7. PHP-FPM — แยก PHP ออกจาก Apache

# ติดตั้ง PHP-FPM
sudo apt install -y php8.3-fpm php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip

# เปิด Module ที่จำเป็น
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.3-fpm

# Restart
sudo systemctl restart apache2 php8.3-fpm
# /etc/php/8.3/fpm/pool.d/www.conf — Tuning
[www]
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 500

# สำหรับ Server RAM 4GB ที่รัน WordPress
# pm.max_children = RAM_for_PHP / avg_PHP_process_size
# = 2048MB / 40MB = 50
💡 เคล็ดลับ: ดู RAM ที่ PHP Process ใช้จริงด้วย ps aux --sort -rss | grep php-fpm | head -5 แล้วเอาค่าเฉลี่ย RSS มาคำนวณ pm.max_children

8. Performance Tuning

เปิด Compression

# เปิด mod_deflate
sudo a2enmod deflate

# Config
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css
    AddOutputFilterByType DEFLATE text/javascript application/javascript application/json
    AddOutputFilterByType DEFLATE application/xml text/xml
    AddOutputFilterByType DEFLATE image/svg+xml
    DeflateCompressionLevel 6
</IfModule>

Browser Cache

# เปิด mod_expires
sudo a2enmod expires

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/html "access plus 1 hour"
</IfModule>

KeepAlive Settings

# /etc/apache2/apache2.conf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

KeepAliveTimeout ตั้ง 5 วินาทีพอ อย่าตั้งสูงเกินไป (เช่น 15) เพราะจะกิน Connection Slot โดยไม่จำเป็น

9. Security Hardening

# /etc/apache2/conf-available/security.conf

# ซ่อน Version
ServerTokens Prod
ServerSignature Off

# ปิด Directory Listing
<Directory /var/www>
    Options -Indexes
</Directory>

# ปิด ETag (ป้องกัน Information Leakage)
FileETag None

# จำกัด HTTP Methods
<Directory /var/www>
    <LimitExcept GET POST HEAD>
        Require all denied
    </LimitExcept>
</Directory>

# ป้องกัน Clickjacking
Header set X-Frame-Options "SAMEORIGIN"

# ป้องกัน MIME Type Sniffing
Header set X-Content-Type-Options "nosniff"

# Content Security Policy
Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'"

ติดตั้ง mod_security (WAF)

# ติดตั้ง ModSecurity
sudo apt install -y libapache2-mod-security2
sudo a2enmod security2

# ใช้ OWASP Core Rule Set
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
sudo sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/modsecurity/modsecurity.conf

# Restart
sudo systemctl restart apache2

# ตรวจสอบ
curl -I 'http://mysite.com/?id=1+OR+1=1'
# HTTP/1.1 403 Forbidden (mod_security blocked SQL Injection)

10. Apache เป็น Reverse Proxy

# เปิด Modules
sudo a2enmod proxy proxy_http proxy_balancer lbmethod_byrequests

# Config Reverse Proxy
<VirtualHost *:443>
    ServerName app.mysite.com
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/app.mysite.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/app.mysite.com/privkey.pem
    
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
    
    # Timeout
    ProxyTimeout 60
    
    # WebSocket Support
    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteCond %{HTTP:Connection} upgrade [NC]
    RewriteRule /(.*) ws://127.0.0.1:3000/$1 [P,L]
</VirtualHost>

Load Balancing

<Proxy "balancer://myapp">
    BalancerMember http://192.168.1.10:3000
    BalancerMember http://192.168.1.11:3000
    BalancerMember http://192.168.1.12:3000
    ProxySet lbmethod=byrequests
</Proxy>

ProxyPass / balancer://myapp/
ProxyPassReverse / balancer://myapp/

11. Benchmark — Apache vs NGINX

ทดสอบด้วย wrk บน Server เดียวกัน (4 vCPU, 8GB RAM, SSD):

TestApache Event + PHP-FPMNGINX + PHP-FPM
Static HTML (1KB)18,500 req/s25,200 req/s
Static Image (100KB)4,200 req/s5,800 req/s
PHP (WordPress Home)850 req/s920 req/s
PHP (WP + OPcache + Redis)2,100 req/s2,250 req/s
Memory (idle, 100 workers)180 MB45 MB

NGINX เร็วกว่า ~35% สำหรับ Static Content แต่สำหรับ PHP Application ที่ใช้ PHP-FPM ทั้งคู่ ต่างกันแค่ 5-8% ถ้าใช้ Cache ทั้งสองฝั่ง Performance ยิ่งใกล้เคียงกัน

12. เปรียบเทียบ Apache กับ Web Server อื่นปี 2026

FeatureApacheNGINXCaddy
.htaccess
Auto SSL❌ (ต้อง Certbot)❌ (ต้อง Certbot)✅ ในตัว
Module System✅ 500+ Modules✅ แต่ Compile-time✅ Plugin
HTTP/2, HTTP/3✅/❌✅/✅✅/✅
Config SyntaxXML-likeC-likeCaddyfile (ง่ายสุด)
Shared Hosting✅ มาตรฐาน⚠️ บางเจ้า

13. สรุป

Apache HTTP Server ยังคงเป็น Web Server ที่เชื่อถือได้ในปี 2026 โดยเฉพาะสำหรับ PHP Application, Shared Hosting และองค์กรที่ต้องการ .htaccess กุญแจสำคัญคือ:

📚 แนะนำเพิ่มเติม

ถ้าคุณดูแล Server หลายตัว แนะนำให้ศึกษา Automation ด้วย — ทั้งเรื่อง IT Infrastructure และการลงทุน iCafeForex.com มีคอร์สสอนระบบ EA Semi-Auto Trading ที่ทำงานอัตโนมัติ 24/7 เหมือน Apache Server ที่ Serve Request ตลอดเวลา

ติดตาม XMSignal — สัญญาณเทรดฟรี เพื่อเสริมรายได้ Passive Income นอกเหนือจากงาน IT

อ.บอม (Bom)
อ.บอม (Bom)
IT Expert & Founder of SiamCafe.net Since 1997
ประสบการณ์ IT 28+ ปี | ดูแล Server หลายร้อยเครื่อง