Bảo mật Nginx với Lets Encrypt trên Ubuntu 20.04 với Certbot

Nội dung
Nội dung

1. Chuẩn bị môi trường

Tạo một EC2 Instance Ubuntu 20.04 LTS trên AWS:

  • Ubuntu 20.04
  • t3.micro (cpu cores > 1)
  • public subnet
  • enable public ip

Tạo Security Group nginx

  • open port 80, and 443

Tạo key pair

Cập nhập devops key pair

chmod 400 keypair.pem

2. Install Nginx Ubuntu 20.04 LTS

  • SSH to the Ubuntu server
ssh -i keypair.pem [email protected]

  • Cập nhập Ubuntu packages

sudo apt update

sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
sudo apt update
sudo apt install nginx
  • Start nginx
sudo systemctl start nginx

  • Enable nginx
sudo systemctl enable nginx

  • Kiểm tra nginx status
sudo systemctl status nginx

3. Thiết lập Nginx Server Block

  • Kiểm tra nginx cấu hình chính
cat /etc/nginx/nginx.conf
  • Kiểm tra mặc định cấu hình nginx
cat /etc/nginx/conf.d/default.conf
  • Tạo folder cho website
sudo mkdir -p /var/www/kienletv.com/html
  • Kiểm tra ownership
sudo chown -R $USER:$USER /var/www/kienletv.com/html

  • Cập nhập permissions
sudo chmod -R 755 /var/www/kienletv.com/
  • Tạo một web page
  • vi /var/www/kienletv.com/html/index.html
<html>
    <head>
        <title>Welcome to kienletv.com!</title>
    </head>
    <body>
        <h1>Success!  The kienletv.com server block is working!</h1>
    </body>
</html>
  • Tạo sites-available directory
sudo mkdir /etc/nginx/sites-available/

  • Tạo sites-enabled directory
sudo mkdir /etc/nginx/sites-enabled
  • Tạo nginx server block
sudo vi /etc/nginx/sites-available/kienletv.com
server {
        listen 80;

        root /var/www/kienletv.com/html;
        index index.html;

        server_name kienletv.com www.kienletv.com;

        location / {
                try_files $uri $uri/ =404;
        }
}
  • Thêm cấu hình
sudo vi /etc/nginx/nginx.conf
include /etc/nginx/sites-enabled/*;
  • Tạo một symlink
sudo ln -s /etc/nginx/sites-available/kienletv.com /etc/nginx/sites-enabled/
  • Test nginx config
sudo nginx -t
  • Reload nginx config
sudo nginx -s reload

  • Tạo A records
  • Kiểm tra DNS (nếu bạn dùng cloudflare enable full strict by ssl/tsl>overview>full_strict)
dig kienletv.com
dig www.kienletv.com

4. Cài Certbot trên Ubuntu 20.04 LTS

  • Vào trang official certbot page
  • Cài snap page
  • Kiểm tra snap version
snap version
  • Nếu chưa cài snap bạn cài snap
apt policy snapd; `apt install snapd`
sudo snap install core; sudo snap refresh core
  • Xoá certbot-auto và tất cả Certbot OS packages nếu cài trước đó
sudo apt-get remove certbot
  • Cài Certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
  • Kiểm tra certbot version
sudo certbot --version

5. Bảo mật Nginx với Lets Encrypt trên Ubuntu 20.04 LTS

  • Test certbot
sudo certbot --nginx --test-cert
  • Mở nginx block
cat /etc/nginx/sites-available/kienletv.com
sudo certbot --nginx
sudo certbot renew --dry-run
  • Kiểm tra systemctl times
systemctl list-timers

6. Clean Up

  • Delete EC2 instance
  • Delete security group nginx
  • Delete key pair devops
  • Remove A records

7. Tham khảo

Bài viết mới nhất