How To Install Django On Debian Etch (Apache2/mod_python)

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

This tutorial explains how to install Django on a Debian Etch 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:

apt-get install mysql-server mysql-client

We want MySQL to listen on all interfaces, not just localhost, therefore we edit /etc/mysql/my.cnf and comment out the line bind-address = 127.0.0.1:

vi /etc/mysql/my.cnf

[...]
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
[...]

Then we restart MySQL:

/etc/init.d/mysql restart

Now check that networking is enabled. Run

netstat -tap | grep mysql

The output should look like this:

server1:~# netstat -tap | grep mysql
tcp        0      0 *:mysql                 *:*                     LISTEN     3085/mysqld
server1:~#

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:

apt-get install apache2 apache2-doc apache2-mpm-prefork apache2-utils libexpat1 ssl-cert libapache2-mod-python

 

3 Install Django

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

apt-get install python-django python-mysqldb

 

4 Configure Apache

I will use /var/www here as the document root of my virtual host and /etc/apache2/sites-available/default as the file containing the configuration of my virtual host. Adjust this to your circumstances.

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 /var/www (e.g. in /home/mycode):

mkdir /home/mycode
cd /home/mycode
/usr/share/python-support/python-django/django/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 open my vhost configuration in /etc/apache2/sites-available/default and place the following lines between the <VirtualHost ... >...</VirtualHost> container:

vi /etc/apache2/sites-available/default

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

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/apache2 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.
[...]

 

Share this page:

8 Comment(s)

Add comment

Comments

From: at: 2008-07-24 18:17:19

I don't think it's useful to disable the local-only listening for MySQL, since you're not connecting from a remote host to the MySQL server.

From: Anonymous at: 2009-11-18 10:05:43

The practice of doing what  the author said came from when networking was off and you it was therefore incompatible with django, so a lot of people just turned off security by habit.

 Being bound to 127.0.0.1 and the socket should work just fine, unless you have a separate database server.

From: Anonymous at: 2010-03-19 16:47:32

Thanks a lot man, it works perfectly following your instructions!

From: Anonymous at: 2010-05-06 01:43:44

Great tutorial, super easy and super clear. How about one on alternatives to mod python? 

From: RadicalEd at: 2010-09-08 15:36:38

I've tried everything and nothing work for me, this was the best, now my Django project is working with Apache.

THANKS

From: zeksar at: 2011-07-12 10:18:05

Yeah!

Great!

thx a lot!

From: Jojo Maquiling at: 2013-02-04 15:56:27

Thanks I have followed your instruction installing on my Debian Squeeze. Its working. You're a great help. More power to you.

From: Joe Langst at: 2013-06-18 12:40:05

My setting.py reads: 

DATABASES = {
    'default': {                                                                                                                                        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
       'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
   }
}