Uwsgi部署flask&Django项目
我的部署环境是在ubuntu中,直接安装了Anaconda3,我自己使用的是flask
1.下载并配置Anaconda
可以参照我之前的配置mac下的anaconda,这是配置Anaconda链接
2.创建一个属于你的服务的虚拟环境
- 创建并激活新的虚拟环境
conda create -n 环境名称 python=3 在python3下创建一个新的虚拟环境
conda activate 环境名称
- 在进入新的环境之后,根据需要安装所需要的依赖包
pip3 install uwsgi
....
在这里说一下,如果pip安装一直报错的话可以conda install uwsgi
conda安装完之后,pip list查看有没有安装成功
3. 添加uwsgi_params文件
文件内容如下
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
4.配置uwsgi.ini配置文件
在项目的目录下新建一个名为uwsgi.ini的文件
Flask配置文件
[uwsgi]
# socket=只能结合nginx访问(如果直接http无法访问)
# http:只能http访问(如果使用nginx会无法访问)
socket=/你的项目中用来存放uwsgi.sock文件的目录/uwsgi.sock
virtualenv =/home/x x x x x x/anaconda3/envs/你的项目虚拟环境名称
#你的项目所依赖的虚拟环境目录
chdir=/home/你的项目所在目录
wsgi-file=manage.py
#flask的文件是manage.py
callable=app
processes=16
threads=500
#process和thread数量需要根据服务器配置来写,具体换算方式我就不做赘述,
master=True
chmod-socket = 777
vacuum = true
python-autoreload = 1
pidfile=/home/你的项目中用来存放uwsgi.pid文件的目录/uwsgi.pid
daemonize=/home/你的项目中用来存放uwsgi.log文件的目录/uwsgi.log
#log文件来记录服务的日志
Django配置文件
[uwsgi]
socket=/home/你的项目中用来存放uwsgi.sock文件的目录/uwsgi.sock
#http=:8000
pidfile = /home/你的项目中用来存放uwsgi.pid文件的目录/uwsgi.pid
# the base directory (full path)
chdir = /home/你的项目所在目录/
# Django s wsgi file
module = 你的django项目目录.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
# ... with appropriate permissions - may be needed
chmod-socket = 777
#代码修改后自动重启
python-autoreload = 1
sttus=%(chdir)/uwsgi/uwsgi.status # status文件,可以查看uwsgi的运行状态
daemonize=%(chdir)/uwsgi/uwsgi.log
配置完以后在项目目录下执行下面的命令
uwsgi uwsgi.ini
#通过.ini文件启动uwsgi
相关的uwsgi指令
# 停止
uwsgi --stop uwsgi.pid
# 重启
uwsgi --reload uwsgi.pid
5.配置nginx
默认你安装了nginx,如果没有请自行安装
- 在你安装的nginx的目录下的site-enabled目录里新建.conf文件
#tream component nginx needs to connect to
upstream 根据你类型填写,比如flask,须和后面uwsgi_pass一致 {
# server unix:///path/to/your/xxxx/uwsgi.sock; # for a file socket
server unix:///你的项目存放.sock文件的目录/uwsgi.sock; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name x.x.x.x此处填写你的服务ip地址; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media 如果是Django项目则配置,flask不需要
location /media {
alias /home/项目目录/media; # your Django project's media files - amend as required
}
location /static {
alias /home/项目目录/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass flask;
include /home/你的项目存放params文件目录/uwsgi_params; # the uwsgi_params file you installed
}
}
完成之后重启nginx
cd /usr/local/nginx/sbin#你的nginx文件目录
./nginx -s reload
到此,打完收工!🤣🤣🤣