Changes between Version 8 and Version 9 of DjangoAndNginx


Ignore:
Timestamp:
Feb 10, 2012, 5:33:07 AM (13 years ago)
Author:
anonymous
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DjangoAndNginx

    v8 v9  
    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.
    2 
    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)
    4 
    5 The first thing I had to do was install python:
    6 {{{
    7 aptitude install python
    8 }}}
    9 
    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):
    11 {{{
    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
    47 }}}
    48 
    49 
    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:
    53 {{{
    54 useradd nginx
    55 passwd -d nginx
    56 }}}
    57 
    58 Let's install nginx from aptitude:
    59 {{{
    60 aptitude install nginx
    61 }}}
    62 
    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 }}}
    69 
    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;
    73 
    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                 location / {
    130                         # host and port to fastcgi server
    131                         fastcgi_pass 127.0.0.1:8080;
    132                         fastcgi_param PATH_INFO $fastcgi_script_name;
    133                         fastcgi_param REQUEST_METHOD $request_method;
    134                         fastcgi_param QUERY_STRING $query_string;
    135                         fastcgi_param CONTENT_TYPE $content_type;
    136                         fastcgi_param CONTENT_LENGTH $content_length;
    137                         fastcgi_pass_header Authorization;
    138                         fastcgi_intercept_errors off;
    139                         fastcgi_param REMOTE_ADDR $remote_addr;
    140                         }
    141                 access_log      /var/log/nginx/localhost.access_log main;
    142                 error_log       /var/log/nginx/localhost.error_log;
    143         }
    144 }
    145 }}}
    146 
    147 Now all we need to do is start up nginx:
    148 {{{
    149 /etc/init.d/nginx start
    150 }}}
Back to Top