Saturday, July 11, 2015

Install, configure, and test mod_python/mod_wsgi/AWS tools/boto on Apache 2.x on CentOS 6.x

·        Download, compile, make, and install mod_python and mod_wsgi.

PS: mod_python shouldn’t be used. https://docs.python.org/2/howto/webservers.html covers the history and the reasoning for doing so.

Cmds: (as a privileged user)

cd /tmp/;mkdir mod_python_3.5.0;cd mod_python_3.5.0/

wget http://dist.modpython.org/dist/mod_python-3.5.0.tgz

gunzip mod_python-3.5.0.tgz

tar -xvf mod_python-3.5.0.tar

cd mod_python-3.5.0/

[If you don’t have apxs (APache eXtenSion) then install httpd development package]

yum install httpd-devel

./configure --with-python=/usr/local/bin/python2.7

[you can omit --with-python option if you want to use the default

make

sudo make install

ll /etc/httpd/modules/mod_python.so

 

cmds: (as privileged user)

cd /tmp/

mkdir mod_wsgi; cd mod_wsgi/

wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.4.12.tar.gz

tar -zxvf 4.4.12.tar.gz

cd mod_wsgi-4.4.12/

make

sudo make install

ll /usr/lib64/httpd/modules/mod_wsgi.so

ldd /usr/lib64/httpd/modules/mod_wsgi.so

httpd -M

service httpd restart

 

cmds: (as privileged user)

vi /etc/httpd/conf/httpd.conf

LoadModule wsgi_module modules/mod_wsgi.so

#inside VirtualHost or <Directory> where document root is defined:

    WSGIScriptAlias /myapp /var/www/wsgi-scripts/myapp.wsgi

 

    <Directory /var/www/wsgi-scripts/>

    Order allow,deny

    Allow from all

    </Directory>

apachectl configtest

service httpd restart

 

create a file with python content

vi /var/www/wsgi-scripts/myapp.wsgi

import sys

 

def application(environ, start_response):

    status = '200 OK'

 

    output = ''

    output += 'sys.version = %s\n' % repr(sys.version)

    output += 'sys.prefix = %s\n' % repr(sys.prefix)

 

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

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

    start_response(status, response_headers)

 

    return [output]

 

Test:

curl localhost/myapp

sys.version = '2.7.9 (default, <MON> <DAY> <YYYY>, <<HH:MM:SS>) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]'

sys.prefix = '/usr/local'

 


 

.bashrc file: (you decide you want for all user for a particular user. Pls note that for a particular user for boto they could have .boto file under their home dir)

export LD_LIBRARY_PATH=/usr/local/lib/python2.7

export LD_RUN_PATH=/usr/lib64/httpd/modules

export JRE_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.45-28.b13.el6_6.x86_64/jre/

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.45-28.b13.el6_6.x86_64

export EC2_HOME=/usr/local/ec2/ec2-api-tools-1.7.4.0/

export EC2_URL=https://ec2.us-west-1.amazonaws.com

export PATH=$PATH:$EC2_HOME/bin

export AWS_ACCESS_KEY=<your key>

export AWS_SECRET_KEY=<your secret>

export aws_access_key_id=<your key>

export aws_secret_access_key=<your secret>

 

source . bashrc

 

printenv | grep -i aws

 

 

Accessing boto within Apache

 

1.      Create a python virtual environment, install boto

2.      Create /etc/boto file and copy the contents of your boto file

3.      Configure Apache to use the virtual environment

 

    WSGIScriptAlias /myapp /var/www/wsgi-scripts/myapp.wsgi #mapping

    WSGIPythonHome /usr/local/lib/python2.7/site-packages/BASELINE #home of python virtual environment

    WSGIPythonPath /var/www/wsgi-scripts #path to your python scripts

4.      (optional) redirect output from existing python code so that phython module elsewhere needs minimal code change.

    output = cStringIO.StringIO()

    sys.stdout = stdout = cStringIO.StringIO()

    #call to your python code that uses boto for AWS

    print >> output, stdout.getvalue()

 




No comments:

Post a Comment