Django includes a useful geographic services system called GeoDjango. Getting it up and running on CentOS can be a chore, particularly if you need packages newer than CentOS 6 provides. In this post, I'm going to list the steps I took install GeoDjango with PostgreSQL's PostGIS system. If you're reading this, then I'm going to assume you have at least a passing familiarity with Linux and Django.
I want to use the latest software packages, something that CentOS isn't well known for. Specifically, I want the latest Django, PostgreSQL, and PostGIS. Different Django projects may require different versions of 3rd party packages, so I also I want the Django/Python stuff in its own virtual environment. I want as much of the software to be updateable through yum or pip as possible, so I tried to stick to official repositories and PyPI packages when I could.
The patch number of the software shouldn't matter for my purposes. Here are the current versions at the time I am writing:
**Three Notes About Security:**
- The passwords I use are bad passwords - they are for example use only.
- You can (and should) configure Postgres to be more secure. Again, this configuration is for example use only.
- I installed almost everything logged in as root (via su -). This is a bad habit. Again: EXAMPLE!
Setting up CentOS
I used the netinstall ISO for 64-bit CentOS so that I could feel like I was making progress immediately. I accepted the defaults whenever possible. Some customizations:
- Source was http://mirror.centos.org/centos/6/os/x86_64/
- Hostname set to geotest.
- Package selection was set to Minimal Desktop.
- Root's password was set to password.
- Username set to jdoe with password password.
After the installation, I set up the VirtualBox guest additions. Be sure to update the kernel and and install some required packages first:} Now you can install the guest additions. Reboot again after they're installed.===== Configuring PostgreSQL =====
Install PostgreSQL and PostGIS
We're going to use the official Postgres yum repositories so that we can easily install PostgreSQL 9 instead of the CentOS-supplied version 8. PostGIS is also available through the PG repo, so we'll install that too.Before you start, add exclude=postgresql* to the [base] and [updates] sections of /etc/yum.repos.d/CentOS-Base.repo. This prevents us from accidentally installing the CentOS PG packages.} ==== Create A Geospatial Database ====
Now we'll use the psql program to create a database with geospatial extensions, i.e. PostGIS.} Verify PostGIS is enabled for your database with: psql -d geodb -c "SELECT postgis_lib_version();". Here's the results the command gave me:}
If the above query results in an error, I don't know what happened and you should give up on computers.===== Installing Python =====
Since CentOS uses Python 2.6 for important system tools, we don't want to clobber the default Python. I based my process on this excellent post at Too Much Data. Here are the commands I used:} The command python2.7 should now be available. Let's keep installing packages for Python:} ===== Set up a Python Virtual Environment =====
Django projects are, like most software, easier to develop and deploy when you keep a tight reign on their configuration. A virtual environment ("virtualenv") will keep your reliance on system Python packages to a minimum. You will be able to install specific versions of each package in a virtualenv, without needing special permissions and without trampling on the system packages.Up until now I've been executing everything as root. The rest of this guide should be executed with normal user privileges, such as my jdoe example account. We told PostgreSQL to allow a user called "jdoe" access to the database, so these examples won't work as root.==== Basic virtualenv ====
It is simple to start a new virtualenv. We'll make one called vegeo.} ==== Installing GDAL ====
Installing the Python GDAL package is more involved. The version of GDAL installed by the PostgreSQL repo is 1.9. The latest GDAL on PyPI is 1.10, so we need to download the older version. We also need to use a specific command to build it because GDAL's setup.py is broken.} ===== Start a Django Project =====
You are now ready to start a Django project. You'll be able to use GeoDjango and get all of the geospatial goodness it offers. I have posted a small proof-of-concept Django project on GitHub so you don't have to write any code:} If you run the server and visit http://127.0.0.1:8000/admin/geotest/friend/ you can add a friend to the database, and the coordinates for their house using the OpenStreetMap widget.