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

来源:互联网 时间:1970-01-01

I'm working on a little Django project for KPTZ Radioin 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 coupleof resourcesfrom 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).

yum -y update yum -y groupinstall "development tools" --skip-broken yum -y install wget gcc gcc-c++ make httpd-devel git vim zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel wget tar xvf Python-2.7.10.tgz cd Python-2.7.10 ./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" make && make altinstall python2.7 -V (to confirm it's working) cd .. wget --no-check-certificate tar xvf setuptools-18.2.tar.gz cd setuptools-18.2 python2.7 install cd .. curl| python2.7 - pip2.7 install virtualenv 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 runserver (just to make sure things are working at this point) python collectstatic python 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:

sudo su - cd /home/someuser/foo /home/someuser/.virtualenvs/foo/bin/gunicorn --workers 4 --timeout 60 --bind foo.wsgi:application Hit Ctrl-C to kill the process if you don't see any errors.

Next we'll create the upstart file:

vim /etc/init/foo.conf Put the following in the foo.conf file and save it:

description "Gunicorn process for foo app"

start on started sshd

stop on shutdown


cd /home/someuser/foo

/home/someuser/.virtualenvs/foo/bin/gunicorn --workers 4 --timeout 60 --log-level debug --bind foo.wsgi:application


start foo (to make sure the upstart process works) ps -wef | grep python (you should see some python processes running under your virtualenv) Create Apache Virtual Host for the App

There's a bunchof 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

# serve static files from Apache

RewriteEngine on

RewriteRule ^/static/.* - [L]

# proxy everything else to the gunicorn process

ProxyPreserveHost on

RewriteRule ^(.*)$$1 [P]

ProxyPassReverse /


apachectl restart

At that point you should be all set! Hope that helps people who are in this same or a similar boat save some time.