Django and Red Hat logos
25 SEP

Originally posted in 2014 on absoluteweb.solutions

The Situation

This past weekend, I have done something I haven’t done in a while, and while initially dreading it, it turned out to be a very good experience. The task? Installing the most recent verision of Django on a Linux Red Hat Rhel 6.6 server. I know, what’s the big deal, right? Well, first of all, I’m not a sysadmin, so re-visiting an OS which I’m not too familiar with (I’m an Ubuntu guy) and the ever-looming ‘dependecy hell’ always gives me a bit of chest pains. I’m also kind of new to the Python/Django world, so with that added in, I was not sure what kind of scenery this road had planned for me.

Needless to say, my worries were quickly realized. By 9 a.m. Saturday morning, I have attempted the easy solution and discovered that it will not work. As you may have guessed, what I was hoping was that I could simply use the `yum` package manager and be done with it. Nope. Ain’t gonna happen. The system has Python 2.6 installed and the best I could do with that is to use Django 1.4, which is not going to work because my app depends on Django 1.7. Hrmm, what to do? Should I upgrade the system Python or should I install it in a virtual environment? Perhaps the first option would be easier, but I decided to go for the latter for three reasons:

I didn’t want to risk affecting other software which may be dependent on that install of Python.
I wanted to challenge myself and learn something new.
My new infatuation with containerized, virtual environments was nagging at me and I just couldn’t get past it.

The Assessment

As mentioned before, the server I had to work with was a Linux Red Hat Rhel 6.6. I had no choice in the matter nor do I disagree with the decision for using it, but that’s what it was. Rhel 7.1 at this point was in Beta and the provider was not supporting it. Now, I have worked with CentOS before, but as of the last three years or so, I haven’t really touched it. So to summarize, here is what I was looking at:

Red Hat Enterprise Linux Server release 6.6 – 64 bit
Apache 2.2
MySql 5.1
Python 2.6.6
Yeah, I know, Apache and MySql are a bit outdated, but at this point, I’m not concerned with that. They’ll work just fine for now and I can always come back to it. As for the installation, I will install the following:

Python 2.7
Virtualenv 1.11.4
Pip 1.5.4
Django 1.7
The Solution
Okay, now that I had a plan in place, it was time to get to work. But before getting into the details, I’d like to give props to all the great resources and posts out there, without which, I would have been totally lost. You can find these at the bottom of this post, if you’d like to delve into details. Also, the solution for this has only been tested in the environment described above. I’m hopeful that it will work equally as well for other Red Hat/CentOS systems.

Assumptions

I will be working out of my home directory (/home/zjasek), so all the code/commands are relative to that.

Dependencies

The first thing I did was to add Epel repo to the yum repositories and update `yum`.

cd ~
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo rpm -Uvh epel-release-6*.rpm
sudo yum update

Next, I installed all the needed packages.

sudo yum install make       
sudo yum install openssl-devel  # necessary during Python 2.7 compilation - http://stackoverflow.com/questions/20829507/virtualenv-no-module-named-zlib
sudo yum install zlib-devel     # necessary during Python 2.7 compilation - https://github.com/yyuu/pyenv/wiki/Common-build-problems
sudo yum install mysql-devel    # necessary for pip install python-mysql - http://stackoverflow.com/questions/5178292/pip-install-mysql-python-fails-with-environmenterror-mysql-config-not-found
sudo yum install python-pip # Python package manager

Install Python 2.7
At that point, I was ready to install a fresh, brand-spankning-new copy of Python 2.7. I will install this as a local copy to be used only by this project.

cd ~
mkdir ~/src # keep source here
mkdir ~/.localpython # keep Python 2.7 here                    

cd ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz                    

cd Python-2.7.9
./configure --enable-shared --prefix=/home/zjasek/.localpython  # IMPORTANT: --enable-shared - https://code.google.com/p/modwsgi/wiki/InstallationIssues
make
make install                    

sudo ln -s /home/zjasek/.localpython/lib/libpython2.7.so.1.0 /usr/lib64 # another gotcha - http://www.harelmalka.com/?p=159

Install Virtualenv
Now for the virtual environment. This is huge for me. I love the idea of having everything in a container for the project. Realistically though, since this server is pretty much dedicated to this project, a global install would have been just fine.

cd ~/src
wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.11.4.tar.gz
tar -zxvf virtualenv-1.11.4.tar.gz                    

cd virtualenv-1.11.4/
~/.localpython/bin/python setup.py install

Now that it’s installed, let’s create one.

cd ~
mkdir env # I'll keep Django and all the 3rd party Django apps in here                    

cd env
~/.localpython/bin/virtualenv py2.7 --no-site-packages --python=/home/zjasek/.localpython/bin/python2.7

Check
Now I start the virtual environment and check Python … drum roll please …

cd ~
source ./env/py2.7/bin/activate # start the virtualenv                    

### Check Python
python --version
(py2.7)[zjasek]$ python --version
Python 2.7.9 

… Sweetness. I have the latest 2.7.x version of python and best of all, it was a piece of cake – at least the 2nd time around.

Install Django and Project Dependencies
Now it’s time to wrap this up and test it out. The last step is to install Django and some other fun stuff required by the project.

cd ~
pip install Django 
pip install mysql-python
pip install suit django-suit-redactor # nice template
pip install django-debug-toolbar # nice for debugging

Create Django Project w/ MySQL
For this project, the requirement was MySQL, so I didn’t bother with the default SqlLite stuff. The rest of this is standard Django installation, which is explained very nicely in the Django docs, but for the sake completeness, I will add it here.

cd ~
source ./env/py2.7/bin/activate # start the virtualenv                    

env/py2.7/bin/django-admin.py startproject testapp .

Okay, all good. Now I go into `settings.py` and change my db to use the mysql engine (I have previously set the MySQL database up, so I’m not showing those steps):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'mydbuser',
        'PASSWORD': 'mypass',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

And take care of `Suit` and `Debug Toolbar`:

# suit app hack
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
    'django.core.context_processors.request',
)                    

INSTALLED_APPS = (
    'suit',
    'suit_redactor',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles', 
    'debug_toolbar.apps.DebugToolbarConfig'
)

And finally, run migrations and crank it up.

cd ~
./manage.py migrate
./manage.py createsuperuser #(folow instructions)
./manage.py runserver

There it is. At this point, the install is ready for development and can be accessed by going to http://120.0.0.1:8000/admin/ in the browser. In part 2, I will go through the bit more painful process of setting up Apache and mod_wsgi for project deployment. Until next time, code on.

References:
http://www.rackspace.com/knowledge_center/article/install-epel-and-additional-repositories-on-centos-and-red-hat
http://stackoverflow.com/questions/20829507/virtualenv-no-module-named-zlib
https://github.com/yyuu/pyenv/wiki/Common-build-problems
http://stackoverflow.com/questions/5178292/pip-install-mysql-python-fails-with-environmenterror-mysql-config-not-found
http://stackoverflow.com/questions/17996628/apxserror-command-failed-with-rc-65536
https://code.google.com/p/modwsgi/wiki/InstallationIssues
http://docs.python-guide.org/en/latest/dev/virtualenvs/
http://stackoverflow.com/questions/5506110/is-it-possible-to-install-another-version-of-python-to-virtualenv