Deploy Jupyterhub Server with Let's Encrypt SSL

Deploy Jupyterhub Server with Let's Encrypt SSL
Photo by Glenn Carstens-Peters / Unsplash

Requirements:

  • Ubuntu server 18.04+.
  • Tên miền (domain name).

Certbot + Let's Encrypt

Để cài đặt Certbot, ta cần cài đặt snap package management cho Ubuntu:

sudo snap install core; sudo snap refresh core

Sau đó là sử dụng snap để cài đặt certbot:

sudo apt remove certbot
sudo snap install --classic certbot

Để chạy lệnh certbot, ta phải link cerbot executable file vào system path:

sudo ln -s /snap/bin/certbot /usr/bin/certbot

Lấy SSL certificate từ Let's encrypt, lưu ý thay thế <domain_name> bằng tên miền của bạn:

sudo ufw allow 443
Output

Rule added
Rule added (v6)
sudo certbot certonly --standalone -d <domain_name>
Output

IMPORTANT NOTES:
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/<domain_name>/fullchain.pem
Key is saved at: /etc/letsencrypt/live/<domain_name>/privkey.pem
This certificate expires on 2022-02-10.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

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

Tại bước này, chúng ta cần chú ý nơi lưu các file chứa private key và certkey /etc/letsencrypt/live/<domain_name>/

sudo ls /etc/letsencrypt/live/<domain_name>
Output

cert.pem  chain.pem  fullchain.pem  privkey.pem  README

Bởi vì Let's Encrypt yêu cầu phải renew SSL certificate mỗi 3 tháng nên ta phải làm lại các công đoạn trên mỗi khi SSL certificate hết hạn. Ngoài ra còn một cách khác đó là sử dụng tính năng gia hạn tự động của certbot. Để làm được điều đó, chỉ cần thêm dòng lệnh sau vào file etc/letsencrypt/renewal/<domain_name>.conf

renew_hook = systemctl reload <your_service>

Trong đó, lệnh systemctl reload <your_service> chỉ là một lệnh ví dụ, phụ thuộc vào ứng dụng của server cần cài đặt những lệnh chạy khác nhau.

Kiểm thử chức năng‌ tự động gia hạn SSL:

sudo certbot renew --dry-run
How To Use Certbot Standalone Mode to Retrieve Let’s Encrypt SSL Certificates on Ubuntu 20.04 | DigitalOcean
Certbot offers a variety of ways to validate your domain, fetch certificates, and automatically configure Apache and Nginx. In this tutorial, we’ll discuss C…

Jupyterhub

Để cài đặt Jupyterhub trước hết cần cài đặt NodeJS và NPM mới nhất:

sudo apt-get install nodejs npm

Vì Jupyterhub là một ứng dụng của Python nên ta có thể cài đặt thông qua PyPI:

python3 -m pip install jupyterhub jupyterlab

Để khởi chạy Jupyterhub server với SSL certificate chỉ cần chạy lệnh sau:

jupyterhub --ip 0.0.0.0 --port 443 --ssl-key /etc/letsencrypt/live/<domain_name>/privkey.pem --ssl-cert /etc/letsencrypt/live/<domain_name>/cert.pem

Giờ mọi bước đã xong, chỉ cần nhập domain của bạn vào browser là có thể truy cập jupyterhub. Vì mặc định jupyterhub sử dụng PAM authentication nên để đăng nhập chỉ cần sử dụng các tài khoản của local system trên máy server.