oct
2008
Getting Django up and running on Centos 5.2 with Apache(mod_python) and MySQL
This is partly for my reference (doing it the second time just now and I forgot everything!) but hopefully it will be helpful for others too.
Disclaimer; I am by no means a Linux expert. This is just how I got it working... If there is a better, easier or wiser way please let me know!
Let's assume you have Yum installed (I didn't on my VPS - but my provider installed it for me).
First, make sure you are up to date;
$ yum update
These are the packages I installed (httpd and python are already installed);
- mod_python (Apache uses this to run Python)
- mysql-server
- mysql-devel (easy_setup needs this)
- python-devel (easy_setup needs this)
- python-setuptools (so we have easy_setup)
- gcc (easy_setup needs this)
$ yum install mod_python mysql-server mysql-devel python python-devel python-setuptools gcc
At this point I would have added MySQL-python to this list, however the default repo's I have only give me version 1.2.1 and Django needs 1.2.1p2 or later. The easiest way I found to get this was with easy_install but it requires a few other packages as mentioned above.
After that is finished you can go on to install MySQL-python by doing this;
$ /usr/bin/easy_install MySQL-python
This should get you version 1.2.2. Perfect. Now, if all went well you should now be almost ready to rock. All that is left is getting Django and doing some config, so follow the instructions here. Replicated below for convenience (but you should really check the website... i.e. a new version might be out);
$ wget http://www.djangoproject.com/download/1.0/tarball/
$ tar xzvf Django-1.0.tar.gz
$ cd Django-1.0
$ sudo python setup.py install
Now we have everything we need, just do a bit of set-up to do. I'm going to show you how to set this up as an Apache virtual host. It's fairly simple, thankfully;
ServerName www.example.com
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonDebug On
PythonPath "['/home'] + sys.path"
ErrorLog logs/example-error_log
CustomLog logs/example-access_log common
ServerName media.example.com
DocumentRoot /home/media/
also make sure to un-comment this line in the httpd.conf;
NameVirtualHost *:80
In the above config you will need to change the server name to your domain. Change the PythonPath to where you have your Django project. The above config would be for a website residing in /home/mysite/ and the settings file in /home/mysite/settings.py
The media has been configured to run off a sub-domain as recommended in the Django docs. Django should not serve static media, it just eats up resources. Ideally media should be on another server, even before you move the database to another server... You can read some more about this stuff here along with a generic installation guide (rather than centos specific)
I also changed the default storage engine in MySQL to innodb as I'd discussed in a previous post and while I was in there I added the option 'skip-networking' so the top lines on my my.cnf looked like this;
[mysqld]
skip-networking
default-storage_engine = innodb
This basically means Django will use innodb and skip-networking stops mySQL opening a port to the outside world. I only want to access it from inside this machine.
Now you arer ready to go serve a Django project!
Thanks to the folk #django for help with this.
Short url - Related tags: centos, django, linux, mysql, python, server-technology