怎么用flask+uwsgi+nginx把项目部署到服务器上

怎么用flask+uwsgi+nginx把项目部署到服务器上

本篇内容主要讲解“怎么用flask+uwsgi+nginx把项目部署到服务器上”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用flask+uwsgi+nginx把项目部署到服务器上”吧!

服务器部署一般是用nginx做负载均衡,用uwsgi转发到相应的web项目中去

安装 nginx(ubuntu和centos有区别)


sudo apt-get install nginx

安装uwsgi


pip3 install uwsgi

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

安装服务器


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

#打开数据库,创建一个数据库


create database_shop

导入本地创建的数据库文件


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命令


uwsgi --ini uwsgi.ini

关闭uwsgi命令


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

到此,相信大家对“怎么用flask+uwsgi+nginx把项目部署到服务器上”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!