Thiết lập SSL miễn phí Let’s Encrypt cho Ubuntu chuẩn HTTPS

13/05/2026 · P T P · Chung

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.me

Kiểm tra DNS:

dig +short example.com
dig +short www.example.com

Kết quả phải là IP server.

Kiểm tra web server:

sudo systemctl status nginx

hoặc:

sudo systemctl status apache2

3. Mở firewall cho HTTP/HTTPS

Nếu dùng UFW:

sudo ufw status

Nginx:

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/tcp

Kiểm tra:

sudo ufw status

Port 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/certbot

Kiểm tra:

certbot --version

Nế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.com

Nộ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.com

Enable site:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Test:

curl -I http://example.com

Nế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.com

Certbot 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: Redirect

Sau khi xong, kiểm tra:

sudo nginx -t
sudo systemctl reload nginx

Mở browser:

https://example.com

Nếu có ổ khóa → OK.


7. Cấp SSL/TLS cho Apache

Cài plugin Apache:

sudo snap install --classic certbot

Chạy:

sudo certbot --apache -d example.com -d www.example.com

Certbot sẽ tự chỉnh VirtualHost.

Kiểm tra Apache:

sudo apachectl configtest
sudo systemctl reload apache2

Test:

curl -I https://example.com

8. Kiểm tra cert đã cấp

Liệt kê cert:

sudo certbot certificates

Thô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.pem

File 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.pem

Private 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 certbot

Test renew giả lập:

sudo certbot renew --dry-run

Nếu output không lỗi → auto-renew ổn.

Gia hạn thật:

sudo certbot renew

Certbot 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 nginx

11. 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 apache2

VirtualHost:

Protocols h2 http/1.1

Kiểm tra:

curl -I --http2 https://example.com

12. 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.com

Kiể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 -dates

14. Lỗi thường gặp

DNS chưa trỏ đúng

Lỗi:

unauthorized

Fix:

dig +short example.com

DNS phải về IP server.

Port 80 bị chặn

Lỗi challenge fail. Fix:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Kiểm tra cloud firewall: AWS Security Group, GCP Firewall, Azure NSG, VPS panel.

Nginx config sai

Test:

sudo nginx -t

Xem log:

sudo journalctl -u nginx -xe

Rate limit Let’s Encrypt

Test nhiều lần → bị limit. Dùng dry-run:

sudo certbot renew --dry-run

Với cert mới, cân nhắc staging:

sudo certbot --nginx --staging -d example.com -d www.example.com

15. 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.com

Sau 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.

#chuan #encrypt #mien #thiet #ubuntu
Chia sẻ:
← Trước
Secure Ubuntu Không Downtime: Tự Động Vá Lỗ Hổng 24/7

Bài viết tương tự

Bình luận

Chưa có bình luận. Hãy là người đầu tiên!