Check out the Latest Articles:
Django Nginx Centos 5

From our earlier installation we went ahead and set up a CentOS box running Django, Apache and mod_wsgi. – you can view that here

Doing a bit of digging around, I have found some substantial information showcasing that Nginx is a much better choice for Django than Apache.

In order to make Nginx work, we need some sort of wsgi combined with Nginx, the easiest way to get this done is to install FLUP

/opt/python2.6/bin/pip install flup

Now let’s go ahead and create another user that will run a separate process, and also allow us to debug easier if something goes wrong.

Add Nginx user

useradd nginx
passwd -d nginx

Install Nginx

yum install nginx
cd /etc/nginx/
mv nginx.conf nginx-backup.conf
touch nginx.conf
vi nginx.conf

NGINX config file example (make sure to replace /www/django_test1/mysite with your project root)

user  nginx nginx;
worker_processes  2;
error_log /var/log/nginx/error_log info;
events {
    worker_connections  1024;
    use epoll;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main
    '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '"$gzip_ratio"';

    client_header_timeout 10m;
    client_body_timeout 10m;
    send_timeout 10m;

    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 2k;
    request_pool_size 4k;

    gzip on;
    gzip_min_length 1100;
    gzip_buffers 4 8k;
    gzip_types text/plain;
    output_buffers 1 32k;
    postpone_output 1460;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    keepalive_timeout 75 20;

    ignore_invalid_headers on;
    index index.html;

    server {
        listen 80;
        server_name localhost;
        location /media/  {
            root /www/django_test1/mysite; # Replace with your own project root
    }
    location /mediaadmin/  {
        alias /opt/python2.6/lib/python2.6/site-packages/django/contrib/admin; # Alias to where our admin media root lives
    }
    location / {
        # host and port to fastcgi server
        fastcgi_pass 127.0.0.1:8080;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
    }
    access_log /var/log/nginx/localhost.access_log main;
    error_log /var/log/nginx/localhost.error_log;
    }
}

If you are running a prior version of apache make sure to stop it.
/etc/init.d/httpd stop

Start my FastCGI

cd /www/django_test1/mysite/
python manage.py runfcgi host=127.0.0.1 port=8080 --settings=settings
/etc/init.d/nginx start

Now that we have moved away from Apache we need to make sure that apache doesn’t switch back on when we restart the server for any needs.

chkconfig httpd off
chkconfig --add nginx
chkconfig --level 2345 nginx on
chkconfig --list nginx



  1. It‘s quite in here! Why not leave a response?