Bình thường khi viết xong app Python Flask thì chúng ta thường chạy bằng lệnh:
python3 myproject.pyGiờ, chúng ta sẽ chạy qua uWSGI và Nginx sử dụng SSL cho nó...chuẩn nhé!
Phần I: Chạy web với HTTP thuần
Step 1: Cài các gói cần thiết và chạy thử Python Flask App
Đầu tiên, cần cài các gói cần thiết:
yum install epel-releaseNếu dùng centos8/Rocky Linux thì có thể cài qua lệnh:
dnf install epel-release
yum install python-pip python-devel gcc nginxTạo Python Virtual Environment:
pip install virtualenv
mkdir ~/myproject
cd ~/myproject
virtualenv myprojectenv
source myprojectenv/bin/activateCài đặt Flask và uWSGI:
pip install uwsgi flaskTạo cái app mẫu nào:
vim ~/myproject/myproject.pyNội dung:
from flask import Flask
application = Flask(__name__)
@application.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
application.run(host='0.0.0.0', port=5001)Chạy test:
python3 myproject.pyTruy cập thử web với port:5001 nếu ra "Hello There!" là ok, chưa ok thì xem mở port trong firewalld...
Nếu chưa mở port thì mở như sau:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reloadTạo WSGI Entry Point:
vim ~/myproject/wsgi.pyfrom myproject import application
if __name__ == "__main__":
application.run()Chạy thử uWSGI Serving:
uwsgi --socket 0.0.0.0:5001 --protocol=http -w wsgiTruy cập thử web với port:5001 nếu ra "Hello There!" là ok.
Xong, sứ mệnh của virtual environment đã kết thúc, tắt nó:
deactivateStep 2: Tạo các cấu hình chạy dịch vụ cho uWSGI
- Tạo file cấu hình cho uWSGI:
vim ~/myproject/myproject.iniNội dung:
[uwsgi]
module = wsgi
master = true
processes = 5
socket = myproject.sock
chmod-socket = 660
vacuum = true
die-on-term = true
Tạo file chạy service:
vim /etc/systemd/system/myproject.service
[Unit]
Description=uWSGI instance to serve myproject
After=network.target
[Service]
User=webuser
Group=nginx
WorkingDirectory=/home/user/myproject
Environment="PATH=/home/user/myproject/myprojectenv/bin"
ExecStart=/home/user/myproject/myprojectenv/bin/uwsgi --ini myproject.ini
[Install]
WantedBy=multi-user.target- Khởi chạy dịch vụ:
systemctl daemon-reload
sudo systemctl start myproject
sudo systemctl enable myprojectStep 3: Cấu hình Nginx Proxy
vim /etc/nginx/conf.d/lecuong.info.confNội dung:
server {
listen 80;
server_name lecuong.info;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/user/myproject/myproject.sock;
}
}Thêm webuser vào nhóm nginx
sudo usermod -a -G webuser nginx
chmod 710 /home/webuser # ko chay lenh nay khi truy cap web se ra loi: 502 Bad Gateway- Kiểm tra cấu hình Nginx
sudo nginx -tNếu nó không lòi ra lỗi gì, thì ngon lành rồi đó! Còn nếu có lỗi thì...tìm cách sửa đi :))
Khởi chạy dịch vụ Nginx:
sudo systemctl start nginx
sudo systemctl enable nginxRồi, xong rồi đó. Truy cập web ngon lành nha!
(*) phần này cần tham khảo chi tiết thì xem ở đây
Phần II: Chạy web với HTTPS cho nó bảo mật nha :))
Step 1: Cài đặt cerbot và Nginx plugin dùng cho cerbot
sudo dnf install certbot python3-certbot-nginx- Tiến hành tạo Cert:
sudo certbot --nginx -d lecuong.info -d www.lecuong.infoNếu chỉ cần tạo cert cho 1 dommain duy nhất thì gõ:sudo certbot --nginx -d your_domainNếu thành công thì certbot nó sẽ thông báo dạng "Congratulations! Your certificate and chain have been saved at:"
Còn nếu lỗi gì đó, thì debug từ từ giải quyết thôi :))
- Cấu hình tự động renew cert SSL (vì mặc định cert sẽ hết hạn sau 90 ngày.):
Test thử xem việc renew có ok ko?
certbot renew --dry-runNếu ok thì cài đặt job cho nó tự renew:
crontab -edán vô:
* */12 * * * root /usr/bin/certbot renew >/dev/null 2>&1Step 2: cấu hình SSL vô web
Mặc định certbot nó ghi sẵn vô /etc/nginx/nginx.conf rồi, nhưng tôi thích dùng file cấu hình riêng hơn!
vim /etc/nginx/conf.d/lecuong.info.conf server {
server_name lecuong.info;
location / {
include uwsgi_params;
uwsgi_pass unix:/home/user/myproject/myproject.sock;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/lecuong.info/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/lecuong.info/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}- Khởi động lại dịch vụ Flask web và Nginx
systemctl restart myproject
systemctl restart nginxNhiêu đó là xong rồi.
Từ giờ, chúng ta có thể truy cập web với https://
Cơm thêm: Nếu bạn nào dùng dịch vụ CloudFlare, và có bật Proxy thì cần đổi cấu hình thì mới chạy được!
Chia sẻ file cấu hình nè: ngix_proxy_behind_cloudflare.conf
Thêm nữa là nếu chạy test renew certbot lỗi thì nên thêm các page rule trên Cloudflare
# This is a Cloudflare issue as it's caching the response.
*yourdomain.com/.well-known/acme-challenge/*
Cache Level: Bypass, Automatic HTTPS Rewrites: Off, Disable Performance
Chúc các bạn may mắn. :))