How to install Django on SME Server

From Mophilly Support

Jump to: navigation, search

Contents

Introduction

NOTE: These instructions are not complete and have not been verified. Use on a production server is not recommended and not supported.

The page was begun in May of 2007 and we continue to revise it as we have time. It is provided here for your entertainment only. We hope to provide a more professional version at some future time, so until then you may use this information soley at your own risk.

Django requires Apache 2.x and mod_python 3.x. It is recommended on the django site that you use Apache’s prefork MPM, as opposed to the worker MPM. I don't have any idea which one is in use in SME Server.

Installation of Django involves some technical skills. Review the SME Developer Documentation and the Django Product Documentation before attempting to install Django on SME.

Use these instructions at your own risk.

System Description

As of December 28, 2007

OS: SME 7.2, with all updates including smeserver-yum-1.2.0-41

Web Server: Apache 2.0 (included in the SME 7.2 updated distro)

Python: Python 2.3.4

mod_python: 3.1.3-5.1

Preparation

Before you begin, please review the Django tutorial to get a sense of what you will do after SME is setup. Also, decide on a name for your project and its directory. The directory will be somewhere other that in Primary or an iBay. In this document "mysite" is used as an example, but you will want a better name.

Django treats a project, e.g. mysite, as a top level directory and everything you add to "mysite" will be below that component in your URL. Once you have a handle on the behavior of Django, you can rework the set up or add rewrite rules to tidy up the URLs presented to the user of your web site.

Install and configure mod_python for Apache

Please review the Django document "How to use Django with mod_python" for more detailed and up-to-date information. "Getting Mod_python Working" is a good troubleshooting guide.

The Mod_python project is the main source. For SME use the Mod_python contrib from Marco Hess. This was built for the Trac implementation on SME, but works well for Django too.

yum --enablerepo=smecontribs install smeserver-mod_python

Yum will run for a while, updating repositories and downloading files. It will ask you to confirm the packages to install and ask you to allow the download. When it is done it execute the following commands to reconfigure and restart SME:

signal-event post-upgrade; signal-event reboot

Build the database libraries

In this step you will build the files necessary to "glue" python and MySQL together.

Install the python-devel and mysql-devel packages

Before you can build the MySQLdb libraries, you must download and install python-devel for python 2.3.4 and the appropriate gcc compiler. After you are done compiling you should uninstall the development packages. Leaving these packages installed on a production server may allow a security breach. The removal steps are listed later in this article.

yum install python-devel

Make certain that libstdc++ is available. The output should be "libstdc++-3.4.6-3.1.i386.rpm".

rpm -qa --qf="%{n}-%{v}-%{r}.%{arch}.rpm\n" 'gcc*' 'libstdc++*' | sort

Get the compiler

yum install gcc-c++-3.4.6-3.i386

When it is finished, update and reboot. Yum advises this at the end of every install.

signal-event post-upgrade; signal-event reboot

Now get the developer libraries for mysql.

yum install mysql mysql-devel

When it is finished, update and reboot. Yum advises this at the end of every install, so this step may be unnecessary. I did it again anyway.

signal-event post-upgrade; signal-event reboot

Get MySQLdb

This allows python to connect to MySQL databases. For this step you will make a "tools" directory, download the MySQLdb tarball, build it and finally install it.

Change to your home directory.

cd ~

Add a directory for tools and switch into it.

mkdir tools
cd tools

Download the current version of mysqldb for python. At the time of this article, that was version 1.2.2.

wget http://downloads.sourceforge.net/mysql-python/MySQL-python-1.2.2.tar.gz?modtime=1172959928&big_mirror=0

Unpack the tar ball and change into the directory.

tar -xzf MySQL-python-1.2.2.tar.gz
cd MySQL-python-1.2.2

Read the "README" file.

Edit the site.cfg file if necessary. Please see the package documentation. Build and install the package.

python setup.py build
python setup.py install

Install Django

As of this writing, the Django project is a fast moving project and staying close to the latest release makes getting help much easier. These instructions follow the installation guide from the Django Project Installation Guide.

Remove any old versions of Django. If you used "setup.py install", then removal is simply deleting the "django" directory from the python "site-packages" directory.

Install Subversion if you don't have it. Again, yum is there to help you.

yum install subversion

And yes, you guessed it, time to signal the update and reboot.

