Posts Tagged ‘CentOS’

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

Yum Install CentOS-5 (Upgrade from CentOS 4 to CentOS 5)

July 1, 2010

Don’t follow any how-to without having your brain in alarm mode.

I found a nice tutorial (http://www.tocpcs.com/yum-install-centos-5-upgrade-from-centos-44-to-centos-5/) from 2007 for the upgrade from CentOS4 to CentOS5  using yum only – which I tried to use and did NOT follow to the end – otherwise my server, standing in a datacenter 150 km away, would have been wiped out.

First of all: This is not for the unexperienced and I don’t take any responsibilities for your data!

So here is the tutorial for the fearless, double checked and proven:

1. First things first: Tell the system that your core is CentOS 5 (which is not right yet, but that way you will get the right packages):

rpm -Uvh http://ftp.tudelft.nl/centos.org/5/os/i386/CentOS/centos-release-5-5.el5.centos.i386.rpm http://ftp.tudelft.nl/centos.org/5/os/i386/CentOS/centos-release-notes-5.5-0.i386.rpm

2. clean up yum’s cache

yum clean all

2a. check that all 3rd party yum repositories (atomic, rpmforge…) are disabled!!! Otherwise you will never ever solve the dependencies

3. remove packages, that might be in the way during the upgrade:

rpm -e --nodeps hal

4. clean up your kernels (yes, there might be more than one)

uname -a (find out, which kernel is your operational kernel)

rpm -qa | grep kernel (find all installed kernels)

rpm -e ... (for all kernels but your operational kernel)

rpm -e --justdb --nodeps (for the active kernel)

5. Now it’s time for the first try

yum upgrade

6. …yes, and now you have to wait until your system sorts out your problems. In the end you will get a list of problematic packages. For me it was python-elemnttree which is scary, because it belongs to yum. Remove all packages where you are sure you can miss them (and re-install later):

rpm -e --justdb --nodeps python-elementtree

…and VFlib2, which I really don’t need, so I removed it with all dependencies:

rpm -e VFlib2 ghostscript ghostscript-fonts hpijs system-config-printer hal-cups-utils

7. …and off we go:

yum upgrade

8. Now, it takes time – and in the end my yum was broken :-)   So here is the fix:

rpm -Uvh --force http://ftp.tudelft.nl/centos.org/5/os/i386/CentOS/python-sqlite-1.1.7-1.2.1.i386.rpm
yum clean all

9. and that’s it. check your grub configuration and reboot, you’re now the proud owner of CentOS5

10. like to clean up a bit more?

rpm -qa | grep el4

11. all those packages can either be removed or – when you got them from a sneaky RPM mirror with newer versions – downgraded.

just for the record: I found the following on my system:

rhnlib-2.1.2-11.el4
dmraid-1.0.0.rc14-9_RHEL4_U7.el4
mysql-libs-5.0.68-1.el4_6
ash-0.3.8-20.el4_7.1
php-pear-1.4.11-1.el4s1.1
perl-DBD-MySQL-4.006-1.el4.centos
up2date-4.7.1-17.el4.centos
perl-DBI-1.604-1.el4s1
php-pecl-zip-1.8.0-1.el4.centos

…and this is, what I did:

rpm -e up2date rhnlib mysql-libs php-pecl-zip ash
yum downgrade perl-DBI perl-DBD-MySQL php-pear dmraid

finally I brought the RPM-database into a consistent state. Not needed, but sometimes helpful

rm -f /var/lib/rpm/__*
rpm --rebuilddb

And that’s definetly it!

enjoy, it worked for me 4 times on different servers!