Changes between Version 12 and Version 13 of DjangoAndNginx


Ignore:
Timestamp:
May 16, 2012, 4:23:49 PM (13 years ago)
Author:
Collin Anderson
Comment:

rewrote most of the page. previous instructions were from 2008

Legend:

Unmodified
Added
Removed
Modified
  • DjangoAndNginx

    v12 v13  
    1 I just recently grabbed a hosting setup on VPSLink.com and wanted to get Django up and running. I'd heard good things about nginx, but had never set it up or configured it before, so I figured I'd give it a try. There were a few little bumps I ran into, but in the end the setup worked pretty well so I wanted to share my setup and configuration experience with everyone here.
     1nginx is a reverse proxy and needs to connect to a server that contains django. This guide is for using [https://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/ fastcgi], but you could instead use
     2[http://bartek.im/blog/2009/04/06/setting-up-nginx-django.html apache+mod_wsgi],
     3[http://senko.net/en/django-nginx-gunicorn/ gunicorn],
     4[https://www.google.com/search?q=nginx+uwsgi+django uwsgi], or
     5[http://www.rkblog.rk.edu.pl/w/p/django-nginx/#3 SCGI+Cherokee]
    26
    3 This setup is running Ubuntu Hardy 8.04 as a xen instance on VPSLink, this walks through starting everything up from scratch, but assumes you are running as root (type `su` to switch to the root user)
     7----
     8This assumes you are using ubuntu 10.04 or later.
    49
    5 The first thing I had to do was install python:
     10You'll need nginx and flup installed.
    611{{{
    7 aptitude install python
     12sudo aptitude install nginx python-flup
    813}}}
    914
    10 Next, I needed Django, which I got the latest SVN release (due to a ManyToMany bug I saw in the past, I'm sticking to a release I didn't have trouble with):
     15In your django project, start up fastcgi. See the [https://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/ fastcgi docs] for more info.
    1116{{{
    12 aptitude install subversion
    13 cd /
    14 mkdir django
    15 cd django
    16 svn co http://code.djangoproject.com/svn/django/trunk/
    17 cd trunk
    18 python setup.py install
    19 }}}
    20 
    21 Next, to run Django through fastcgi, we need python-flup:
    22 {{{
    23 aptitude install python-flup
    24 }}}
    25 
    26 Let's start a sample project just to make things easy:
    27 (Note: django-admin.py *should be* in your path and executable after a proper install, if this is not the case you may need to fix your installation or environment)
    28 {{{
    29 cd /
    30 mkdir projects
    31 cd projects/
    32 django-admin.py startproject sample_project
    33 }}}
    34 
    35 Also, create a media directory:
    36 {{{
    37 cd /
    38 mkdir media
    39 }}}
    40 
    41 Let's start up the fastcgi setup:
    42 (Note: I didn't need any extra stuff on my pythonpath, but if you do, just add --pythonpath=/path/to/add)
    43 (Note: Nothing should print here)
    44 {{{
    45 cd /projects/sample_project/
    46 python manage.py runfcgi host=127.0.0.1 port=8080 --settings=settings
     17python ./manage.py runfcgi host=127.0.0.1 port=8080
    4718}}}
    4819
    4920
    50 Now let's setup Nginx:
    51 
    52 First, create a user for nginx to run as, and then remove a password from it so no one can log in as the user:
     21Next, we need to create a configuration file in `/etc/nginx/sites-available/sample_project.conf` and symlink it to `/etc/nginx/sites-enabled/sample_project.conf`:
    5322{{{
    54 useradd nginx
    55 passwd -d nginx
     23sudo touch /etc/nginx/sites-available/sample_project.conf
     24sudo ln -s /etc/nginx/sites-available/sample_project.conf /etc/nginx/sites-enabled/sample_project.conf
    5625}}}
    5726
    58 Let's install nginx from aptitude:
     27Here is what a minimal conf file would look like:
    5928{{{
    60 aptitude install nginx
    61 }}}
     29server {
     30    listen 80;
     31    server_name myhostname.com;
     32    access_log /var/log/nginx/sample_project.access.log;
     33    error_log /var/log/nginx/sample_project.error.log;
    6234
    63 Next, we need to change the configuration (and back up the default one just in case):
    64 {{{
    65 cd /etc/nginx/
    66 mv nginx.conf nginx-backup.conf
    67 touch nginx.conf
    68 }}}
     35    # https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production
     36    location /static/ { # STATIC_URL
     37        alias /home/www/myhostname.com/static/; # STATIC_ROOT
     38        expires 30d;
     39    }
    6940
    70 Now we need to put some new stuff into the default nginx configuration, here is the template I used:
    71 {{{
    72 user  nginx nginx;
     41    location /media/ { # MEDIA_URL
     42        alias /home/www/myhostname/static/; # MEDIA_ROOT
     43        expires 30d;
     44    }
    7345
    74 worker_processes  2;
    75 
    76 error_log /var/log/nginx/error_log info;
    77 
    78 events {
    79         worker_connections  1024;
    80         use epoll;
    81 }
    82 
    83 http {
    84         include         /etc/nginx/mime.types;
    85         default_type    application/octet-stream;
    86 
    87         log_format main
    88                 '$remote_addr - $remote_user [$time_local] '
    89                 '"$request" $status $bytes_sent '
    90                 '"$http_referer" "$http_user_agent" '
    91                 '"$gzip_ratio"';
    92 
    93         client_header_timeout   10m;
    94         client_body_timeout     10m;
    95         send_timeout            10m;
    96 
    97         connection_pool_size            256;
    98         client_header_buffer_size       1k;
    99         large_client_header_buffers     4 2k;
    100         request_pool_size               4k;
    101 
    102         gzip on;
    103         gzip_min_length 1100;
    104         gzip_buffers    4 8k;
    105         gzip_types      text/plain;
    106 
    107         output_buffers  1 32k;
    108         postpone_output 1460;
    109 
    110         sendfile        on;
    111         tcp_nopush      on;
    112         tcp_nodelay     on;
    113 
    114         keepalive_timeout       75 20;
    115 
    116         ignore_invalid_headers  on;
    117         index index.html;
    118 
    119         server {
    120                 listen 80;
    121                 server_name localhost;
    122                 location /site_media  {
    123                         root /media/; # Notice this is the /media folder that we create above
    124 
    125                         location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
    126                                 access_log   off;
    127                                 expires      30d;
    128                         }
    129                 }
    130 
    131                 location / {
    132                         # host and port to fastcgi server
    133                         fastcgi_pass 127.0.0.1:8080;
    134                         fastcgi_param PATH_INFO $fastcgi_script_name;
    135                         fastcgi_param REQUEST_METHOD $request_method;
    136                         fastcgi_param QUERY_STRING $query_string;
    137                         fastcgi_param CONTENT_TYPE $content_type;
    138                         fastcgi_param CONTENT_LENGTH $content_length;
    139                         fastcgi_pass_header Authorization;
    140                         fastcgi_intercept_errors off;
    141                         fastcgi_param REMOTE_ADDR $remote_addr;
    142                         }
    143                 access_log      /var/log/nginx/localhost.access_log main;
    144                 error_log       /var/log/nginx/localhost.error_log;
    145         }
     46    location / {
     47        include fastcgi_params;
     48        fastcgi_pass 127.0.0.1:8080;
     49    }
    14650}
    14751}}}
    14852
    149 Now all we need to do is start up nginx:
     53Now, reload nginx.
    15054{{{
    151 /etc/init.d/nginx start
     55sudo /etc/init.d/nginx reload
    15256}}}
    15357
    154 Anonymous says there is a lot cleaner way to install Nginx+Django on a Debian-based system. See [http://serverfault.com/a/370573/52873 this post on stackoverflow for details]
     58You may use a unix socket instead of a port number like so: `fastcgi_pass unix:/home/www/myhostname/fastcgi.sock`
     59
     60You'll probably want to keep your fastcgi process running using [http://blog.bashton.com/2011/using-django-fastcgi-with-upstart/ upstart], [https://code.djangoproject.com/wiki/InitdScriptForLinux init.d], or [http://just-another.net/byteflowdjango-supervisord-nginx-win supervisor]
     61
     62See also:
     63- [http://serverfault.com/a/370573/84192 serverfault question on django+nginx]
     64- [http://wiki.nginx.org/DjangoFastCGI nginx wiki page on django+FastCGI]
Back to Top