Deploy ur Django Site with Nginx

lufy
September 08, 2017

A note of my django site deployment~~~~


Firstly, debug django site with: 


# ./manager runserver 0.0.0.0:8000


And access from web browser, then try the followed steps.


Reference Documentation: Setting up Django and your web server with uWSGI and nginx


1. Install nginx


Get nginx from http://nginx.org/download/nginx-1.13.4.tar.gz


Untar, configure, make, make install:


# tar xf nginx-1.13.4.tar.gz


# cd nginx-1.13.4


# ./configure


# make; make install


  Tip: if you prefer to install and manage nginx with a non-super user, just install it by specified user, such as test, and make a directory which owned by user test.


Then make a link at /usr/bin link to /usr/local/nginx, by super user root.


  Tip: as alternative, when install nginx, ./configure has several options, with which could also redirect execute file path to /usr/bin/ , this need user rights if you are useing a non-root user. 


[root@host ]# ln /usr/local/nginx /usr/bin/


Check nginx installation with executing:


# nginx


Visit hostname with browser, will get nginx default page.


2. Add conf file for django site to nginx.


Create a conf file in /usr/local/nginx/conf/


# vim /usr/local/nginx/conf/mysite.conf


# mysite.conf
upstream django{
server unix:///tmp/mysite.sock;        # how nginx get the datas, alternatively, tcp/port could used here, I prefered unix socket as recommended.
}
server {
listen 8000;                                     # nginx listen port
server_name hostname;                   # server name
charset utf-8;  
client_max_body_size 75M;
location /static {
alias /path/to/mysite/static;              # static file path, link all the related static files here.
}
location / {
uwsgi_pass django;                          
include uwsgi_params;                     # uwsgi_params file location, here is the relative path: /usr/local/nginx/conf/uwsgi_params.
}
}


Include mysite.conf in nginx.conf and add nginx manage user.


# vim /nginx/conf/nginx.conf


user test;


http {
......
include mysite.conf;
default_type application/octet-stream;


......


}


Test the conf file(with root user, cause 80 is the default nginx port.):


# nginx -t


Make the config files effective


# nginx -s reload


Until now, 8000 port is listened by nginx, when visit from browser, would get a nginx 502 page, because the uwsgi -- the web gateway is not ready.


3. Install and config uwsgi


Install uwsgi, simply execute pip install uwsgi in command line:


# pip install uwsgi


  Tip: how to get pip


Create ini file, which uwsgi run with.


# vim mysite.ini


[uwsgi]
socket=/tmp/mysite.sock                 # uwsgi generate socket
chdir=/path/to/mysite                     # mysite directory
module=mysite.wsgi                        # mysite uwsgi module, python module.
master=true                                   
processes=4     
threads=2
vacuum=true


Then run uwsgi with:


# uwsgi --ini mysite.ini


Test hostname:8000 in web browser, should get the same content as debugging django site with ./manager ......


When this works well, uwsgi could be run background:


# uwsgi --ini blog.ini --daemonize /tmp/uwsgi-$DATE.log

Comments (0)

Leave a Comment
Maximum 1000 characters

No comments yet. Be the first to comment!