1, 安装 uwsgi
pip install uwsgi
2, 测试 uwsgi
新建测试文件
$ vim test.py # test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return ["Hello World"]
启动uwsgi,并测试
$ uwsgi --http :8000 --wsgi-file test.py $ curl -i http://localhost:8000
3, 通过 uwsgi 来启动django
# 原始启动方式,通过manage.py $ python manage.py runserver 0.0.0.0:8000 # 使用uwsgi $ uwsgi --http :8000 --chdir /usr/share/nginx/html/django-blog --wsgi-file /usr/share/nginx/html/django-blog/mysite/wsgi.py # 测试 $ curl -i http://localhost:8000
4,利用Nginx转发请求,uwsgi执行python
配置Nginx文件
# mysite_nginx.conf # the upstream component nginx needs to connect to upstream django { server 127.0.0.1:8000; # 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 django.luckybird.me; # substitute your machine's IP address or FQDN charset utf-8; access_log /var/log/nginx/dj-access.log main; error_log /var/log/nginx/dj-error.log; # max upload size client_max_body_size 75M; # adjust to taste location /static { alias /usr/share/nginx/html/django-blog/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed } }
重启Nginx
5,通过 uwsgi 来启动django,并请求Nginx端口
# 使用uwsgi $ uwsgi --http :8000 --chdir /usr/share/nginx/html/django-blog --wsgi-file /usr/share/nginx/html/django-blog/mysite/wsgi.py # 测试80端口 $ curl -i http://localhost
至此,将Nginx、uwsgi、django整合在一起了
uwsgi还可以采用socket方式启动
1,添加socket配置文件
# mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /usr/share/nginx/html/django-blog # Django's wsgi file wsgi-file = /usr/share/nginx/html/django-blog/mysite/wsgi.py # the virtualenv (full path) home = /usr # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe socket = /usr/share/nginx/html/django-blog/mysite/mysite.sock # ... with appropriate permissions - may be needed chmod-socket = 666 # clear environment on exit vacuum = true
2,通过socket方式启动uwsgi
# 测试文件 $ uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # django项目 $ uwsgi --socket mysite.sock --chdir /usr/share/nginx/html/django-blog --wsgi-file /usr/share/nginx/html/django-blog/mysite/wsgi.py --chmod-socket=666
3,修改Nginx配置文件,然后重启
upstream django { server unix:///usr/share/nginx/html/django-blog/mysite/mysite.sock; # for a file socket #server 127.0.0.1:8000; # for a web port socket (we'll use this first) }
4,测试
# 测试80端口 $ curl -i http://localhost
遇到问题
uwsgi: invalid option -- 'x'
原因:centos下,在没有安装libxml2时,pip安装的uwsgi无法支持xml格式的配置文件
解决:
yum install libxml* pip uninstall uwsgi pip install uwsgi
ImportError: Import by filename is not supported.
原因:使用 –module 参数,找不到wsgi module,或者指向 –wsgi-file 文件 wsgi.py
方法:
--wsgi-file wsgi.py
Leave a Reply