Posts Tagged ‘django’

How to run Django apps on a CentOS Plesk server

June 17, 2011

The reason for this tutorial is quite easy: To make this happen, I had to read about 100 pages, all with different solutions, all of them not or only partially working.

After aprox. 8 hours of trial and error, I finally had the best of all the posts compiled to a working set of instructions – so here is the HowTo:

Login as user root to the server in question.

First of all: The usage with the pre-installed mod_python is no fun at all and leads to many errors, therefore it is better to switch to mod_wsgi. If you need the mod_python, stop NOW and search for a different solution. Honestly: I tried it also with mod_python and wasted 6 of the above 8 hours, banging with my head into wall after wall.

Update: And now, that everything was compiled and done, they tell me to use Python 2.6 instead of 2.4 – so please find below the latest install guide for Python 2.6!

1. Add the EPEL repository for the correct CentOS version your server runs on:

CentOS 5:

http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm

CentOS 4:

http://download.fedoraproject.org/pub/epel/4/i386/epel-release-4-10.noarch.rpm

2. disable this repository (no joke), otherwise it tries always to update stuff and will break Plesk!
Edit the file /etc/yum.repos.d/epel.repo and set everywhere “enabled=0″

3. install the following packages:

yum install gcc httpd-devel rpm-build
yum install git python26 python26-devel python26-distribute python26-libs python26-simplejson --enablerepo=epel

4. Now we create the pip (Python package installer) tool

cd /usr/src
curl -O http://python-distribute.org/distribute_setup.py
python26 distribute_setup.py
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python26 get-pip.py
yumdownloader --source mod_wsgi --enablerepo=epel
rpm -ivh mod_wsgi*rpm

5. Enable Python 2.6 support for mod_wsgi

cd /usr/src/redhat/SPECS

Edit the file mod_wsgi.spec

change:

%configure --enable-shared

to:

%configure --enable-shared --with-python=/usr/bin/python2.6

6. Compile mod_wsgi and install

rpmbuild -ba mod_wsgi.spec
rpm -Uvh /usr/src/redhat/RPMS/i386/mod_wsgi-3.2-2.i386.rpm

7. Disable mod_python in Apache
Edit /etc/httpd/conf.d/python.conf

change:

LoadModule python_module modules/mod_python.so

# Override type-map handler for /var/www/manual
<Directory "/var/www/manual/mod/mod_python">
        <Files *.html>
                SetHandler default-handler
        </Files>
</Directory>

to:

#LoadModule python_module modules/mod_python.so

# Override type-map handler for /var/www/manual
#<Directory "/var/www/manual/mod/mod_python">
#        <Files *.html>
#                SetHandler default-handler
#        </Files>
#</Directory>

8. Enable mod_wsgi
Edit /etc/httpd/conf.d/wsgi.conf

Change:

#LoadModule wsgi_module modules/mod_wsgi.so

to:

LoadModule wsgi_module modules/mod_wsgi.so

 

Now we’re ready to rumble.

The following describes the setup of the Django Fiber example project (ridethepony) to the domain “domain.nl”:

1. lets go to the subdirectory in question:

cd /var/www/vhosts/domain.nl

2. setup a vhost.conf file to redirect dynamic requests to mod_wsgi and static page requests to the common httpdocs directory
Edit conf/vhost.conf, adding the following lines:

Alias /robots.txt /var/www/vhosts/domain.nl/httpdocs/robots.txt
Alias /favicon.ico /var/www/vhosts/domain.nl/httpdocs/favicon.ico

Alias /static/ /var/www/vhosts/domain.nl/httpdocs/
Alias /media/ /var/www/vhosts/domain.nl/httpdocs/media/

WSGIScriptAlias / /var/www/vhosts/domain.nl/django.wsgi

<Directory /var/www/vhosts/domain.nl>
    Order allow,deny
    Allow from all
</Directory>

3. download the application, unpack and rename

git clone git://github.com/ridethepony/django-fiber-example.git
mv django-fiber-example pony

4. prepare the application

chmod 0777 pony
cd pony
pip install -r requirements.txt
cp settings.example.py settings.py

5. Edit settings.py

Change:

'NAME': 'fiber_example',                # Or path to database file if using sqlite3.

to:

'NAME': '/var/www/vhosts/domain.nl/pony/fiber_example',                # Or path to database file if using sqlite3.

6. Edit settings_default.py

Change:

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)

to:

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
    '/var/www/vhosts/domain.nl/pony/pages/templates',
)

7. Create the default database and import the content

python26 manage.py syncdb
python26 manage.py loaddata ./fixtures/example_initial_data.json
chmod 0666 fiber_example

8. copy the static css files into the static directories

python26 manage.py collectstatic --link
cp -a pages/static/* ../httpdocs/.

9. create the base WSGI file

cd ..

create file django.wsgi with the following content:

import os
import sys

path = '/var/www/vhosts/domain.nl/pony'
if path not in sys.path:
sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

10. activate the Apache and vhost changes:

/usr/local/psa/admin/sbin/websrvmng --reconfigure-all
service httpd restart

11. Enjoy the result at http://domain.nl