flask+uwsgi+nginx项目部署


服务器部署通常是用nginx作负载均衡,用uwsgi转发到相应的web项目中去php

安装 nginx(ubuntu和centos有区别)css

sudo apt-get install nginx


安装uwsgihtml

pip3 install uwsgi


#将数据库迁移到服务器上mysql

安装服务器nginx

sudo apt-get install mysql-server (ubuntu版)


#打开数据库,建立一个数据库web

create database_shop


导入本地建立的数据库文件sql

source (文件地址)


配置uwsgi.ini文件,这个文件跟uwsgi同行数据库

[uwsgi]
#使用nginx链接时使用,Django程序所在服务器地址
;socket=127.0.0.1:8001
#直接作web服务器使用,Django程序所在服务器地址
http=0.0.0.0:8001
#项目目录(manage.py所在目录)
chdir=/home/ubuntu/apiwxcjsoft/apicjsoft
#项目中wsgi.py文件的目录,相对于项目目录(写项目目录chdir以后的目录)
wsgi-file=apicjsoft/wsgi.pycallable = app #flask_manager 须要加上这句话
# 进程数
processes=4
# 线程数
threads=2
# uwsgi服务器的角色
master=True
# 存放进程编号的文件
pidfile=uwsgi.pid
# 日志文件,由于uwsgi能够脱离终端在后台运行,日志看不见。咱们之前的runserver是依赖终端的(会生成在与uwsgi.ini平级目录中)
daemonize=uwsgi.log
# 指定依赖的虚拟环境
virtualenv=/home/ubuntu/apiwxcjsoft/apicjsoft/env


启动uwsgi命令flask

uwsgi --ini uwsgi.ini


关闭uwsgi命令ubuntu

uwsgi --stop uwsgi.pid 关闭 


配置nginx

ubuntu下找到/etc/nginx/sites-available下的default文件

server { listen 80; #监听端口,通常为80# listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html;
server_name _;#配置转发地址, location / { include /etc/nginx/uwsgi_params; uwsgi_pass 127.0.0.1:8001;#写在uwsgi.ini中得地址 # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # try_files $uri $uri/ =404; }

修改完后从新启动nginx

service nginx restart


中止命令

service nginx stop


启动命令

service nginx start

本文分享自微信公众号 - 会呼吸的Coder(BreathCoder)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。