安装
安装 Gunicorn
pip install gunicorn
安装 Nginx
sudo apt install nginx
配置 Nginx
打开 Nginx 配置文件:
sudo vim /etc/nginx/sites-available/default
修改该文件中关于 location 的内容:
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
proxy_pass http://127.0.0.1:5000; # 此处使用 5000 端口
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static {
alias /项目路径/static;
}
启动
先启动 Gunicorn :
nohup gunicorn -w 2 -b :5000 manage:app &
-w设置进程数量,此处使用2个进程。-b指定端口,需与上一步配置中的端口一致,此处使用5000端口。nohup使终端关闭时程序不被挂断。&使程序在后台运行。
接下来启动 Nginx :
sudo /usr/sbin/nginx
至此网站部署完成,访问 ip 或 ip:80 可以打开网站。
可能遇到的问题
Gunicorn 提示权限不足
如果程序中需要读写用户创建的文件,启动 Gunicorn 时不要使用 sudo 。
网页可以打开但无法加载静态资源
在配置 Nginx 时,需要配置
location /static {
alias /项目路径/static;
}
本文指导如何安装Gunicorn和Nginx,配置它们来部署网站,并详细解释了权限不足和静态资源加载问题。通过步骤教学,帮助读者避免常见部署难题。
298

被折叠的 条评论
为什么被折叠?



