How To Install Python 2.7 CentOS 6 with cPanel/WHM
Deploy your Python based web application on cPanel by setting up your Python environment on cPanel/WHM safely.
Prerequisites:
- CentOS 5/6 32 or 64 bit with root access
- cPanel
cPanel/WHM uses Python 2.7 for its core, so we install Python 2.7 separately since we don’t want to break Centos 6 (yum) that uses Python 2.7.
Creating Python folder
mkdir /usr/src/python2.7
cd /usr/src/python2.7
Installing Python 2.7 into alternate location
mkdir /usr/src/python2.7
cd /usr/src/python2.7/
wget http://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
tar zxvf Python-2.7.8.tgz
cd Python-2.7.8
./configure –prefix=/opt/python2.7 –with-threads –enable-shared
make
make install
Creating symbolic link to the alternate Python version
ln -s /opt/python2.7/bin/python /usr/bin/python2.7
echo ‘/opt/python2.7/lib’>> /etc/ld.so.conf.d/opt-python2.7.conf
ldconfig
ln -s /opt/python2.7/lib/python2.7/config/libpython2.7.a /usr/local/lib/
*for gcc
Test if new Python version works
python2.7
If successful you will see something like this:
Python 2.7.8 (default, Sep 3 2011, 18:28:42)[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Installing Python setup-tools
cd /usr/src/python2.7/
wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg –prefix=/opt/python2.7
Installing easy_install for Python 2.7 (alt)
First get the setup script for Setuptools:
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
Then install it for Python 2.7
python2.7 ez_setup.py
Installing pip for Python 2.7
# Now install pip using the newly installed setuptools:
easy_install-2.7 pip
*if version specific easy_install does not work, use python2.7 -m easy_install pip
Installing virtualenv to our Python 2.7
cd /opt/python2.7/bin/
./easy_install virtualenv
Installing mod_wsgi (this module will work only with python 2.7)
cd /opt/python2.7/lib/python2.7/config/
ln -s ../../libpython2.7.so
cd /usr/src/python2.7/
wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
tar zxvf mod_wsgi-3.3.tar.gz
cd mod_wsgi-3.3
./configure –with-python=/opt/python2.7/bin/python
make
make install
Avoid cPanel’s easy_apache
We need to avoid cPanel’s easy_apache clearing up /usr/local/apache/modules folder
mkdir /usr/local/apache/extramodules
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 the following into the editor:
LoadModule wsgi_module /usr/local/apache/extramodules/mod_wsgi.so
AddHandler wsgi-script .wsgi
Exit and save as prompted
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.
Setting up a virtual environment
Each isolated Python environment (also called sandbox) can have its own Python version and packages making it useful when working on multiple projects.
Install virtualenv for Python 2.7 and create a sandbox called my27project:
pip2.7 install virtualenv (python2.7 -m pip install virtualenv)
virtualenv-2.7 my27project (python2.7 -m virtualenv my27project)
Check the system Python interpreter version:
python –version (should state default python version e.g. 2.6)
Activate the my27project sandbox and check the version of the default Python interpreter in it:
source my27project/bin/activate
python –version (should statedj python 2.7.8)
deactivate
Setting up Django
Login with SSH
Once logged in, type the following commands
mkdir .env
cd .env
virtualenv env (python2.7 -m virtualenv env)
source env/bin/activate
pip install flup
pip install django
echo ‘source /home/username/.env/env/bin/activate’ >> ~/.bashrc
echo ‘export PATH=$PATH:$HOME/.env/env/lib/python2.7/site-packages/django/bin’ >> ~/.bash_profile
echo ‘export PYTHONPATH=$PYTHONPATH:$HOME/.env/env/lib/python2.7/site-packages’ >> ~/.bash_profile
Tip! Copy/paste the above text into a text editor, replace ‘username’ with your actual username, and then copy the text out again and paste it straight into the shell session.
Now we need to test it out.
Log out of shell and log right back in
To the left of the shell prompt you should see ‘(env)’… it’s working!
Does it work? Great! If not, check the steps you already completed. Also, check your .bashrc and .bash_profile to make sure that the entries in them are correct.
(e.g. nano .bashrc)
Assuming it works, enter the Python interactive console by just typing ‘python’ and pressing Enter:
(env)username@server [~]# python
Python 2.7.8 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import flup
>>> import django
Assuming there are still no errors, you’re now ready to install Django and set it up to run via FastCGI, just like you’d do if you were not using virtualenv.
Type the following:
cd ~/
mkdir website
cd website
# replace ‘myproj’ with whatever your project name
django-admin.py startproject myproj
# this is up to you, but it is nice to keep everything contained
mkdir media
mkdir scripts
mkdir templates
Create a symbolic link to your project so Python knows where it is:
cd ~/.env/env/lib/python2.7
ln -s ~/website/myproj/myproj
Make sure we can import the project:
(env)username@server [~]# python
Python 2.7.8 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import myproj
If it doesn’t import, then check your symbolic link that you crated in the previous step. Now you need to to configure Django to work via FastCGI:
Navigate to your public_html directory
cd ~/public_html
Create a file called ‘dispatch.fcgi’ and paste in the following contents, changing ‘username’ to your actual username:
#!/home/username/.env/env/bin/python
import sys
import os
sys.path.insert(0, ‘/home/username/.env/env/lib/python2.7/site-packages’)
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘myproj.settings’
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method=”threaded”, daemonize=”false”)
Once you’ve saved it make it executable:
chmod 755 dispatch.fcgi
Lastly, we need to create a .htaccess file to direct the browser to dispatch.fcgi.
Create a file in public_html called ‘.htaccess’ and paste in the following:
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
Django Dependencies
easy_install -U distribute #update distribute on your virtualenv
pip install MySQL-python #install your package
References
- https://www.webhostpython.com/billing/knowledgebase.php?action=displayarticle&id=33
- http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/
- https://help.asmallorange.com/index.php?/Knowledgebase/Article/View/305/0/installing-django-using-virtualenv
- Configuring Symlink for Python: http://stackoverflow.com/questions/8400272/usr-bin-ld-cannot-find-lpython2-7