基于ubuntu 16.04系统,使用 Gunicorn + Nginx 进行布署




ssh 用户名@ip地址
以下操作都在远程服务器上进行操作 (ubuntu 16.04)
sudo apt-get update
apt-get install mysql-server
apt-get install libmysqlclient-dev
sudo apt-get install redis-server
pip install virtualenv
pip install virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc
Python 项目中可以包含一个 requirements.txt 文件,用于记录所有依赖包及其精确的版本号,以便在新环境中进行部署操作。
pip freeze > requirements.txt
pip install -r requirements.txt
sudo apt-get build-dep python-mysqldb
$ sudo apt-get install nginx
/etc/init.d/nginx start #启动
/etc/init.d/nginx stop #停止
启动失败大概率是因为配置文件错误
# 如果是多台服务器的话,则在此配置,并修改 location 节点下面的 proxy_pass
upstream flask {
server 127.0.0.1:5000;
server 127.0.0.1:5001;
}
server {
# 监听80端口
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# 请求转发到gunicorn服务器
proxy_pass http://127.0.0.1:5000;
# 请求转发到多个gunicorn服务器
# proxy_pass http://flask;
# 设置请求头,并将头信息传递给服务器端
proxy_set_header Host $host;
# 设置请求头,传递原始请求ip给 gunicorn 服务器
proxy_set_header X-Real-IP $remote_addr;
}
}
pip install gunicorn
gunicorn -h
# -w: 表示进程(worker) -b:表示绑定ip地址和端口号(bind)
gunicorn -w 2 -b 127.0.0.1:5000 运行文件名称(注意只是名称 没有.py的后缀):Flask程序实例名
启动失败大概率是因为解释器或者环境问题
参考阅读: Gunicorn相关配置:https://blog.csdn.net/y472360651/article/details/78538188
scp -r 本地文件路径 root@39.106.21.198:远程保存路径
数据库模板文件迁移
原文:https://www.cnblogs.com/xujin247/p/11876876.html