- บทนำ
- สถาปัตยกรรม
- สิ่งที่ต้องเตรียม
- 1. ติดตั้ง Certbot
- 2. Nginx + Certbot
- 3. Apache + Certbot
- 4. HTTP-01 Challenge
- 5. Webroot Mode
- 6. Wildcard Certificate ด้วย DNS-01
- 7. ตรวจสอบ Certificate
- 8. Automatic Renewal
- 9. Nginx Reverse Proxy
- 10. Docker Application + Nginx บน Host
- 11. คำสั่งที่ใช้บ่อย
- Troubleshooting
- สรุป
- เอกสารอ้างอิง
#บทนำ
Let's Encrypt คือ Certificate Authority (CA) ที่ให้บริการ TLS/SSL Certificate โดยอัตโนมัติผ่านโปรโตคอล ACME ส่วน Certbot คือ ACME client ที่นิยมใช้สำหรับขอ ติดตั้ง และต่ออายุ Certificate ให้เว็บเซิร์ฟเวอร์ เช่น Nginx และ Apache
#สถาปัตยกรรม
Browser
|
HTTPS :443
v
Nginx / Apache
|
TLS Certificate
|
Certbot <---- ACME ----> Let's Encrypt
|
Domain validation
+-- HTTP-01 : port 80
+-- DNS-01 : DNS TXT record
#สิ่งที่ต้องเตรียม
- Domain เช่น
example.com - DNS A/AAAA record ชี้มายัง Public IP ของ Server
- เปิด TCP port 80 และ 443
- Ubuntu/Debian Server พร้อมสิทธิ์ sudo
- Nginx หรือ Apache ทำงานอยู่
ตรวจสอบ DNS:
nslookup example.com
# หรือ
dig example.com
#1. ติดตั้ง Certbot
แนวทางที่ Certbot แนะนำสำหรับ Linux ส่วนใหญ่คือ Snap:
sudo apt update
sudo apt install snapd -y
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/local/bin/certbot
ตรวจสอบ:
certbot --version
#2. Nginx + Certbot
ตรวจสอบ Nginx:
sudo nginx -t
sudo systemctl status nginx
ออก Certificate และให้ Certbot ปรับ Nginx:
sudo certbot --nginx -d example.com -d www.example.com
หากต้องการขอ Certificate อย่างเดียว:
sudo certbot certonly --nginx -d example.com -d www.example.com
Certificate โดยทั่วไปอยู่ที่:
/etc/letsencrypt/live/example.com/fullchain.pem
/etc/letsencrypt/live/example.com/privkey.pem
#3. Apache + Certbot
sudo certbot --apache -d example.com -d www.example.com
ตรวจสอบ configuration:
sudo apachectl configtest
sudo systemctl reload apache2
#4. HTTP-01 Challenge
HTTP-01 เป็นวิธีที่ใช้งานง่ายสำหรับเว็บทั่วไป โดย Let's Encrypt จะเรียก URL ลักษณะนี้:
http://example.com/.well-known/acme-challenge/TOKEN
ดังนั้น port 80 ต้องเข้าถึงได้จาก Internet และ DNS ต้องชี้มายังเครื่องที่ตอบ challenge
Firewall ตัวอย่าง:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw status
#5. Webroot Mode
เหมาะเมื่อไม่ต้องการให้ Certbot แก้ configuration ของ Web Server:
sudo certbot certonly \
--webroot \
-w /var/www/html \
-d example.com \
-d www.example.com
ต้องแน่ใจว่า /.well-known/acme-challenge/ ถูก Web Server ให้บริการได้
#6. Wildcard Certificate ด้วย DNS-01
Wildcard เช่น *.example.com ต้องใช้ DNS-01 challenge:
sudo certbot certonly \
--manual \
--preferred-challenges dns \
-d example.com \
-d '*.example.com'
Certbot จะให้สร้าง TXT record เช่น:
_acme-challenge.example.com
สำหรับ production ควรใช้ DNS plugin/API เพื่อให้ renewal ทำงานอัตโนมัติ แทน manual DNS
ตัวอย่าง Cloudflare plugin แบบ Snap:
sudo snap set certbot trust-plugin-with-root=ok
sudo snap install certbot-dns-cloudflare
#7. ตรวจสอบ Certificate
sudo certbot certificates
หรือ:
openssl s_client -connect example.com:443 -servername example.com
#8. Automatic Renewal
ทดสอบกระบวนการต่ออายุโดยไม่ออก Certificate จริง:
sudo certbot renew --dry-run
ตรวจสอบ systemd timer:
systemctl list-timers | grep certbot
สั่ง renewal ด้วยตนเอง:
sudo certbot renew
หลังต่ออายุ หากระบบของคุณต้อง reload service เอง สามารถใช้ deploy hook เช่น:
sudo certbot renew --deploy-hook "systemctl reload nginx"
#9. Nginx Reverse Proxy
ตัวอย่างก่อนเปิด HTTPS:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
ตรวจสอบและ reload:
sudo nginx -t
sudo systemctl reload nginx
จากนั้น:
sudo certbot --nginx -d app.example.com
โฟลว์จะเป็น:
Internet
|
HTTPS :443
v
Nginx + Let's Encrypt
|
reverse proxy
v
Application :3000
#10. Docker Application + Nginx บน Host
สมมติ container เปิดเฉพาะ localhost:
services:
web:
image: myapp:latest
ports:
- "127.0.0.1:3000:3000"
ให้ Nginx บน host รับ traffic 80/443 และ proxy ไป container จากนั้นใช้ Certbot บน host ตามปกติ วิธีนี้แยก lifecycle ของ TLS ออกจาก application container ได้ชัดเจน
#11. คำสั่งที่ใช้บ่อย
# ดู certificate
sudo certbot certificates
# ขอผ่าน Nginx
sudo certbot --nginx -d example.com
# ขอผ่าน Apache
sudo certbot --apache -d example.com
# ทดสอบ renewal
sudo certbot renew --dry-run
# ต่ออายุ
sudo certbot renew
# ลบ certificate
sudo certbot delete --cert-name example.com
#Troubleshooting
#Challenge failed
ตรวจสอบ DNS, Public IP, NAT/Firewall และ port 80
curl -I http://example.com
sudo ss -lntp | grep -E ':80|:443'
#Nginx configuration error
sudo nginx -t
แก้ configuration ให้ผ่านก่อนเรียก Certbot
#DNS ยังไม่อัปเดต
dig example.com +short
dig www.example.com +short
#ใช้ Cloudflare Proxy
หากพบปัญหา validation ให้ตรวจสอบ DNS record, proxy configuration และ challenge method โดย DNS-01 เหมาะกับ wildcard และระบบที่ต้องการจัดการผ่าน DNS API
#สรุป
สำหรับเว็บทั่วไป แนวทางที่ง่ายคือ Domain → Nginx/Apache → Certbot → Let's Encrypt และใช้ HTTP-01 พร้อมเปิด port 80/443 ส่วน wildcard หรือระบบที่ไม่ต้องการเปิด HTTP validation ควรใช้ DNS-01 พร้อม DNS provider plugin เพื่อให้การต่ออายุทำงานอัตโนมัติ
#เอกสารอ้างอิง
- Let's Encrypt Documentation: https://letsencrypt.org/docs/
- Certbot Instructions: https://certbot.eff.org/instructions