Install Python 2.7.x on CentOS 6.6 with Plesk 12 for Django

Recently I’ve been playing around with REST API’s, during my search for a good framework, I stumbled across Django Rest Framework and fell in love with it instantly. It’s an easy, well documented Framework and there is a quick-start tutorial so you can get going pretty fast. One huge bonus is that the Django Rest Framwork has a build-in Web browsable API, this obviously, is a very welcome feature, especially during the development phase.

The VPS I’m using is relying on CentOS 6.6 for it’s operating system. This poses a problem; CentOS 6.6 comes with Python 2.6.6 installed, while Django requires Python 2.7 or higher.

In this blog post I’m going explain how we can install Django and all it’s required files on a CentOS 6.6 server with Plesk 12. Once you completed all steps you can install Django Rest Framework as described here.

Backup

Before proceeding with any upgrades or installations it is always wise to have a good backup or a snapshot if you are running a virtual machine. Once your backup or snapshot is created connect to your server via SSH with your root user.

Python installation

First of all, lets check your current Python version:

If your version is lower then 2.7.x we have to update, so lets get started. We are going to install Python 2.7.9, pip, virtualenv and we will use Phusion Passenger as an application server for hosting the Django projects.

Update CentOS and install the development tools

Because Python needs to be compiled we need the Development tools, so lets install them.

Install the required components for the Python installation

We also need to install a bunch of components before we can start download and install Python.

Download and extract Python

To download and extract Python run the following commands.

Compile and install Python

And now that we have download and extract the required source files, lets configure, make and (alt)install Python.

* We use “make altinstall” because the regular “make install” method can break yum.

Install setuptools

Install pip

We use pip to install virtualenv, pip is the Python package installer (preferred installer program).

Install virtualenv

To setup a separate virtual Python environment for each app we use virtualenv.

Install the Phusion Passenger application server

Install Ruby

Lets install Ruby with some extra components. I’ve added nano so we can use it later during the config of Phusion, you are however free to use any other text editor.

Install the Phusion gem

Install and build Passenger for apache

Now copy the config output and past it in your favorite texteditor.

Create the Passenger config file

Paste the copied config and Write Out the passenger.conf file.

Restart apache

Close your SSH connection

Setup your hosting in Plesk

Create a domain or subdomain and change it’s Document root to a subdirectory, in this example we use “/django-app/django-public”. Later on we use the parent directory to store the virtual environment and passenger script.

Hosting Settings

Once you’ve created the (sub)domain empty the content of the django-public folder, Plesk will automatically copy the default site stub data to it.

Enable SSH

To setup the virtualenv and install Django lets enable SSH for the current domain user. Once enabled, connect to the server via SSH with the username associated with the domain.

SSH Settings

Create the Virtual Environment

Install Django

And verify your Django installation.

Upload your app

Use your favorite ftp or ssh client to upload your Django application. In the parent directory “django-app”.

Setup Passenger

To setup the application server we need to create a file named passenger_wsgi.py and place it in the “django-app” directory.

Replace the variables on line 3 with the name of your app directory.

Check your app

Screen Shot 2014-10-22 at 09.17.05

Eureka! A Django app hosted on a CentOS 6.6 server with Plesk 12.


Sources:

Plesk and Django

Installing Python 2.7.9 on CentOS 6.5

Phusion Passenger with Apache on RHEL 6 / CentOS 6 / SL 6 with SELinux