零、所用环境
Ubuntu,Python3
一、uWSGI
在虚拟环境中使用
1. 安装uwsgi
apt-get install build-essential python-dev
apt-get install build-essential python3-dev
pip install uwsgi
2. 通过uwsgi启动项目
uwsgi --http 0.0.0.0:端口号 -s /tmp/uwsgi.sock --module /project_path --callable app
3. 编写uwsgi.ini
"""
[uwsgi]
chdir = /project_path
module = project_name
home = 虚拟环境路径(cdvirtualenv)
master = true
processes = number
socket = /tmp/uwsgi.sock
wsgi-file = /project_path/project_name.py
callable = app
die-on-term = false
"""
4. 通过uwsgi.ini启动项目
uwsgi --http 0.0.0.0:端口号 --ini /project_path/uwsgi.ini
5. 杀端口
sudo fuser -k 端口号/tcp
二、Nginx
系统环境下使用
1. 安装Nginx
sudo apt-get install nginx
2. 修改配置文件
方式一:sudo vim /etc/nginx/sites-enabled/default
方式二:sudo vim /usr/local/nginx/sites-enabled/default
方式三:sudo vim /usr/local/nginx/conf/nginx.conf
"""
server {
# http80, https443, others is also ok
listen 443;
server_name domain;
charset utf-8;
client_max_body_size 75M;
access_log /project_path/project_name.access.log;
error_log /project_path/project_name.error.log;
# https(tencent)
ssl on;
ssl_certificate 1_yinxiang0.com_bundle.crt;
ssl_certificate_key 2_yinxiang0.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
# # redirect http to https
# rewrite ^(.*) https://$host$1 permanent;
location / {
# uwsgi
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;
# # gunicorn
# proxy_pass http://127.0.0.1:8000;
# proxy_redirect off;
# proxy_set_header Host $host:443;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# location /static/ {
# alias /project_path/static/;
# }
}
"""
备注
腾讯云https参照https://cloud.tencent.com/document/product/400/4143#2.-nginx-.E8.AF.81.E4.B9.A6.E9.83.A8.E7.BD.B2
阿里云https参照https://help.aliyun.com/knowledge_detail/95491.html?spm=5176.2020520154.cas.25.3a70WhTkWhTkVv
3. 检查nginx配置文件是否有错
方式一:sudo nginx -t
方式二:sudo /usr/local/nginx/sbin/nginx -t
备注:which nginx找到nginx路径,再执行-t操作。
成功会出现类似提示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4. 重启nginx
1. sudo service nginx restart
2. sudo nginx -s reload
2. sudo /usr/local/nginx/sbin/nginx -s reload
5. 查看nginx状态
service nginx status
进入浏览器访问域名查看网页是否正常启动。
三、Supervisor
系统环境及Python2中使用
1. 安装
pip install supervisor
2. 编写supervisor.conf
备注:多个进程,写多个[program:project_name]即可
"""
[program:project_name]
command = [which uwsgi] --ini /project_path/uwsgi.ini
directory = project_path
startsecs = 5
stopwaitsecs = 5
autostart = true
autorestart = true
stdout_logfile = /project_path/log/supervisord.log
stderr_logfile = /project_path/log/supervisorderr.log
[supervisord]
loglevel = info
[supervisorctl]
serverurl = http://0.0.0.0:端口号
username = username
password = password
[inet_http_server]
port = :端口
username = username
password = password
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
"""
3. 启动supervisor
supervisord -c /project_path/supervisor.conf
supervisord -c supervisor.conf
备注:可在公网IP:端口号进行管理
查看进程ID
ps aux|grep supervisor
杀进程
kill -9 进程号
4. 终端进入supervisor管理端
supervisorctl -c /home/ubuntu/srv/bbs_demo/supervisor.conf
* status # 查看状态
* start program_name #启动程序
* restart program_name #重新启动程序
* stop program_name # 关闭程序
* reload # 重新加载配置文件
* quit # 退出控制台
四、Gunicorn
虚拟环境下使用
1. 安装gunicorn和gevent
pip install gunicorn
pip install gevent
2. 编写gunicorn.conf
"""
import os
bind = '0.0.0.0:8000' #绑定的ip及端口号
workers = 3 #进程数
backlog = 2048 #监听队列
worker_class = "gevent" #默认sync模式
debug = False
chdir = '/project_path'
proc_name = 'gunicorn.proc'
"""
3. 通过gunicorn启动项目
sync模式
gunicorn -c /project_path/gunicorn.conf project_name:app/api
gevent模式
gunicorn -k gevent -c /project_path/gunicorn.conf project_name:app/api
4. 已知存在的问题
a. 不支持Flask的CSRF保护;
b. 登录过一段时间会失效。