How To Install Python 2.7 with mod_wsgi on CentOS 5.6 with cPanel

How To Install Python 2.7 with mod_wsgi on CentOS 5.6 with cPanel - Software Documentation Kit

How To Install Python 2.7 with mod_wsgi on CentOS 5.6 with cPanel

09.04.2011, Linux, Programming, Tutorial, by polk.

This is a step by step tutorial which will help you install Python 2.7 with mod_wsgi onto CentOS 5.6 and cPanel.

Prerequisites:

  • CentOS 5.6 32 or 64 bit with root access
  • cPanel
Note: we are not using cPanel custom modules guide because it only works with built-in Python 2.4.

Creating Python folder

mkdir /usr/src/python2.7
cd
/usr/src/python2.7

If required by your project, you can install SQLite

  1. wget http://www.sqlite.org/sqlite-autoconf-3070701.tar.gz
  2. tar zxvf sqlite-autoconf-3070701.tar.gz
  3. cd sqlite-autoconf-3070701
  4. ./configure
  5. make
  6. make install

Installing Python 2.7 into alternate location since we don’t want to break Centos 5.6 (yum) that uses Python 2.4

  1. cd /usr/src/python2.7/
  2. wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
  3. tar zxvf Python-2.7.2.tgz
  4. cd Python-2.7.2
  5. ./configure --prefix=/opt/python2.7 --with-threads --enable-shared
  6. make
  7. make install

Creating symbolic link to the alternate Python version

  1. ln -s /opt/python2.7/bin/python /usr/bin/python2.7
  2. echo '/opt/python2.7/lib'>> /etc/ld.so.conf.d/opt-python2.7.conf
  3. ldconfig

Let’s test if new Python version works

/usr/bin/python2.7

If successful you will see something like this:

Python 2.7.2 (default, Sep  3 2011, 18:28:42)[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2

press control+D to exit

Now we need to install Python setup-tools

  1. cd /usr/src/python2.7/
  2. wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
  3. sh setuptools-0.6c11-py2.7.egg --prefix=/opt/python2.7

Installing virtualenv to our Python 2.7

  1. cd /opt/python2.7/bin/
  2. ./easy_install virtualenv

Installing mod_wsgi (this module will work only with python 2.7)

  1. cd /opt/python2.7/lib/python2.7/config/
  2. ln -s ../../libpython2.7.so
  3. cd /usr/src/python2.7/
  4. wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
  5. tar zxvf mod_wsgi-3.3.tar.gz
  6. cd mod_wsgi-3.3
  7. ./configure --with-python=/opt/python2.7/bin/python
  8. make
  9. make install

We need to avoid cPanel’s easy_apache clearing up /usr/local/apache/modules folder

  1. mkdir /usr/local/apache/extramodules
  2. mv /usr/local/apache/modules/mod_wsgi.so /usr/local/apache/extramodules/

Include mod_wsgi into Apache configuration

nano /usr/local/apache/conf/includes/pre_virtualhost_global.conf

paste:

  1. LoadModule wsgi_module /usr/local/apache/extramodules/mod_wsgi.so
  2. AddHandler wsgi-script .wsgi

Before we restart Apache, lets test if we have the correct configuration

service httpd configtest

You should get this answer:

Syntax OK

Now restart Apache

/scripts/restartsrv httpd

Python 2.7 with mod_wsgi is now installed on your cPanel server.

You can create a test.wsgi file to test Python scripts

  1. def application(environ, start_response):
  2.     """Simplest possible application object"""
  3.     output = "Hello World"
  4.     status = '200 OK'
  5.     response_headers = [('Content-type', 'text/plain'),
  6.                         ('Content-Length', str(len(output)))]
  7.     start_response(status, response_headers)
  8.     return [output]

Please post your questions and comments below.

原文地址:https://www.cnblogs.com/lexus/p/2365840.html