การทำ HTTPS ด้วย Certbot และ Nginx บน Ubuntu Server


Making HTTPS with Certbot and Nginx on Ubuntu Server

การรักษาความปลอดภัยของเว็บไซต์เป็นสิ่งสำคัญอย่างยิ่งในยุคดิจิทัลปัจจุบัน โดยเฉพาะการใช้โปรโตคอล HTTPS ซึ่งช่วยให้ข้อมูลที่ส่งผ่านเว็บไซต์ของคุณถูกเข้ารหัสและปลอดภัย ในบทความนี้เราจะมาสอนวิธีการติดตั้งและกำหนดค่า HTTPS บนเซิร์ฟเวอร์ Ubuntu ด้วย Certbot และ Nginx

สิ่งที่ต้องมี

  • เซิร์ฟเวอร์ Ubuntu แนะนำ Ubuntu 20.04 LTS หรือเวอร์ชันล่าสุด
  • Nginx หากยังไม่มี สามารถติดตั้งได้ตามขั้นตอนในบทความนี้
  • โดเมนเนมที่ชี้ไปยังเซิร์ฟเวอร์ของคุณ เช่น example.com และมี DNS records ชี้ไปยัง IP ของเซิร์ฟเวอร์
  • สิทธิ์การเข้าถึงแบบ root หรือผู้ใช้ที่มีสิทธิ์ sudo บนเซิร์ฟเวอร์

วิธีการชี้โดเมนไปยัง IP ของเซิร์ฟเวอร์

  • เพิ่ม DNS records สำหรับโดเมน certbot.pargorn.com ที่ชี้ไปยัง IP ของเซิร์ฟเวอร์
  • เลือก A record และใส่ IP ของเซิร์ฟเวอร์

ขั้นตอนที่ 1 อัปเดตระบบ

  • เริ่มต้นด้วยการอัปเดตแพคเกจของระบบให้เป็นเวอร์ชันล่าสุด
อัปเดตแพคเกจ
sudo apt update
sudo apt upgrade -y

ขั้นตอนที่ 2 ติดตั้ง Nginx

  • หากยังไม่ได้ติดตั้ง Nginx สามารถติดตั้งได้ด้วยคำสั่ง
ติดตั้ง Nginx
sudo apt install nginx -y
  • หลังจากติดตั้งเสร็จสิ้น ตรวจสอบให้แน่ใจว่า Nginx ทำงานอยู่
ตรวจสอบสถานะ Nginx
sudo systemctl status nginx
  • คุณควรเห็นสถานะ active (running) ถ้า Nginx ทำงานถูกต้อง

statis nginx

ขั้นตอนที่ 3 สร้างเว็บไซต์ “Hello Certbot”

เราจะสร้างเว็บไซต์ง่ายๆ ที่แสดงข้อความ “Hello Certbot” สำหรับโดเมน certbot.pargorn.com

1. สร้างไดเรกทอรีสำหรับเว็บไซต์

สร้างไดเรกทอรี
sudo mkdir -p /var/www/certbot.pargorn.com/html

2. กำหนดสิทธิ์ให้กับไดเรกทอรี

กำหนดสิทธิ์
sudo chown -R $USER:$USER /var/www/certbot.pargorn.com/html
sudo chmod -R 755 /var/www/certbot.pargorn.com

3. สร้างไฟล์ HTML สำหรับเว็บไซต์

  • สร้างไฟล์ index.html ที่จะแสดงข้อความ “Hello Certbot”
สร้างไฟล์ index.html
nano /var/www/certbot.pargorn.com/html/index.html

4. จากนั้นเพิ่มเนื้อหาดังนี้

  • บันทึกไฟล์โดยกด Ctrl + O แล้วออกจาก nano ด้วย Ctrl + X
index.html
<!DOCTYPE html>
<html>
<head>
    <title>Hello Certbot</title>
</head>
<body>
    <h1>Hello Certbot</h1>
</body>
</html>

5. สร้างไฟล์การตั้งค่าเซิร์ฟเวอร์สำหรับ Nginx

  • สร้างไฟล์การตั้งค่าสำหรับเว็บไซต์ certbot.pargorn.com
สร้างไฟล์การตั้งค่าเซิร์ฟเวอร์
sudo nano /etc/nginx/sites-available/certbot.pargorn.com

6. เพิ่มเนื้อหาดังนี้

  • บันทึกไฟล์โดยกด Ctrl + O แล้วออกจาก nano ด้วย Ctrl + X
certbot.pargorn.com
server {
    listen 80;
    listen [::]:80;
 
    server_name certbot.pargorn.com;
 
    root /var/www/certbot.pargorn.com/html;
    index index.html index.htm;
 
    location / {
        try_files $uri $uri/ =404;
    }
}

7. เปิดใช้งานการตั้งค่าเว็บไซต์

  • สร้าง symbolic link ไปยัง sites-enabled เพื่อเปิดใช้งานเว็บไซต์
เปิดใช้งานการตั้งค่าเว็บไซต์
sudo ln -s /etc/nginx/sites-available/certbot.pargorn.com /etc/nginx/sites-enabled/

8. ทดสอบการตั้งค่าของ Nginx

ทดสอบการตั้งค่า
sudo nginx -t

nginx test

9. หากไม่มีข้อผิดพลาด ให้รีสตาร์ท Nginx

รีสตาร์ท Nginx
sudo systemctl reload nginx

10. ตรวจสอบว่าเว็บไซต์ “Hello Certbot” ทำงานอยู่หรือไม่

  • เปิดเว็บเบราว์เซอร์และเข้าไปที่ http://certbot.pargorn.com

