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证书。