Search This Blog

Python 2.7, Django, Apache, and Gunicorn on CentOS 6.5

I'm working on a little Django project for KPTZ Radio in Port Townsend and since this project has to talk to a serial relay board from a specific server that has other things running on it, I've been going through the process of installing Python 2.7 on CentOS 6.5, along with configuring Django, Apache, and Gunicorn.

Since I'm a lot more used to dealing with Nginx and Gunicorn on Ubuntu, getting this all up and running correctly took a lot more trial and error than I thought it would, but I finally got it figured out so figured I'd share since I found a lot of either incomplete or misleading information about this as I searched for solutions.

Installing Python 2.7

Your first question is probably why I'm not installing Python 3. In the case of this particular project, pyserial was not (when I first started the project) Python 3 compatible, so rather than fight that battle I decided to use Python 2.7.

The problem with Python 2.7 is on CentOS 6.5, Python 2.6.6 is the default, and since there's other Python-related stuff running on the server already I didn't want to run the risk of screwing anything else up, so I had to install Python 2.7 as an alternate Python installation. Luckily there were a couple of resources from people who had already been through this so it wasn't an issue. Here's the steps I took on a fresh CentOS 6.5 VM I was using to do some trial runs before doing everything on the production server (do all these as the root user).

Create a User to Own the Project

Depending on how you want to do things this could be considered optional, but I created a user to own the project files (again as root):

useradd -m -s /bin/bash/someuser

Create a Python virtualenv and Install the Django Project

Next we'll create a Python 2.7 virtualenv, grab the Django code, install the Django project's requirements, and do a couple of other configuration things for the Django app.

sudo su - someuser

mkdir ~/.virtualenvs

cd ~/.virtualenvs

virtualenv foo --python=python2.7

cd foo

source bin/activate

cd ~/

git clone foo

cd foo

pip install -r requirements.txt

python manage.py runserver (just to make sure things are working at this point)

python manage.py collectstatic

python manage.py migrate

Configure Upstart to Start the Gunicorn Process When the Server Boots

I suppose on the next version of CentOS this will be done with systemd but thankfully on CentOS 6.5 we can still use Upstart. Note that if you're familiar with Upstart on Ubuntu the syntax is quite different on CentOS -- thanks to my good friend and former coworker Brandon Culpepper for pointing that out before I lost my mind.

First, we'll do a quick test to make sure everything's working at this point:

Create Apache Virtual Host for the App

There's a bunch of ways to set up Django apps with Apache. In my early days with Django I would have used mod_wsgi but since I'm way more used to Nginx and Gunicorn these days, I figured I'd set up Apache in similar fashion and have it proxy to Gunicorn.

vim /etc/httpd/conf/httpd.conf

Uncomment the NameVirtualHost *.80 line if it isn't already uncommented

Add a new VirtualHost section at the bottom of the Apache config file:<VirtualHost *:80> ServerName whatever DocumentRoot /home/someuser/foo

Comments

Popular posts from this blog

If you follow me on Google+ you'll know I had a recent rant about Windows Media Center, which after running fine for about a year suddenly decided as of January 29 it was done downloading the program guide and by extension was therefore done recording any TV shows.

I'll spare you more ranting and simply say that none of the suggestions I got (which I appreciate!) worked, and rather than spending more time figuring out why, I decided to try something different.

NextPVR is an awesome free (as in beer, not as in freedom unfortunately ...) PVR application for Windows that with a little bit of tweaking handily replaced Windows Media Center. It can even download guide data, which is apparently something WMC no longer feels like doing.

Background
I wound up going down this road in a rather circuitous way. My initial goal for the weekend project was to get Raspbmc running on one of my Raspberry Pis. The latest version of XBMC has PVR functionality so I was anxious to try that out as a …

This is a first for me since under normal circumstances we run all our Django applications on Linux with Nginx, but we're in the process of developing an application for another department and due to the requirements around this project, we'll be handing the code off to them to deploy. They don't have any experience with Linux or web servers other than IIS, so I recently took up the challenge of figuring out how to run Django applications on Windows Server 2012 with IIS.

Based on the dated or complete lack of information around this I'm assuming it's not something that's very common in the wild, so I thought I'd share what I came up with in case others need to do this.

My setup for my day job these days is a Surface Pro 4 and either an LG 34UC87M-B or a Dell P2715Q monitor, depending on where I'm working. This is a fantastic setup, but some applications have trouble dealing with the high pixel density and don't scale appropriately.
One case in point is Skype for Business. For some reason it scales correctly as I move between the Surface screen and the external monitor when I use the Dell, but on the LG monitor Skype is either massive on the external monitor, or tiny on the Surface screen.
After a big of digging around I came across a solution that worked for me, which is to change a setting in Skype's manifest file (who knew there was one?). On my machine the file is here:
C:\Program Files\Microsoft Office\Office16\LYNC.EXE.MANIFEST
And the setting in question is this:<dpiAware>True/PM</dpiAware>
Which I changed to this:
<dpiAware>False/PM</dpiAware>
Note that you'll probably have to edit the file as administr…