ขั้นตอนที่ 4 ติดตั้ง Certbot

  • Certbot เป็นเครื่องมือที่ช่วยให้การขอและต่ออายุ SSL Certificates จาก Let’s Encrypt เป็นเรื่องง่าย ในการติดตั้ง Certbot สำหรับ Nginx บน Ubuntu
ติดตั้ง Certbot
sudo apt install certbot python3-certbot-nginx -y

ขั้นตอนที่ 5 ขอรับ SSL Certificate ด้วย Certbot

  • หลังจากติดตั้งเสร็จสิ้น สามารถใช้ Certbot ในการขอรับ SSL Certificate ได้โดยใช้คำสั่ง
ขอรับ SSL Certificate
sudo certbot --nginx -d certbot.pargorn.com

Certbot จะถามข้อมูลต่างๆ ดังนี้

  • อีเมลสำหรับการแจ้งเตือน ใส่อีเมลที่สามารถติดต่อได้เพื่อรับการแจ้งเตือนเกี่ยวกับ SSL Certificate
  • ยอมรับข้อตกลงการให้บริการ
  • ยอมรับการแชร์อีเมลกับ Electronic Frontier Foundation (EFF) หากต้องการรับข่าวสารเกี่ยวกับการเข้ารหัสเว็บ ข่าวสารจาก EFF และวิธีการสนับสนุนเสรีภาพดิจิทัล

ตัวอย่าง

ตัวอย่างการขอรับ SSL Certificate
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): [email protected]
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. You must agree in
order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Account registered.
Requesting a certificate for certbot.pargorn.com
 
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/certbot.pargorn.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/certbot.pargorn.com/privkey.pem
This certificate expires on 2025-04-05.
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 certbot.pargorn.com to /etc/nginx/sites-enabled/certbot.pargorn.com
Congratulations! You have successfully enabled HTTPS on https://certbot.pargorn.com
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

ขั้นตอนที่ 6 ตรวจสอบการตั้งค่า Nginx

  • Certbot จะปรับแต่งไฟล์การตั้งค่า Nginx ให้รองรับ HTTPS โดยอัตโนมัติ แต่คุณสามารถตรวจสอบได้ที่ไฟล์การตั้งค่าใน /etc/nginx/sites-available/certbot.pargorn.com
certbot.pargorn.com
server {
    listen 80;
    listen [::]:80;
    server_name certbot.pargorn.com;
    return 301 https://$host$request_uri;
}
 
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name certbot.pargorn.com;
 
    ssl_certificate /etc/letsencrypt/live/certbot.pargorn.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/certbot.pargorn.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
 
    root /var/www/certbot.pargorn.com/html;
    index index.html index.htm;
 
    location / {
        try_files $uri $uri/ =404;
    }
}
  • หลังจากตรวจสอบแล้ว ให้ทดสอบการตั้งค่าของ Nginx
ทดสอบการตั้งค่า
sudo nginx -t
  • ถ้าการตั้งค่าถูกต้อง ให้รีสตาร์ท Nginx
รีสตาร์ท Nginx
sudo systemctl reload nginx

ขั้นตอนที่ 7 ทดสอบเว็บไซต์ด้วย HTTPS

เปิดเว็บเบราว์เซอร์และเข้าไปที่ https://certbot.pargorn.com คุณควรเห็น ไอคอนรูปกุญแจล็อก 🔒 ที่แสดงถึงการเชื่อมต่อที่ปลอดภัย พร้อมทั้งหน้าเว็บที่แสดงข้อความ “Hello Certbot” หากไม่มีข้อผิดพลาดใดๆ เกิดขึ้น แสดงว่าการตั้งค่า HTTPS สำเร็จเรียบร้อยแล้ว

hello certbot https

ขั้นตอนที่ 8 ตั้งค่าการต่ออายุอัตโนมัติสำหรับ SSL Certificate

  • SSL Certificates จาก Let’s Encrypt มีอายุ 90 วัน คุณสามารถตั้งค่าให้ Certbot ต่ออายุอัตโนมัติได้ด้วยการเพิ่ม Cron Job หรือใช้ systemd timer ที่มาพร้อมกับ Certbot

  • ตรวจสอบให้แน่ใจว่า Cron Job สำหรับ Certbot มีอยู่

ตรวจสอบ Cron Job
sudo systemctl list-timers | grep certbot

certbot timer

  • โดยปกติ Certbot จะตั้งค่าให้ทำการตรวจสอบและต่ออายุอัตโนมัติทุกวัน หากต้องการตรวจสอบการต่ออายุสามารถทำได้ด้วยคำสั่ง
  • ถ้าไม่มีข้อผิดพลาด แสดงว่าการตั้งค่าการต่ออายุอัตโนมัติทำงานได้ถูกต้อง
ตรวจสอบการต่ออายุ
sudo certbot renew --dry-run

สรุป

การติดตั้งและกำหนดค่า HTTPS บน Nginx ด้วย Certbot บน Ubuntu Server เป็นกระบวนการที่ไม่ซับซ้อนและสามารถทำได้ในไม่กี่ขั้นตอน การใช้ HTTPS จะช่วยเพิ่มความปลอดภัยให้กับเว็บไซต์ของคุณและสร้างความเชื่อมั่นให้กับผู้ใช้งาน ในบทความนี้เราได้สร้างเว็บไซต์ที่แสดงข้อความ “Hello Certbot” และใช้โดเมน certbot.pargorn.com เพื่อแสดงให้เห็นถึงการตั้งค่าอย่างครบถ้วน อย่าลืมตรวจสอบและต่ออายุ SSL Certificates อย่างสม่ำเสมอเพื่อรักษาความปลอดภัยของเว็บไซต์อย่างต่อเนื่อง

อ้างอิง

วิดีโอ

Buy Me A Coffee