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).
- 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 https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz
- 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 https://pypi.python.org/packages/source/s/setuptools/setuptools-18.2.tar.gz
- tar xvf setuptools-18.2.tar.gz
- cd setuptools-18.2
- python2.7 setup.py install
- cd ..
- curl https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py | python2.7 –
- pip2.7 install virtualenv
Create a User to Own the Project
- useradd -m -s /bin/bash/someuser
Create a Python virtualenv and Install the Django Project
- 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
- sudo su –
- cd /home/someuser/foo
- /home/someuser/.virtualenvs/foo/bin/gunicorn –workers 4 –timeout 60 –bind 0.0.0.0:8000 foo.wsgi:application
- Hit Ctrl-C to kill the process if you don’t see any errors.
- 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 shutdownscript
cd /home/someuser/foo
/home/someuser/.virtualenvs/foo/bin/gunicorn –workers 4 –timeout 60 –log-level debug –bind 0.0.0.0:8000 foo.wsgi:application
endscript - 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
- 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 onRewriteRule ^(.*)$ http://127.0.0.1:8000$1 [P]
ProxyPassReverse / http://127.0.0.1:8000/
</VirtualHost> - apachectl restart