一、Uwsgi安装
python3 -m pip install uwsgi
cp /usr/local/python3/bin/uwsgi /usr/bin/
测试
在django项目主目录下create test.py
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
运行
uwsgi --http :8000 --wsgi-file test.py
访问localhost:8000能返回Hello World说明uwsgi安装成功
二、uwsgi启动django
django主目录下create wsgi.py
python3
#coding:utf-8 import os import sys import importlib importlib.reload(sys) from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cmdb.settings") #project为项目名,请按需求修改 application = get_wsgi_application()
和manage.py同级目录create project.ini
[uwsgi] http = :9999 #the local unix socket file than commnuincate to Nginx #socket = 127.0.0.1:8001 # the base directory (full path) chdir = /export/Packages/cmdb # Django's wsgi file wsgi-file = /export/Packages/cmdb/wsgi.py # maximum number of worker processes processes = 8 #thread numbers startched in each worker process threads = 4 #monitor uwsgi status stats = 127.0.0.1:9191 # clear environment on exit vacuum = true
启动
uwsgi project.ini
三、nginx安装
settings.py中加上
STATIC_ROOT = os.path.join(BASE_DIR, "static/") #执行 python manage.py collectstatic
生成一个目录存静态文件,下面nginx的配置文件中/static下改成生成静态文件的路径
nginx.conf
upstream cmdb.sdops.com{ server 127.0.0.1:9999 weight=10 max_fails=2 fail_timeout=30s; } server { listen 80; server_name cmdb.sdops.com; access_log /export/servers/nginx/logs/cmdb/access.log main; error_log /export/servers/nginx/logs/cmdb/error.log warn; # chunkin on; error_page 411 = @my_error; location @my_error { # chunkin_resume; } location / { proxy_pass http://cmdb.sdops.com; expires 0; } location /static { alias /export/Packages/cmdb/static;#静态文件位置 } location /logs/ { autoindex off; deny all; } }
四、运行
uwsgi project.ini
这样django就能高并发啦