首页 > 代码库 > Start Your Django Project in Nginx with uWsgi

Start Your Django Project in Nginx with uWsgi

Step 0:Install A,B,C,blabla needed

This can be seen in my another article in the blog.click here(unavailable now,just in the future)

 

Step 1:Create A Django Project

chdir /path/to/your/project/base/

django-admin.py startproject mysite

 

Step 2:Create Your uwsgi congfigure files and Edit them

chdir /path/to/your/project/base/mysite

touch mysite_uwsgi.conf mysite_uwsgi.py mysite_uwsgi.ini mysite_mysite_uwsgi.pid uwsgi.pid

sudo ln -s /path/to/your/project/base/mysite/mysite_uwsgi.conf /etc/nginx/sites-enabled/ # so that this configuration can be included into nginx‘s conf

# sudo ln -s /path/to/your/project/base/mysite/mysite_uwsgi.conf /etc/nginx/sites-enabled/mysite_nginx.conf # another name is also ok!

## ------------------------------------------------------------------------------------------------------------------------

vim mysite_uwsgi.conf

# mysite_uwsgi.confserver {    listen         8000;     server_name    10.10.10.132; # after all of these steps,type http://10.10.10.132:8000/ into your browser to get what you want(ps.10.10.10.132 if my IP)    charset UTF-8;     client_max_body_size 75M;     location / {         include uwsgi_params;        uwsgi_pass 127.0.0.1:8001; # nginx(not you) wiil get something from 127.0.0.1:8001 then return to you        uwsgi_read_timeout 2;    }       location /static {        expires 30d;        autoindex on;         add_header Cache-Control private;        alias /path/to/your/project/base/mysite/mysite/static/;     } }

  

# ln this file into the ‘/path/to/nginx/site-enable/‘ (have done in step 2 ‘sudo ln blablablabla‘)

## ------------------------------------------------------------------------------------------------------------------------

vim mysite_uwsgi.py

# mysite_uwsgi.py
import osos.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")from django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()

  

## ------------------------------------------------------------------------------------------------------------------------

vim mysite_uwsgi.ini

[uwsgi]socket = :8001master = truemodule = mysite_uwsgiprocesses = 8listen = 120enable-threads = truedaemonize = /path/to/your/project/base/mysite/mysite_uwsgi.logpidfile = /path/to/your/project/base/mysite/mysite_uwsgi.pidpythonpath = /path/to/your/project/base/mysite/pythonpath = /path/to/your/project/base/mysite/mysite_uwsgipythonpath = /path/to/your/project/base/mysite/mysitebuffer-size = 32768reload-mercy = 8vacuum = true

After all of these above,we can go to Step 3.

 

Step 3:Restart Your Nginx Service And uWsgi service

Here list the Commonly used commands:
sudo /etc/init.d/nginx stop

sudo /etc/init.d/nginx start

sudo /etc/init.d/nginx restart

And in this step you just need to restart your nginx(if it is started before, or use start instead)

 

sudo /etc/init.d/nginx restart # restart nginx

chdir /path/to/your/project/base/mysite

uwsgi --socket 127.0.0.1:8001 --wsgi-file mysite_uwsgi.py # start uwsig service,if not,you will get a 502(Bad Gateway) error.

 

Tips:

  chdir /path/to/your/project/base/mysite

  uwsgi --http 10.10.10.132:8001 --wsgi-file mysite_uwsgi.py

  and then type http://10.10.10.132:8000/ in your browser to see if uwsgi work ok.if ok ,then shose socket

 

Tips:

  nginx + uwsgi

  apache + mod_wsgi

  nginx + apache + ???

 

 Step 4:Check If It is OK

Open your browser and type http://10.10.10.132:8000/ (10.10.10.132:8000 also ok)

And you will see the django‘s beautiful welcome page.

 

for more information:

tutorial from uwsgi.readthedocs.org:Django_and_nginx (follow it but failed,orz...)

tutorial from oschina.net:nginx + uwsgi + django (work)

Start Your Django Project in Nginx with uWsgi