How to run Django apps on a CentOS Plesk server

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!

Update: Because some years are gone, I’ve updated this article to current versions: CentOS 5.9 – Python 2.7.2 – Plesk 11 (yeah, the Plesk version doesn’t make a difference, I know):

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

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 python-devel yum-utils
yum install rpmdevtools tk-devel tcl-devel expat-devel db4-devel gdbm-devel sqlite-devel 
yum install bzip2-devel openssl-devel ncurses-devel readline-devel 
yum install git --enablerepo=epel

3b. As there are no (good) Python 2.7 RPMs, we will build them ourselves:

cd /usr/src/redhat
wget https://raw.github.com/nmilford/specfiles/master/python-2.7/python27-2.7.2.spec -O ./SPECS/python27-2.7.2.spec
wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2 -O ./SOURCES/Python-2.7.2.tar.bz2
sed -i 's/^%define config_sharedlib no/%define config_sharedlib yes/' ./SPECS/python27-2.7.2.spec
QA_RPATHS=$[ 0x0001|0x0010 ] rpmbuild -bb ./SPECS/python27-2.7.2.spec
yum localinstall --nogpgcheck /usr/src/redhat/RPMS/`uname -m`/python27-*

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

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

5. Enable Python 2.7 support for mod_wsgi

sed -i 's,^%configure --enable-shared.*,%configure --enable-shared --with-python=/usr/bin/python2.7,' redhat/SPECS/mod_wsgi.spec

6. Compile mod_wsgi and install

rpmbuild -ba redhat/SPECS/mod_wsgi.spec
rpm -Uvh /usr/src/redhat/RPMS/`uname -m`/mod_wsgi-*.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

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

8. copy the static css files into the static directories

python2.7 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/httpdmng --reconfigure-all
service httpd restart

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

Das könnte dich auch interessieren …

9 Antworten

  1. Felix Schwarz sagt:

    Instead of rebuilding mod_wsgi it’s easier just to get python26-mod_wsgi from EPEL. Also using python26-virtualenv you can get a local pip instance.

  2. CoKo sagt:

    Was also my first idea – but that was not working on the Plesk server I tested it on – and the co-op with Plesk was a major need…

  3. Marc sagt:

    Wow, so great to see other people having the exact same problem as I do. Not only just the same, but the same setup and need. Great!

    Now only thing is that your article is a bit out of time with the current RPM builds as well as Python is in 2.7 and Django itself is also in a later version.

    If you read this, can you suggest an updated approach to things? I am pretty much sitting on a CentOs5, with Plesk and need to get Django running. Right now mod_wsgi is stopping me from moving ahead, and the dual Python versions installed only makes it all much worse.

    • CoKo sagt:

      Hmm, I haven’t tried it with never versions since then…

      The dual python versions are normally no problem at all, because the newer version is only available if call it including the version number, so all Plesk and other processes keep using the old sluggish 2.4 (which is a MUST if you want to keep compatibility)…

      Tomorrow night I will give it a shot on a clean CentOS 5.8 install with Plesk (still using 9.5.4, though…) and will post an update…

  4. Alf sagt:

    Did you have any luck on the new version?
    I have a similar problem but with CentOS 5.9 (Final)
    Panel version 11.0.9 Update #41, last updated at Mar 16, 2013 01:19 AM

    Trying to get python 2.7 and django 1.5 working on the above setup.

  5. Jose Luis sagt:

    if i install this module of phyton, i lose php compatibility? thx

Schreibe einen Kommentar zu CoKo Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.