Preamble
Of the several ways to deploy Django, I immediately dismounted mod_python, because I did not want to raise heavy Apache. Decided to deploy on a lightweight web server. At the moment, the main lightweight alternatives to Apache two are lighttpd and nginx. I initially chose the first one, but I 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 handy thing, by the way), I was disappointed - the login form was shown, but when I tried to log in I was thrown to the
admin . After half an hour of googling, I found a
topic on the notorious Ivan Salagayev forum, which described a solution to the problem. After I followed the described tips, it all worked for a bang. I present to you the necessary configuration of the server and Django.
Nginx configuration
All paths are specified for my distribution (ArchLinux), they may differ between you:
1. / etc / nginx / conf / fastcgi_params
')
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;
About the last parameter in this file: whether it is necessary or not for Django, I do not know, but did not conduct research, it works and so.
2. /etc/nginx/conf/nginx.conf
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;
}
}
}
Everything, the nginx setup is complete.
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 should use "python manage.py"
method - prefork or threaded, prefork is recommended due to GIL, as well as the fact that in UNIX and POSIX-compatible OS, creating a process is cheaper than creating a stream.
host, port - the host and port on which the FastCGI server will hang, the parameters must be the same as those specified in the nginx config. As for me, using port communication is better than using a socket, but this is up to you.
pidfile - the name of the file where the PID of the server will be written in order to be able to kill him later
That's all, everything works :) For convenience, I usually create a small server.sh file at the root of the project that manages the FastCGI server. Here are its contents:
#!/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
Using it and so, I think, is clear, if not - I will explain:
server.sh start - starts the server
server.sh stop - stops the server
server.sh restart - restarts the server
PS Well, the first post on Habré is published :)
UPD: Transferred to the thematic blog