Deploying a Django project under nginx
Preamble
Of the several ways to deploy Django, I immediately dismissed mod_python because I didn't want to lift heavy Apache. Decided to deploy to a lightweight web server. At the moment, there are two main lightweight alternatives to Apache - lighttpd and nginx. I initially chose the first, but ran into problems with the URL. I thought that maybe nginx would work better, and deployed the application on it. In this case, one screencast helped me a lot, I don’t remember exactly whose authorship.
Everything was fine, but when I wanted to use the Django admin panel (a convenient thing, by the way), I was disappointed - the login form showed up, but when I tried to log in I was thrown into admin . After half an hour of googling, I found a topicon the well-known forum of Ivan Salagayev, which described the solution to the problem. After I followed the tips described, everything worked out with a bang. I present to you the necessary configuration of the server and Django.
Nginx configuration
All paths are indicated for my distribution (ArchLinux), they may differ for you:
1. / etc / nginx / conf / fastcgi_params Regarding the last parameter in this file: whether it is necessary or not for Django, I don’t know, but I haven’t done any research, it works so. 2. /etc/nginx/conf/nginx.conf That's it, the nginx configuration is finished.
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;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
user http;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name some.cool.server;
charset utf-8;
access_log logs/cool.access.log ;
client_max_body_size 300m;
location / {
fastcgi_pass 127.0.0.1:8881; # эти параметры мы укажем потом и при запуске Django-fastcgi
include fastcgi_params;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Django setup
The command needed to start the server usually looks something like this:
./manage.py runfcgi method=prefork host=127.0.0.1 port=8881 pidfile=/tmp/server.pid
Note: if your manage.py is not made executable, like mine, instead of "./manage.py" you need to use the "python manage.py"
method - prefork or threaded, prefork is recommended in view of the GIL, as well as the fact that on UNIX and POSIX-compatible OSs, creating a process is cheaper than creating a thread.
host, port - the host and port on which the FastCGI server will hang, the parameters must match those specified in the nginx config. As for me, using a port connection is better than using a socket, but this is at your discretion
pidfile - the name of the file where the server’s PID will be written, so that you can then kill it
That's all, everything works :) For convenience, I usually create a small server.sh file in the project root that manages the FastCGI server. Here is its contents: Using it anyway , I think it’s clear, if not, I ’ll explain: server.sh start - starts the server server.sh stop - stops the server server.sh restart - restarts the PS server Well, the first post on Habré has been published :) UPD: Moved to the thematic blog
#!/bin/bash
case "$1" in
"start")
./manage.py runfcgi method=prefork host=127.0.0.1 port=8881 pidfile=/tmp/server.pid
;;
"stop")
kill -9 `cat /tmp/server.pid`
;;
"restart")
$0 stop
sleep 1
$0 start
;;
*) echo "Usage: ./server.sh {start|stop|restart}";;
esac