Next, download the current version of Django. Change directory to your "tools" directory. The command "svn co" will make the django_src directory for you.

svn co http://code.djangoproject.com/svn/django/trunk/ django_src

Change directory to django_src/django and run setup.py install.

cd /usr/tools/django_src/django/
python setup.py install

Copy the file "django-admin.py" to a useful place on your system path to ease invocation later on.

cp django_src/django/bin/django-admin.py /usr/local/bin/

Now, test this to see that python can find Django. Invoke python then type:

>>> import django

If python does not response with an "ImportError" message, you are good to go.

Remove the development packages and compiler

After you are done compiling you must remove the development packages. This is critical in order to maintain security in SME. Fortunately, yum makes this easy.

yum remove python-devel
yum remove gcc-c++-3.4.6-3.i386
yum remove mysql mysql-devel

With that done, update and reboot.

signal-event post-upgrade; signal-event reboot

Dealing with PYTHON_EGG_CACHE

You can skip this step unless you run into the issue.

When I first ran the Django admin app, I encountered this error:

[Errno 13] Permission denied: '/root/.python-eggs' 

I could not find a permissions issue, all seemed OK, but there are a lot of references to this error on the web. I found link with a post from Brett Neumeier that was helpful.

Here's Brett's solution for the unpacking of the SQLite egg. Adjust for the database you are using; in this case, MySQL_python-1.2.2-py2.3-linux-i686.egg.

cd /path/to/python/site-packages
mv pysqlite-2.0.7-py2.4-linux-i686.egg foo.zip
mkdir pysqlite-2.0.7-py2.4-linux-i686.egg
cd pysqlite-2.0.7-py2.4-linux-i686.egg
unzip ../foo.zip
rm ../foo.zip

Now the egg file is unpacked so the files are present in the file system. The makes the PYTHON_EGG_CACHE irrelevant and everybody is happy.

Dealing with Django media location

The location of the Django media directory is worth looking at. Django assumes it has control of the primary, and perhaps only, web server document tree. This makes installation on SME a bit bothersome unless you are comfortable with the primary bay being "django-ized".

Create the SME Module Templates

Next step is to make sure the module is added to httpd.conf. For SME Server, that means adding a module template. The template needs a special file name and location. Be sure to review the SME Developer Documentation for information on the SME template system..

Make a custom template directory if it does not exist.

mkdir -p /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf

Copy the necessary templates from the stock templates used by SME, found in the "templates" directory, into the new directory. Be sure you only copy these and do not modify or delete them. Also, give them names that are meaningful to your Django installation. By using a unique name that preserves the order of loading, you will have an easier time if you need to reconfigure the templates.

cp /etc/e-smith/templates/etc/httpd/conf/httpd.conf/20LoadModule80
     /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf/20LoadModule80django
cp /etc/e-smith/templates/etc/httpd/conf/httpd.conf/80AddHandler00
     /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf/80AddHandler00django

I use the "locate" command to find pathnames. New files and directories need to be added to the locate command's database.

updatedb

Modify the custom template files. I used pico, but vim, nano and others work just as well. Do not use WordPad or TextEdit, as these change the file format.

File: 20LoadModule80django

# Add support for Django
 LoadModule python_module modules/mod_python.so
# end django add

File: 80AddHandler00django

Alias /mysite /usr/local/mysite
<Directory "/usr/local/mysite/"> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE settings PythonDebug On PythonAutoReload On Options None AllowOverride All order deny,allow deny from all allow from all </Directory>

Please note the PythonDebug and PythonAutoReload directives. For a production site you will want set both of these to "Off". Also, the "deny" and "allow" settings may not be appropriate for your server.

Rebuild the httpd template and restart the httpd service.

expand-template /etc/httpd/conf/httpd.conf
sv t /service/httpd-e-smith

Now check that the service is running.

sv s /service/httpd-e-smith

You should get a response similar to this...

run: /service/httpd-e-smith: (pid 28537) 61s, normally down

If you get something different, check your error logs. Correct any warnings or errors reported before proceeding.

The final step is to add a symbolic link in site-packages so the python interpreter can find your project directory. You can add the path to the PYTHONPATH, but I prefer the symbolic link

ln -s /usr/local/mysite /usr/lib/python2.3/site-packages/mysite

Start Learning Django

You are ready to start working with Django. The best place to start is with the Django Project Tutorial.

Personal tools