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.conf
server {
    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 is 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 os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()

  

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

vim mysite_uwsgi.ini

[uwsgi]
socket = :8001
master = true
module = mysite_uwsgi
processes = 8
listen = 120
enable-threads = true
daemonize = /path/to/your/project/base/mysite/mysite_uwsgi.log
pidfile = /path/to/your/project/base/mysite/mysite_uwsgi.pid
pythonpath = /path/to/your/project/base/mysite/
pythonpath = /path/to/your/project/base/mysite/mysite_uwsgi
pythonpath = /path/to/your/project/base/mysite/mysite
buffer-size = 32768
reload-mercy = 8
vacuum = 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.

 

Q1:invalid request block size: 21573 (max 4096)...skip

 

uwsgi --socket :8052 --wsgi-file demo_wsgi.py --protocol=http

 

http://stackoverflow.com/questions/15878176/uwsgi-invalid-request-block-size

Q2:The requested URL /public was not found on this server.

 # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/your/mysite/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     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }

 

 

 

for more information:

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

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

 

posted @ 2014-09-30 11:13  Mx.Hu  阅读(1794)  评论(0编辑  收藏  举报