How To Install Django On Fedora 9 (Apache2/mod_python)

Version 1.0
Author: Falko Timme
Last edited 07/21/2008

This tutorial explains how to install Django on a Fedora 9 server. Django is a web framework that allows to develop Python web applications quickly with as much automation as possible. I will use it with Apache2 and mod_python in this guide.

This howto is meant as a practical guide; it does not cover the theoretical backgrounds. They are treated in a lot of other documents in the web.

This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

 

1 Install MySQL

Django can use multiple database backends, e.g. PostgreSQL, MySQL, SQLite, etc. If you want to use MySQL, you can install it as follows:

yum install mysql mysql-server

We create the system startup links for MySQL as follows...

chkconfig --levels 235 mysqld on

... and start MySQL:

/etc/init.d/mysqld start

Run

mysqladmin -u root password yourrootsqlpassword
mysqladmin -h server1.example.com -u root password yourrootsqlpassword

to set a password for the user root (otherwise anybody can access your MySQL database!).

 

2 Install Apache And mod_python

If Apache2 and mod_python aren't already installed on your system, you can install them as follows:

yum install mod_python

(This will also install Apache2 if it isn't already installed.)

 

3 Install Django

In order to install Django and the Python MySQL bindings, we run:

yum install Django MySQL-python

 

4 Configure Apache

Before we configure Apache, we must create a Django project (e.g. called mysite) (see http://www.djangoproject.com/documentation/tutorial01/). For security reasons I create that project outside my document root (I'm using the default Fedora document root /var/www/html here) (e.g. in /home/mycode):

mkdir /home/mycode
cd /home/mycode
/usr/bin/django-admin.py startproject mysite

This will create the directory /home/mycode/mysite with some Python files in it.

Now with the project mysite created, we can configure Apache. I create a backup copy of the original /etc/httpd/conf.d/python.conf file and create a new one as follows:

cp /etc/httpd/conf.d/python.conf /etc/httpd/conf.d/python.conf_orig
cat /dev/null > /etc/httpd/conf.d/python.conf
vi /etc/httpd/conf.d/python.conf

LoadModule python_module modules/mod_python.so

<Location "/mysite">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonDebug On
    PythonPath "['/home/mycode'] + sys.path"
</Location>

(This configuration is valid for the default Fedora 9 vhost in /var/www/html - if you have already defined other vhosts where you'd like to use Django, please place the <Location ...>...</Location> section in the appropriate vhost configuration, but leave the LoadModule line in /etc/httpd/conf.d/python.conf.)

The path in the first line (<Location "/mysite">) refers to the URL - meaning this configuration will be used if you use /mysite in the URL (e.g. http://www.example.com/mysite). You can change this to your likings. Please adjust the other values (SetEnv DJANGO_SETTINGS_MODULE mysite.settings and PythonPath "['/home/mycode'] + sys.path") to the name of your project and the path where it is located.

Restart Apache afterwards:

/etc/init.d/httpd restart

Now you can access http://www.example.com/mysite in a browser. If everything went well, you should see something like this:

This means Django has been successfully installed, and you can now use it to develop your Python web applications (please refer to http://www.djangoproject.com/documentation/ to learn how to develop web applications with Django).

 

5 Connect To A MySQL Database From A Django Project

If you want to use a MySQL database in your Django project, you should first create that database (e.g. mysite) and a database user (e.g. mysiteadmin) for that database:

mysql -u root -p

CREATE DATABASE mysite;
GRANT ALL ON mysite.* TO 'mysiteadmin'@'localhost' IDENTIFIED BY 'mysiteadmin_password';
GRANT ALL ON mysite.* TO 'mysiteadmin'@'localhost.localdomain' IDENTIFIED BY 'mysiteadmin_password';
FLUSH PRIVILEGES;
quit;

Then open the settings.py file in the project folder (e.g. /home/mycode/mysite) and modify the database settings, e.g. as follows:

vi /home/mycode/mysite/settings.py

[...]
DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
DATABASE_NAME = 'mysite'             # Or path to database file if using sqlite3.
DATABASE_USER = 'mysiteadmin'             # Not used with sqlite3.
DATABASE_PASSWORD = 'mysiteadmin_password'         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
[...]

 

6 Links

Share this page:

2 Comment(s)

Add comment

Comments

From: Fogliato at: 2009-05-19 18:28:22

Thanks for your Post, it help me very much! I can configure django on fedora, but i can not configure with mod_wsgi, the Django recomends use WSGI... Can you tell me how to configure with mod_wsgi please?

Thank you!!!

From: akheron at: 2009-11-08 06:06:42

Me gustó el tuto, es entendible y facil de seguir, sin embargo me sale un problema al ponerlo en marcha. Uso fedora 11, Gnome 2.26.3, firefox 3.5.5,python 2.6 y es una laptop DELL XPS m1210. El bug es el siguiente:

MOD_PYTHON ERROR

ProcessId: 2115
Interpreter: 'localhost.xps'

ServerName: 'localhost.xps'
DocumentRoot: '/var/www/html'

URI: '/mysite'
Location: '/mysite'
Directory: None
Filename: '/var/www/html/mysite'
PathInfo: ''

Phase: 'PythonHandler'
Handler: 'django.core.handlers.modpython'

Traceback (most recent call last):

File "/usr/lib/python2.6/site-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)

File "/usr/lib/python2.6/site-packages/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)

File "/usr/lib/python2.6/site-packages/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)

File "/usr/lib/python2.6/site-packages/django/core/handlers/modpython.py", line 228, in handler
return ModPythonHandler()(req)

File "/usr/lib/python2.6/site-packages/django/core/handlers/modpython.py", line 191, in __call__
self.load_middleware()

File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 33, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:

File "/usr/lib/python2.6/site-packages/django/utils/functional.py", line 269, in __getattr__
self._setup()

File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", line 40, in _setup
self._wrapped = Settings(settings_module)

File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", line 75, in __init__
raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)

ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings