Changes between Initial Version and Version 1 of DjangoAndNginx


Ignore:
Timestamp:
May 6, 2008, 2:35:26 PM (17 years ago)
Author:
jgeewax@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DjangoAndNginx

    v1 v1  
     1I 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
     3This setup is running Ubuntu Hardy 8.04 as 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
     5The first thing I had to do was install python:
     6{{{
     7aptitude install python
     8}}}
     9
     10Next, 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{{{
     12aptitude install subversion
     13cd /
     14mkdir django
     15cd django
     16svn co http://code.djangoproject.com/svn/django/trunk/ -r 7210
     17cd trunk
     18python setup.py install
     19}}}
     20
     21Next, to run Django through fastcgi, we need python-flup:
     22{{{
     23aptitude install python-flup
     24}}}
     25
     26Let's start a sample project just to make things easy:
     27(Note: for some reason, the PYTHON_PATH and PATH variables, although they included the django bin directory, still couldn't find django-admin.py, so I included the full path)
     28{{{
     29cd /
     30mkdir projects
     31cd projects/
     32python /usr/lib/python2.5/site-packages/django/bin/django-admin.py startproject sample_project
     33}}}
     34
     35Also, create a media directory:
     36{{{
     37cd /
     38mkdir media
     39}}}
     40
     41Let'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{{{
     45cd /projects/sample_project/
     46python manage.py runfcgi host=127.0.0.1 port=8080 --settings=settings
     47}}}
     48
     49
     50Now let's setup Nginx:
     51
     52First, 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{{{
     54useradd apache
     55passwd -d apache
     56}}}
     57
     58Let's install nginx from aptitude:
     59{{{
     60aptitude install nginx
     61}}}
     62
     63Next, we need to change the configuration (and back up the default one just in case):
     64{{{
     65cd /etc/nginx/
     66mv nginx.conf nginx-backup.conf
     67touch nginx.conf
     68}}}
     69
     70Now we need to put some new stuff into the default nginx configuration, here is the template I used:
     71{{{
     72user  apache apache;
     73
     74worker_processes  2;
     75
     76error_log /var/log/nginx/error_log info;
     77
     78events {
     79        worker_connections  1024;
     80        use epoll;
     81}
     82
     83http {
     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                        }
     140                access_log      /var/log/nginx/localhost.access_log main;
     141                error_log       /var/log/nginx/localhost.error_log;
     142        }
     143}
     144}}}
     145
     146Now all we need to do is start up nginx:
     147{{{
     148/etc/init.d/nginx start
     149}}}
Back to Top