Django on RedHat or CentOSComputing · django · Linux centos · django · nginx · redhat
I am normally using Debian, Ubuntu or even Suse for deploying Django, but a recent customer needed to deploy on Red Hat Enterprise Server 5.
We decided beforehand to test deployment on RHEL5 and also on CentOS 5.4 and so these instructions should work for both environments. NginX will be used as the webserver.
The first thing I like to do is to upgrade the repository and add EPEL. This wasn’t needed I found on the RedHat box I was using, but was needed on CentOS but your mileage might vary. It should be noted that we made the system as up to date as possible with patches and yum updates.
I won’t be covering the database setup, it is assumed that this is already setup and done. I also won’t be covering the actually placing of the Django application anywhere.
yum search zlib
yum install zlib-devel*
yum install gcc make
Python 2.4 ships with both RHEL5 and CentOS 5 and is needed for the packaging environment YUM so I would advise not to replace it but just compile and install 2.5.5 (or 2.6) next to it.
tar fxz Python-2.5.5.tgz
Now we need to build and install NginX. I choose this primarily because of my experience with it, (I have used Apache2 a lot but its archaic ) and because of its speed.
yum install pcre-devel.i386 openssl-devel.i386
tar fxz nginx-0.7.65.tar.gz
./configure –sbin-path=/usr/local/sbin –conf-path=/etc/nginx/nginx.conf –pid-path=/var/run/nginx.pid –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –with-http_ssl_module –with-http_flv_module –with-cc-opt=”-I /usr/include/pcre”
sudo chmod +x /etc/init.d/nginx
/sbin/chkconfig nginx on
/sbin/chkconfig –list nginx
There is a decent RedHat init.d script here: http://wiki.nginx.org/RedHatNginxInitScript, but remember that I have changed the installation location above so you will need to change the location in that script of nginx from /usr/sbin/nginx to /usr/local/sbin/nginx
You will also need a nginx.conf configuration script for /etc/nginx/ or you could roll your own using sites-available, sites-enabled pattern. I will attach the script soon.
Now with Python 2.5 installed we can install Django and other requirements:
I am using easy_install here as that is the most widely used tool, but pip with a requirements file is a better solution.
Now we need a database adapter. I am using PostgreSQL 8.4.2, if you are using MySQL, Oracle or some other database you will need to follow the recommended route for the Python adapter.
yum search psycopg2
yum install python-psycopg2.i386 (This will install for the old Python)
yum install postgresql-devel
tar xvfz psycopg2-2.0.13.tar.gz
Change the location of pg_config:
And now install:
python2.5 setup.py build
python2.5 setup.py install
Then you need to install your application. This is really specific to your needs. I choose to install the files in /opt/cantemo/application-name as that is specific to our solutions, but RedHat style might mean that you wish to install somewhere else.
Lastly you will want to have your application start easily and also make sure that it starts if the machine ever reboots. To do this I am going to install the start-stop-daemon that is used on Debian based linux distros on Redhat. I read some threads that it will but there is no ETA on that.
You should download the initd script for Django from http://code.djangoproject.com/wiki/InitdScriptForLinux. Change the RUN_AS user to the user on RedHat that you are going to use for running Nginx. By default this is apache.
tar -xzf apps-sys-utils-start-stop-daemon-IR1_9_18-2.tar.gz
gcc start-stop-daemon.c -o start-stop-daemon
cp start-stop-daemon /usr/local/sbin/
chmod +x /usr/local/sbin/start-stop-daemon
chmod +x /etc/init.d/fastcgi
/sbin/chkconfig –add fastcgi
/sbin/chkconfig –list fastcgi
Lastly make sure that your application directories have the correct user permissions but you should be able to start everything by issuing the following commands:
Eric Florenzano posted a nice tutorial on deploying django using FastCGI so also check that out, but I didn’t have much luck with daemontools on CentOS, and didn’t bother trying on RedHat. I suggest instead using start-stop-daemon. Check it out for other hints and tips though, particularly on prefork vs. threaded, pip, and more nginx configuration tips.comments powered by Disqus