Deploy ur Django Site with Nginx lufy September 08, 2017 <p>A note of my django site deployment~~~~</p> <p>Firstly, debug django site with: </p> <p><span style="background-color: #c0c0c0;"># ./manager runserver 0.0.0.0:8000</span></p> <p>And access from web browser, then try the followed steps.</p> <p>Reference Documentation: <a href="https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html" target="_blank">Setting up Django and your web server with uWSGI and nginx</a></p> <p>1. Install nginx</p> <p>Get nginx from <a href="http://nginx.org/download/nginx-1.13.4.tar.gz" target="_blank">http://nginx.org/download/nginx-1.13.4.tar.gz</a></p> <p>Untar, configure, make, make install:</p> <p><span style="background-color: #c0c0c0;"># tar xf nginx-1.13.4.tar.gz</span></p> <p><span style="background-color: #c0c0c0;"># cd nginx-1.13.4</span></p> <p><span style="background-color: #c0c0c0;"># ./configure</span></p> <p><span style="background-color: #c0c0c0;"># make; make install</span></p> <p> Tip: if you prefer to install and manage nginx with a non-super user, just install it by specified user, such as <em>test</em>, and make a directory which owned by user <em>test</em>.</p> <p>Then make a link at <span style="background-color: #c0c0c0;">/usr/bin</span> link to <span style="background-color: #c0c0c0;">/usr/local/nginx</span>, by super user root.</p> <p> Tip: as alternative, when install nginx, <span style="background-color: #c0c0c0;">./configure</span> has several options, with which could also redirect execute file path to <span style="background-color: #c0c0c0;">/usr/bin/</span> , this need user rights if you are useing a non-root user. </p> <p><span style="background-color: #c0c0c0;">[root@host ]# ln /usr/local/nginx /usr/bin/</span></p> <p>Check nginx installation with executing:</p> <p><span style="background-color: #c0c0c0;"># nginx</span></p> <p>Visit <span style="background-color: #c0c0c0;"><em>hostname</em></span> with browser, will get nginx default page.</p> <p>2. Add conf file for django site to nginx.</p> <p>Create a conf file in <span style="background-color: #c0c0c0;">/usr/local/nginx/conf/</span></p> <p><span style="background-color: #c0c0c0;"># vim /usr/local/nginx/conf/mysite.conf</span></p> <p><span style="background-color: #c0c0c0;"># mysite.conf</span><br /><span style="background-color: #c0c0c0;">upstream django{</span><br /><span style="background-color: #c0c0c0;"> server unix:///tmp/mysite.sock; # how nginx get the datas, alternatively, tcp/port could used here, I prefered unix socket as recommended.</span><br /><span style="background-color: #c0c0c0;">}</span><br /><span style="background-color: #c0c0c0;">server {</span><br /><span style="background-color: #c0c0c0;"> listen 8000; # nginx listen port</span><br /><span style="background-color: #c0c0c0;"> server_name hostname; # server name</span><br /><span style="background-color: #c0c0c0;"> charset utf-8; </span><br /><span style="background-color: #c0c0c0;"> client_max_body_size 75M;</span><br /><span style="background-color: #c0c0c0;"> location /static {</span><br /><span style="background-color: #c0c0c0;"> alias /path/to/mysite/static; # static file path, link all the related static files here.</span><br /><span style="background-color: #c0c0c0;"> }</span><br /><span style="background-color: #c0c0c0;"> location / {</span><br /><span style="background-color: #c0c0c0;"> uwsgi_pass django; </span><br /><span style="background-color: #c0c0c0;"> include uwsgi_params; # uwsgi_params file location, here is the relative path: /usr/local/nginx/conf/uwsgi_params.</span><br /><span style="background-color: #c0c0c0;">}</span><br /><span style="background-color: #c0c0c0;">}</span></p> <p>Include <span style="background-color: #c0c0c0;">mysite.conf</span> in <span style="background-color: #c0c0c0;">nginx.conf</span> and add nginx manage user.</p> <p><span style="background-color: #c0c0c0;"># vim /nginx/conf/nginx.conf</span></p> <p><span style="background-color: #c0c0c0;">user test;</span></p> <p><span style="background-color: #c0c0c0;">http {</span><br /><span style="background-color: #c0c0c0;">......</span><br /><span style="background-color: #c0c0c0;"> include mysite.conf;</span><br /><span style="background-color: #c0c0c0;"> default_type application/octet-stream;</span></p> <p><span style="background-color: #c0c0c0;">......</span></p> <p><span style="background-color: #c0c0c0;">}</span></p> <p>Test the conf file(with root user, cause 80 is the default nginx port.):</p> <p><span style="background-color: #c0c0c0;"># nginx -t</span></p> <p>Make the config files effective</p> <p><span style="background-color: #c0c0c0;"># nginx -s reload</span></p> <p>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.</p> <p>3. Install and config uwsgi</p> <p>Install uwsgi, simply execute pip install uwsgi in command line:</p> <p><span style="background-color: #c0c0c0;"># pip install uwsgi</span></p> <p> Tip: <a title="pip documentation" href="https://pypi.python.org/pypi/pip/" target="_blank">how to get pip</a></p> <p>Create ini file, which uwsgi run with.</p> <p><span style="background-color: #c0c0c0;"># vim mysite.ini</span></p> <p><span style="background-color: #c0c0c0;">[uwsgi]</span><br /><span style="background-color: #c0c0c0;">socket=<em>/tmp/mysite.sock</em> # uwsgi generate socket</span><br /><span style="background-color: #c0c0c0;">chdir=<em>/path/to/mysite</em> # mysite directory</span><br /><span style="background-color: #c0c0c0;">module=<em>mysite.wsgi</em> # mysite uwsgi module, python module.</span><br /><span style="background-color: #c0c0c0;">master=true </span><br /><span style="background-color: #c0c0c0;">processes=4 </span><br /><span style="background-color: #c0c0c0;">threads=2</span><br /><span style="background-color: #c0c0c0;">vacuum=true</span></p> <p>Then run uwsgi with:</p> <p><span style="background-color: #c0c0c0;"># uwsgi --ini mysite.ini</span></p> <p>Test hostname:8000 in web browser, should get the same content as debugging django site with ./manager ......</p> <p>When this works well, uwsgi could be run background:</p> <p><span style="background-color: #c0c0c0;"># uwsgi --ini blog.ini --daemonize /tmp/uwsgi-$DATE.log</span></p>
Comments (0)
Leave a Comment
No comments yet. Be the first to comment!