Django on apache + mod_fastcgi
Today I was configuring apache(2) under mod_fastcgi to get my django project working on it. It was requested since the testing would go on a similar-to-deployment method.
So, I followed the guides at djangoproject but, as it seemed, I had some issues. Some were my own problem, some were not.
After fixing them, I decided to write a little “tutorial” to help other people perhaps having the same issues.
First things first
Before digging in to this tutorial, I advice trying to do the guidelines of django itself. In the end, if you follow as strictly as possible, it should work.
What will be discussed here will look very (and I mean very) similar to the guidelines of django, but with some notes on what I met on the process.
What is assumed?
During this, I’ll just simply assume you’ve got apache, and tested it to work (the way you installed it), I’m not going to explain how to install apache in any way, it’s useless, pointless, and there’s a whole bunch of tutorials covering it way better than I possibly could! GOOGLE it if you don’t have apache yet!
Step one.
So, as for the tutorial itself, we’re going to start of with running your django project in fcgi modus. As it is, the basic usage is very simple, and you can’t possibly go wrong in this.
Got from the official documentation:
./manage.py runfcgi [options]
Where [Options] is mandatory. The simplest way would be to use the socket option (as I’ll describe below), and a pid file is highly recommended!
In the end, you might want to configure your own protocol too (django supports all protocols supported by flup. And I chose for scgi (for unknown reasons really…)
So, knowing those things, the basic best-use would be to use the command:
./manage.py runfcgi socket=/path/to/socket/file pidfile=/path/to/pid/file [protocol=your_protocol]
To make things easier, I put those things straight in my project directory. Although one might opt-in for the approach of having a subdirectory, or using a completely different directory.
However, to start, which will help anyone having trouble setting things up properly, you should try to stop deamonizing the script, eventually you’ll see whatever happens…
To do so, just add daemonize=false to the string of options. Note that we’ll only use this during the test of the setup, if you don’t have any issues, I even wonder why you’re still reading this…
One might also be interested in auto-starting some script, but I’ll leave that shizzle up to you, although I strongly recommend, I can’t cover most auto-start script here (and since I’m used to archlinux, it was a hassle to do so on debian!)
Apache is next.
So, we don’t have to look back to the project anymore, we’re going to start of configuring apache.
Now, on this, I find the documentation of django a bit unclear, but depending on your OS, you’d be editing httpd.conf (or similar, e.g. on my debian it was called apache2.conf)
For clarity I’ll just keep refering to httpd.conf from here on!
After (obviously) installing and activating mod_fastcgi, you’d have to get one line in the httpd.conf file to enable the apache server to communicate with your project.
httpd.conf: FastCGIExternalServer /path/to/fcgi/file -socket /path/to/socket/file
NOTE: The fcgi file can exist, but it is no requirement.
NOTE2: If you’re later on testing get “Permission denied: FastCGI: failed to connect to server blabla: connect() failed” you’ll need to make sure the user apache uses (mostly www-data) has access to the fcgi file. The simpelest way to solve this is a chmod 777 /path/to/fcgi/file
Next thing would be to configure your website. Note that here you’ll have to use the proper syntax to get your /media/ online. I used /media/ too for my own media files (in the end seeing that it isn’t the proper/best method, I’ll change that when I got the guts)
httpd.conf:
<VirtualHost 0.0.0.0>
ServerName example.com
DocumentRoot /path/to/document/root
Alias /media /home/user/python/django/contrib/admin/media # Note that this may differ per setup
RewriteEngine On
RewriteRule ^/(media.*)$ /$1 [QSA,L,PT] # This one will NOT get django to deliver the media files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ /fcgi_file.fcgi/$1 [QSA,L] # Note that you'll have to use the proper filename
</VirtualHost>
To end
Now, restart (or start) apache, and best tail -f the error file (just in case), pay attention to your (if you did what I suggested) non-daemonized django project. If nothing happens, and you can visit your website without an 500 internal server error, or anything similar, you can get the daemonized django project in the running!
Congratulations, you just configured and installed apache + mod_fastcgi to use in your django project. Next would be to actually get things running the way YOU want it.
Before I leave you guys to never visit me again, I’d like to thank the guys at irc.freenode.org#django for the help, and the django developers for getting such a great framework online!
