Installing Pyramid on Red Hat Enterprise Linux 6

Installing Pyramid on Red Hat Enterprise Linux 6

This Document covers how to install Pyramid on Red Hat Enterprise Linux 6 (rhel6) for both development with the paste server and production with Apache and mod_wsgi. It borrows heavily from the Pyramid Documentation (http://docs.pylonsproject.org) but suggests changes as they apply to rhel6.

Development

If you develop on rhel6 (this has also been tested on Fedora 14), this section will cover how to install pyramid so you can run the the paster server from your home directory. It borrows from Pyramid's documentation but suggests changes as they apply to rhel6:

Install your own Python

We do this because we will be installing other Python packages and don't want to risk breaking the system Python rhel6 depends on. It's also nice to have the latest version (2.7.1 as of the time this was written).

You might need to install the following packages on a bare-bones installation of rhel6 server.

sudo yum install wget gcc make zlib-devel
mkdir -p ~/python/install
cd ~/python/install
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz 
tar xzf Python-2.7.1.tgz
cd Python-2.7.1
./configure --prefix=$HOME/opt/Python-2.7.1 
Before you build Python you will need to enable zlib:
vim Modules/Setup 
and then uncomment the reference to include zlib. Then build and install Python:
make
make install

Install Pyramid

mkdir -p ~/python/pyramid/install
cd ~/python/pyramid/install
wget http://peak.telecommunity.com/dist/ez_setup.py
~/opt/Python-2.7.1/bin/python ez_setup.py
~/opt/Python-2.7.1/bin/easy_install virtualenv
~/opt/Python-2.7.1/bin/virtualenv --no-site-packages env
cd env
bin/easy_install ordereddict
bin/easy_install pyramid
cd ~
echo "export PATH=$HOME/python/pyramid/install/env/bin:$PATH" >> ~/.bashrc
source ~/.bashrc

You can then test your environment by trying the following tutorials:

Production

If you run rhel6 in a production environment this section will show you how to configure it to host Pyramid Applications using Apache and mod_wsgi. It has been tested and works with SELinux enabled. It borrows heavily from the Pyramid document: Running a Pyramid Application under mod_wsgi, but makes changes for rhel6.

Install httpd and mod_wsgi from RedHat

 yum install httpd mod_wsgi
Modify /etc/httpd/conf/httpd.conf to define a ServerName.

Install your own Python

We do this because we will be installing other Python packages and don't want to risk breaking the system Python rhel6 depends on. It's also nice to have a the latest version (2.7.1 as of the time this was written).

mkdir -p /opt/src
cd /opt/src
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz 
tar xzf Python-2.7.1.tgz
cd Python-2.7.1
./configure --prefix=/opt/Python-2.7.1 
vim Modules/Setup # "remove line commenting out zlib"
make; make install

Install ez_setup.py and virtualenv

cd /opt/src
wget http://peak.telecommunity.com/dist/ez_setup.py
/opt/Python-2.7.1/bin/python ez_setup.py 
/opt/Python-2.7.1/bin/easy_install virtualenv

Create a virtualenv which we'll use to install our application

Note that there is a lot of similarity between step 4 and onward from the Pyramid document: Running a Pyramid Application under mod_wsgi, but which changes made for rhel6:

We'll create a virtualenv inside of where we want to install our application. RedHat conventionally stores web apps in /var/www:

mkdir /var/www/modwsgi
cd /var/www/modwsgi
/opt/Python-2.7.1/bin/virtualenv --no-site-packages env

Install Pyramid into the newly created virtualenv

cd /var/www/modwsgi/env
bin/easy_install ordereddict
bin/easy_install pyramid

Install your application

cd /var/www/modwsgi/env
bin/paster create -t pyramid_starter myapp
cd myapp
../bin/python setup.py install
You should also verify that your application will run before mixing it with mod_wsgi:
../bin/paster serve development.ini

Create a pyramid.wsgi file

The file should be located at /var/www/modwsgi/env/pyramid.wsgi, have permissions 755, and contain the following:

from pyramid.paster import get_app
application = get_app(
	'/var/www/modwsgi/env/myapp/production.ini', 'main')

Modify /etc/httpd/conf.d/wsgi.conf so that it contains

LoadModule wsgi_module modules/mod_wsgi.so # provided by RHEL
WSGISocketPrefix /var/run/wsgi # required for RHEL httpd sockets

WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
WSGIDaemonProcess pyramid user=apache group=apache processes=1 \
   threads=4 \
   python-path=/var/www/modwsgi/env/lib/python2.7/site-packages
WSGIScriptAlias /myapp /var/www/modwsgi/env/pyramid.wsgi

<Directory /var/www/modwsgi/env>
  WSGIProcessGroup pyramid
  Order allow,deny
  Allow from all
</Directory>

Note that we added "WSGISocketPrefix /var/run/wsgi" as explained at https://code.google.com/p/modwsgi/wiki/ConfigurationIssues.

Start Apache

service httpd start
then visit 'http://yoursite/myapp'.