อ้างอิงจาก บทความของ Digital Ocean

สิ่งที่จะต้องมี⌗

  • Ubuntu 22.04
  • nginx 1.18 ( ถ้ายังไม่มีลงด้วยคำสั่ง sudo apt install nginx )
  • snap ( น่าจะติดมากับ Ubuntu 22.04 อยู่แล้ว )

ข้อมูลเบื้องต้น⌗

  • ชื่อ Server ที่ต้องการใช้งาน (Web Server Name): tech.snail.run
  • IP ของ Server ที่จะรัน (Web Server IP)

ขั้นตอนการขอ SSL Certificate⌗

  1. ตั้งค่าที่ DNS ให้ IP ของ tech.snail.run เป็น IP ของเครื่องที่ต้องการทำเป็น Web Server
  2. ติดตั้ง certbot
sudo snap install core
sudo snap refresh core
sudo apt remove certbot
sudo snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot
  1. สร้างหรือแก้ไข config file ของ site ที่ต้องการ ในกรณีนี้คือ /etc/nginx/sites-available/tech.snail.run โดยจะต้องมี server block ที่มี directive server_name เป็น tech.snail.run
  2. ตรวจสอบ config nginx ด้วยคำสั่ง
sudo nginx -t

ถ้าพบ error ให้แก้ก่อนที่จะลอง reload ด้วยคำสั่ง

sudo systemctl reload nginx
  1. จัดการกับ Firewall ( ในกรณีที่เปิดไว้ ) เพื่อปล่อยให้ packet HTTPS สามารถเข้าและออกจากเครื่องได้
  2. ขอ SSL Certificate ( ซะที )
sudo certbot --nginx -d tech.snail.run

ถ้าได้ Output หน้าตาแบบนี้ถือว่าใช้ได้

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for tech.snail.run

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/tech.snail.run/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/tech.snail.run/privkey.pem
This certificate expires on 2022-12-18.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for draft-tech.snail.run to /etc/nginx/sites-enabled/tech.snail.run
Congratulations! You have successfully enabled HTTPS on https://tech.snail.run

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Notes:ในตัวอย่างของ Digital Ocean เค้าจะขอทั้ง example.com และ www.example.com ซึ่งเราก็สามารถทำได้เหมือนกัน เพียงแต่เราก็จะต้องตั้ง DNS ให้ IP ของ example.com และ www.example.com ชี้มาที่เครื่องที่ขอ Certificate ด้วย

ตรวจสอบว่าต่ออายุอัตโนมัติได้หรือไม่⌗

  1. ตรวจสอบสถานะของ Certbot Renew Service
sudo systemctl status snap.certbot.renew.service

ควรจะได้ผลลัพธ์ประมาณนี้

○ snap.certbot.renew.service - Service for snap application certbot.renew
     Loaded: loaded (/etc/systemd/system/snap.certbot.renew.service; static)
     Active: inactive (dead)
TriggeredBy: ● snap.certbot.renew.timer

นั่นคือ มี Service เตรียมรันแล้ว และจะถูกเรียกให้รันด้วย service ที่ชื่อ snap.certbot.renew.timer ( ค่า default ของเครื่องผม ที่ได้มาจะตั้งไว้วันละ 2 ครั้ง คือเวลา 3.18 และ 21.19 ) ตรวจสอบเวลาของ timer ได้ที่ไฟล์ /etc/systemd/system/snap.certbot.renew.timer

จากนั้นให้ลองทดสอบการต่ออายุด้วยคำสั่ง

sudo certbox renew --dry-run

ควรจะได้ผลลัพธ์ที่น่าพึงพอใจประมาณนี้

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/tech.snail.run.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for tech.snail.run

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded: 
  /etc/letsencrypt/live/tech.snail.run/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

ผมเพิ่งจะลองทำครั้งแรกที่เครื่องนี้ ไว้อีกเกือบ ๆ 3 เดือนมาดูอีกทีว่ามันต่ออายุให้เองสำเร็จหรือเปล่า