CentOS上部署Python應用可參考以下步驟,以Web應用為例:
sudo yum install python3 python3-pip nginx # 安裝Python、pip和Nginx
python3 -m venv venv # 創建虛擬環境
source venv/bin/activate # 激活環境
pip install -r requirements.txt # 安裝項目依賴
gunicorn -w 4 -b 127.0.0.1:8000 app:app # 啟動Gunicorn,監聽本地8000端口
/etc/nginx/conf.d/myapp.conf
:server {
listen 80;
server_name your_domain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
重啟Nginx:sudo systemctl restart nginx
sudo firewall-cmd --add-service=http --permanent # 允許HTTP流量
sudo firewall-cmd --reload
systemd
配置服務開機自啟(參考)。說明:
certbot
申請免費SSL證書。