Thiết lập SSL/TLS miễn phí với Let’s Encrypt để secure Ubuntu server chuẩn HTTPS
Website chạy HTTP → dữ liệu đi “trần”: login, cookie, form, API token dễ bị sniff/MITM. HTTPS → mã hóa, xác thực domain, tăng trust, tốt SEO, bắt buộc cho nhiều API hiện đại. Tin tốt: Let’s Encrypt cấp SSL/TLS cert miễn phí, tự động hóa tốt, renew dễ. Trên Ubuntu server, combo phổ biến nhất: Nginx/Apache + Certbot + Let’s Encrypt.
Bài này hướng dẫn setup chuẩn HTTPS cho Ubuntu server, kèm kiểm tra, auto-renew, hardening cơ bản.
1. Let’s Encrypt là gì?
Let’s Encrypt → CA miễn phí, tự động, mở. Cert được browser tin cậy. Hạn cert: 90 ngày → cần auto-renew.Ưu điểm:
– Miễn phí – Tự động cấp/gia hạn – Hỗ trợ wildcard qua DNS challenge – Tương thích Nginx, Apache – Chuẩn ACME
Nhược điểm:
– Cert ngắn hạn → phải đảm bảo renew chạy ổn – Rate limit → test sai nhiều lần có thể bị chặn tạm – Cần domain trỏ đúng server
2. Điều kiện trước khi cài SSL/TLS
Trước khi chạy Certbot, cần:
– Ubuntu server: 20.04 / 22.04 / 24.04
– User có quyền sudo
– Domain đã trỏ DNS về IP server
– Web server đang chạy: Nginx hoặc Apache
– Firewall mở port:
– 80/tcp HTTP
– 443/tcp HTTPS
Kiểm tra IP:
curl -4 ifconfig.meKiểm tra DNS:
dig +short example.com
dig +short www.example.comKết quả phải là IP server.
Kiểm tra web server:
sudo systemctl status nginxhoặc:
sudo systemctl status apache23. Mở firewall cho HTTP/HTTPS
Nếu dùng UFW:
sudo ufw statusNginx:
sudo ufw allow 'Nginx Full'Apache:
sudo ufw allow 'Apache Full'Hoặc mở thủ công:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcpKiểm tra:
sudo ufw statusPort 80 phải mở vì Let’s Encrypt thường dùng HTTP-01 challenge để xác minh domain.
4. Cài Certbot trên Ubuntu
Cách khuyến nghị: dùng snap.
sudo apt update
sudo apt install snapd -y
sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbotKiểm tra:
certbot --versionNếu thấy version → OK.
5. Cấu hình Nginx trước khi cấp cert
Nginx cần server block đúng server_name.
Ví dụ file:
sudo nano /etc/nginx/sites-available/example.comNội dung mẫu:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
}
Tạo thư mục web:
sudo mkdir -p /var/www/example.com
echo "OK" | sudo tee /var/www/example.com/index.html
sudo chown -R www-data:www-data /var/www/example.comEnable site:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxTest:
curl -I http://example.comNếu trả 200 hoặc 301/302 hợp lý → sẵn sàng.
6. Cấp SSL/TLS cho Nginx bằng Certbot
Chạy:
sudo certbot --nginx -d example.com -d www.example.comCertbot sẽ:
– Xác minh domain – Lấy cert – Sửa config Nginx – Thêm HTTPS block – Có thể hỏi redirect HTTP → HTTPS
Chọn redirect nếu muốn chuẩn:
2: RedirectSau khi xong, kiểm tra:
sudo nginx -t
sudo systemctl reload nginxMở browser:
https://example.comNếu có ổ khóa → OK.
7. Cấp SSL/TLS cho Apache
Cài plugin Apache:
sudo snap install --classic certbotChạy:
sudo certbot --apache -d example.com -d www.example.comCertbot sẽ tự chỉnh VirtualHost.
Kiểm tra Apache:
sudo apachectl configtest
sudo systemctl reload apache2Test:
curl -I https://example.com8. Kiểm tra cert đã cấp
Liệt kê cert:
sudo certbot certificatesThông tin thường thấy:
Certificate Name: example.com
Domains: example.com www.example.com
Expiry Date: ...
Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/example.com/privkey.pemFile quan trọng:
– fullchain.pem → cert chain
– privkey.pem → private key
– cert.pem → domain cert
– chain.pem → intermediate cert
Không share:
/etc/letsencrypt/live/example.com/privkey.pemPrivate key lộ → phải revoke + cấp lại.
9. Auto-renew SSL/TLS
Cert Let’s Encrypt hết hạn sau 90 ngày. Certbot snap thường tạo systemd timer tự động.
Kiểm tra:
systemctl list-timers | grep certbotTest renew giả lập:
sudo certbot renew --dry-runNếu output không lỗi → auto-renew ổn.
Gia hạn thật:
sudo certbot renewCertbot chỉ renew khi cert gần hết hạn. Không cần cron thủ công nếu timer đã có.
10. Redirect HTTP sang HTTPS chuẩn
Certbot thường tự thêm redirect. Nếu muốn cấu hình tay với Nginx:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}HTTPS block:
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Reload:
sudo nginx -t
sudo systemctl reload nginx11. Bật HTTP/2
HTTP/2 → multiplexing, header compression, performance tốt hơn.
Nginx:
listen 443 ssl http2;Apache cần module:
sudo a2enmod http2
sudo systemctl restart apache2VirtualHost:
Protocols h2 http/1.1Kiểm tra:
curl -I --http2 https://example.com12. Hardening TLS cơ bản
TLS config yếu → vẫn có HTTPS nhưng điểm bảo mật thấp. Với Nginx, thêm:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;Thêm security headers:
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;HSTS:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;Cảnh báo: chỉ bật HSTS khi chắc chắn HTTPS hoạt động ổn cho toàn domain/subdomain. Sai config → user bị kẹt HTTPS.
13. Kiểm tra chất lượng HTTPS
Dùng SSL Labs:
https://www.ssllabs.com/ssltest/Mục tiêu:
– Grade A hoặc A+ – Không TLS 1.0/1.1 – Chain đầy đủ – Cert không gần hết hạn – Redirect đúng – Không mixed content
Kiểm tra CLI:
curl -Iv https://example.comKiểm tra ngày hết hạn:
echo | openssl s_client -servername example.com -connect example.com:443 2>/dev/null | openssl x509 -noout -dates14. Lỗi thường gặp
DNS chưa trỏ đúng
Lỗi:
unauthorizedFix:
dig +short example.comDNS phải về IP server.
Port 80 bị chặn
Lỗi challenge fail. Fix:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcpKiểm tra cloud firewall: AWS Security Group, GCP Firewall, Azure NSG, VPS panel.
Nginx config sai
Test:
sudo nginx -tXem log:
sudo journalctl -u nginx -xeRate limit Let’s Encrypt
Test nhiều lần → bị limit. Dùng dry-run:
sudo certbot renew --dry-runVới cert mới, cân nhắc staging:
sudo certbot --nginx --staging -d example.com -d www.example.com15. Khi nào dùng DNS challenge?
HTTP-01 cần port 80 public. Nếu server không public hoặc cần wildcard:
*.example.com→ dùng DNS-01 challenge.
Ví dụ:
sudo certbot certonly --manual --preferred-challenges dns -d example.com -d "*.example.com"Certbot yêu cầu tạo TXT record:
_acme-challenge.example.comSau khi DNS propagate → cert cấp.
Production nên dùng DNS plugin của Cloudflare/Route53 để auto-renew wildcard.
Kết luận thực tế
Ubuntu server chạy HTTPS chuẩn không khó: domain đúng DNS, port mở, Certbot cài đúng, web server config sạch. Với Nginx/Apache, Let’s Encrypt giúp cấp cert miễn phí trong vài phút, redirect HTTP → HTTPS tự động, renew qua systemd timer.
Checklist cuối:
– Domain trỏ đúng IP
– Port 80/443 mở
– sudo certbot --nginx hoặc --apache chạy OK
– sudo certbot renew --dry-run pass
– SSL Labs đạt A/A+
– HSTS chỉ bật khi chắc chắn
– Private key được bảo vệ
Làm đúng các bước trên → Ubuntu server có HTTPS miễn phí, bảo mật, tự gia hạn, đủ chuẩn production.
Bình luận (0)
Chưa có bình luận. Hãy là người đầu tiên!