There are many ways to deploy Django project. Nginx and uWSGI is one of them.
Nginx deals with static files. uWSGI transfers no-static file request from Nginx to Django.
Note that you can only use uWSGI + Django for deployment.
I assume you already installed Django, Nginx and uWSGI.
(1) Test uWSGI.
Write a simple test.py first
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"Then run command
uwsgi --http :8001 --wsgi-file test.pyThen you can check if http://127.0.0.1:8001 is working
(2) Test Django
run command
python manage.py runserver 0.0.0.0:8002
Then you can check if it is working.
(3) Write Django uwsgi file
write Django_wsgi.py file which is at the same directory with manage.py in Django project
#!/usr/bin/env python # coding: utf-8 import os import sysNote that change "mysite" to your site name.reload(sys)sys.setdefaultencoding('utf8')os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")from django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()
(4) Deploy with Django + uwsgi
Assume your Django project path is "/root/WWW/mysite"
uwsgi --http :8000 --chdir /root\WWW/mysite --module django_wsgi
(5) Write Django xml file
write a file as Django_socket.xml and put it at the same directory as manage.py
<uwsgi> <socket>:8077</socket> <chdir>/root/WWW/mysite</chdir> <module>django_wsgi</module> <processes>4</processes> <!-- 进程数 --> <daemonize>uwsgi.log</daemonize> </uwsgi>
(6) Config Nginx
Config Nginx configuration file "/etc/nginx/nginx.conf".
Add following server must under http directive in the file
server { listen 80; server_name www.you.com; access_log /root/WWW/mysite/access.log; error_log /root/WWW/mysite/error.log; #charset koi8-r; #access_log logs/host.access.log main; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8077; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location /static/ { alias /root/WWW/mysite/static/; index index.html index.htm; } location /media/ { alias /root/WWW/mysite/media/; } }
(7) Start Ngnix and uWSGI
Run the following commands:
(7.1) start ngnix
service ngnix start
(7.2) start uWSGI
cd /root/WWW/mysite
uwsgi -x django_socket.xml
(8) Notes
(8.1)
If Ngnix has 403 error, modify ngnix.conf file
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
change/uncomment the first line touser root
here, root the account name
(8.2) setting,xml in Django project
change as follows.
DEBUG = False
ALLOWED_HOSTS = [
'.example.com', # Allow domain and subdomains
'.example.com.', # Also allow FQDN and subdomains
]
Reference:
[1] check uWSGI version : uwsgi --version
[2] check Nginx version: nginx -v
[3] http://django-china.cn/topic/101/
[4] http://django-china.cn/topic/124/
[5] http://stackoverflow.com/questions/20321673/debugging-apache-django-wsgi-bad-request-400-error
Comments
Post a Comment