uwsgi+django
-
创建新的虚拟环境,且解决crm的环境依赖
-
在虚拟环境下安装uwsgi
pip3 install uwsgi -
学习uwsgi命令,如何启动python应用 启动python web文件 创建一个test.py写入如下代码
1def application(env, start_response): 2 start_response('200 OK', [('Content-Type','text/html')]) 3 return [b"Hello World"] # python3用uwsgi启动一个python web文件
指定8000端口启动 http服务
指定wsgi文件
uwsgi --http :8000 --wsgi-file test.py -
用uwsgi启动django项目 uwsgi --http :9000 --module Alibab_crm.wsgi
uwsgi加上热加载命令 uwsgi --http :8000 --module Alibab_crm.wsgi --py-autoreload=1
使用uwsgi配置文件去启动项目
-
手动创建uwsgi.ini 配置文件 (alicrm) [root@s16ds Alibab_crm]# cat uwsgi.ini
1# mysite_uwsgi.ini file 2[uwsgi] 3# Django-related settings 4# the base directory (full path) 5#指定django的项目目录,第一层 6chdir = /opt/Alibab_crm 7# Django's wsgi file 8#找到django的wsgi文件 9#这里需要写项目的第二层目录Alibab_crm 10module = Alibab_crm.wsgi 11# the virtualenv (full path) 12#填写虚拟环境的绝对路径 13home = /root/Envs/alicrm 14# process-related settings 15# master 16master = true 17# maximum number of worker processes 18processes = 5 19# the socket (use the full path to be safe 20#指定socket协议,运行django,只能与nginx结合时使用 21#指定socket协议,运行django,只能与nginx结合时使用 22#指定socket协议,运行django,只能与nginx结合时使用 23socket = 0.0.0.0:8000 24#如果你没用nginx,只想自己启动一个http界面,用这个 25#http = 0.0.0.0:8000 26 27# ... with appropriate permissions - may be needed 28# chmod-socket = 664 29# clear environment on exit 30vacuum = true -
通过配置文件启动uwsgi
uwsgi --ini uwsgi.ini
-
-
收集django crm的静态文件
编辑crm的settings.py配置文件 写入如下代码
1# 定义django的静态资源根目录,便于用命令收集资源,存放的地儿 2STATIC_ROOT="/opt/crm_static" 3STATIC_URL = '/static/' 4STATICFILES_DIRS = [ 5 os.path.join(BASE_DIR, 'static') 6 7] 8用命令收集静态文件
python3 manage.py collectstatic -
配置nginx,反响代理django服务器,且解析静态文件
proxy_pass 仅仅是请求转发的参数,与uwsgi结合,还有跟高级的协议参数
修改nginx配置文件如下
1server { 2 listen 80; 3 server_name s16chiji.com; 4 location / { 5 #root /opt/s16chiji; 6 #index index.html; 7 #使用uwsgi_pass 转发基于uwsgi协议的一个请求 8 9 uwsgi_pass 192.168.15.71:8000; 10 include /opt/nginx112/conf/uwsgi_params; 11 } 12 #配置一个url的入口,告诉django静态文件在哪里去找 13 #当请求url是 s16chiji.com/static/的时候 14 #就进行别名,nginx去/opt/crm_static下寻找js文件 15 location /static { 16 alias /opt/crm_static/; 17 } 18 #通过这个参数,定义错误页面的文件 ,当状态码是 404 400 401 时,返回40x.html页面 19 error_page 404 401 400 403 /40x.html; 20 } -
此时nginx结合uwsgi 已经完成
-
记住这里推出虚拟环境,使用物理环境去运行
配置supervisor工具,管理django后台
- 这个东西只能用python2去实现
-
下载supervisor
easy_install supervisor -
配置supervisor的配置文件,编写django任务
echo_supervisord_conf > /etc/supervisor.conf -
编写运行django的任务
vim /etc/supervisor.conf 在最底行写入如下代码
1[program:s16alicrm] 2command=/root/Envs/alicrm/bin/uwsgi --ini /opt/Alibab_crm/uwsgi.ini 3autorestart=true 4stopasgroup=true 5killasgroup=true -
启动suopersivod这个程序
1启动服务端 2supervisord -c /etc/supervisor.conf 3通过客户端命令查看任务 4supervisorctl -c /etc/supervisor.conf -
学习supervisor管理命令
1[root@s16ds alicrm]# supervisorctl -c /etc/supervisor.conf 2s16alicrm RUNNING pid 5293, uptime 0:03:03 3supervisor> stop all #停止所有任务 4supervisor> start all #启动s所有任务 5supervisor> status s16alicrm