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.7Your 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
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:
- 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.
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
/home/someuser/.virtualenvs/foo/bin/gunicorn --workers 4 --timeout 60 --log-level debug --bind 0.0.0.0:8000 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 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:
# serve static files from Apache
RewriteRule ^/static/.* - [L]
# proxy everything else to the gunicorn process
RewriteRule ^(.*)$ http://127.0.0.1:8000$1 [P]
ProxyPassReverse / http://127.0.0.1:8000/
- 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.