Scrawls from Preston...

Powered by Pelican.

Thu 29 October 2009

Setting up pinax on Dreamhost Private Server

using apache, mysql, mod_wsgi, and virtualenv

Dreamhost provides free hosting to nonprofits, and for now they seem to have thrown in a virtual private server. This is hard not to take advantage of, and I'm hoping that the VPS is better performing than the shared hosting - but my concern is that they all share the same mysql servers. So here is how I got pinax up and running in its comfy and modern Python web stack.

At first I tried to go the route of custom install of python2.6 - but got bogged down trying to get either passenger_wsgi or mod_wsgi to work properly - they both seemed to have fatal dead ends in the end.

The first thing you need to do is associate a domain with your PS and set it up as a directory of a user.

so that you have a user and directory like /home/django_user/example.com/

In the Dreamhost panel private server section "Web Server Configuration" uncheck "DreamHost Managed:". This will prevent any accidental changes you make in the panel from undoing the customizations you need to make to the apache config.

Next you need to create an admin user in the Dreamhost control panel for your PS, lets say django_admin, this probably should be a different user than is set up for the domain. This user will just be configuring system level software, namely set the python version to use, install the mysql bindings, PIL, and mod_wsgi packages.

login as the admin user and then:

mkdir src

# This is to set python 2.5 as the default - if you don't care you can skip this step - but I'm assuming this is done

cd /usr/bin/

sudo rm python

sudo ln -s python2.5 python

cd ~/src

# note that this is specific for 2.5 - use the right egg for your python version

wget http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c11-py2.5.egg#md5=64c94f3bf7a72a13ec83e0b24f2749b2

sudo sh setuptools-0.6c11-py2.5.egg

You next need to download the MySQL-python tarball from http://sourceforge.net/projects/mysql-python/ as there is no easy way to fetch it from the command line. FTP it to your src/ directory of the admin user then:

tar xvzf MySQL-python-1.2.3c1.tar.gz

cd MySQL-python-1.2.3c1

sudo python setup.py install

cd ..

wget http://effbot.org/media/downloads/Imaging-1.1.6.tar.gz

tar xvzf Imaging-1.1.6.tar.gz

cd Imaging-1.1.6

python setup.py build --force

sudo python setup.py install

python selftest.py

You should see: 57 tests passed. Next:

sudo easy_install pip

sudo pip install virtualenv

# virtualenvwrapper is primarily useful if you are going to do

# dev work or host many projects - I don't assume you fetch this

sudo pip install virtualenvwrapper

cd ~/src

wget http://modwsgi.googlecode.com/files/mod_wsgi-3.0c5.tar.gz

tar xvzf mod_wsgi-3.0c5.tar.gz

./configure --with-apxs=/usr/local/dh/apache2/template/sbin/apxs --with-python=/usr/bin/python

make

sudo make install

# note that your private server ID replaces the 99999,

# sub your favorite editor of choice (vi,emacs etc)

sudo pico /usr/local/dh/apache2/apache2-ps99999/etc/httpd.conf

find the section on modules and add:

LoadModule wsgi_module /dh/apache2/template/lib/modules/mod_wsgi.so

Now your base python environment is configured, don't log out of this user - as we will need to make more changes to the httpd.conf file later. Instead open up another terminal session and login as your django_user

The first thing we need to do is create some folders we will use later

mkdir src envs projects wsgi-scripts

Then we will grab pinax, install it, and activate the virtualenv

cd ~/src

wget http://downloads.pinaxproject.com/Pinax-0.7.1-bundle.tar.gz

tar xvzf Pinax-0.7.1-bundle.tar.gz

python Pinax-0.7.1-bundle/scripts/pinax-boot.py ~/envs/pinax_test/

cd ~

source envs/pinax_test/bin/activate

#optional - update django to current release version

pip install -U django

Now we will create a basic pinax project

pinax-admin clone_project basic_project projects/basictest

cd projects/basictest/

Before you continue, go back to the Dreamhost panel and create a mysql database for your project - note the name and password, and the host name.

Edit the settings.py in your basictest project to use these database settings

Now lets see if we can get pinax up and running:

chmod +x manage.py

./manage.py syncdb

./manage.py runserver 0.0.0.0:8000

You should now be able to go to http://example.com:8000 and see the pinax welcome screen, ctrl-C to stop the dev server.

To get pinax working with mod_wsgi you need two parts, a wsgi script, and changes to the apache configuration

create a file called pinax_basictest.wsgi in your wsgi-scripts folder. Its contents should look like (cobbled from wsgi docs and pinax deployment sample wsgi file):

import os

import sys

import site

sys.stdout = sys.stderr

# not needed in all environments - but is in others

os.environ['PYTHON_EGG_CACHE'] = '/tmp'

# this forces the virtualenv site-packages to be higher priority than system site-packages

site.addsitedir('/home/django_user/envs/pinax_test/lib/python2.5/site-packages')

sys.path.insert(0,'/home/django_user/projects/')

from django.conf import settings

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

sys.path.insert(0, os.path.join(settings.PINAX_ROOT, "apps"))

sys.path.insert(0, os.path.join(settings.PROJECT_ROOT, "apps"))

import django.core.handlers.wsgi

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

# uncomment below to do a sanity check on the wsgi setup

# def test_wsgi(environ, start_response):

# status = '200 OK'

# output = 'Hello World! wsgi py \n' + sys.version + '\n' + '\n'.join(sys.path)

#

# response_headers = [('Content-type', 'text/plain'),

# ('Content-Length', str(len(output)))]

# start_response(status, response_headers)

#

# return [output]

#

# application = test_wsgi

Next you need to switch back to your admin user terminal (you didn't close it right?) and edit your httpd.conf file again

sudo pico /usr/local/dh/apache2/apache2-ps99999/etc/httpd.conf

go to the very end of the file, and before the last closing /virtualhost tag (so inside the virtualhost) add the following

# wsgi

Order allow,deny

Allow from all

# absolute path

WSGIScriptAlias / /home/django_user/wsgi-scripts/pinax_basictest.wsgi

Allow from all

Order allow,deny

Allow from all

Order allow,deny

Alias "/media/" "/home/django_user/projects/basictest/media/"

# note some use hyphen - others _

Alias "/admin_media/" "/home/django_user/envs/pinax_test/lib/python2.5/site-packages/django/contrib/admin/media/"

Finally, still while in the admin user terminal - restart apache (remember to change for your private server id):

sudo /etc/init.d/httpd2 restart apache2-ps999999

Now you should be able to go to http://example.com and see the pinax welcome screen

If you get a 500 error:


https://ptone.com/dablog