How to Create a Django Rest Framework-GIS on Ubuntu 16.04 Server (Part 1)

Why You Should Use Django REST Framework


Step 1: Install Packages from Ubuntu Repositories

$ sudo apt-get update$ sudo apt-get install python3-pip python3-setuptools python3-dev libpq-dev postgresql postgresql-contrib nginx
$ sudo apt-get install -y postgis postgresql-9.5-postgis-2.2

Step 2: Create a PostgreSQL Database and User

$ sudo -u postgres psql
potgres=# CREATE DATABASE geoapi;
postgres=# CREATE USER yourprojectuser WITH PASSWORD 'yourpassword';
$potgres=# CREATE EXTENSION postgis;
potgres=# ALTER ROLE yourprojectuser SET client_encoding TO 'utf8';potgres=# ALTER ROLE yourprojectuser SET default_transaction_isolation TO 'read committed';potgres=# ALTER ROLE yourprojectuser SET timezone TO 'UTC';postgres=# ALTER ROLE yourprojectuser SUPERUSER;
postgres=# GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;

Step 3: Create a Python Virtual Environment

$ sudo -H pip3 install --upgrade pip$ sudo -H pip3 install virtualenv
$ mkdir geoapi$ cd geoapi
$ virtualenv geoapienv
$ source geaopienv/bin/activate
$ pip install django==1.11.5 gunicorn psycopg2

Step 4: Create a Django Project

$ startproject geoapi

Step 5: Create a Django Application

$ geoapi/ startapp mygeoapi
$ nano geoapi/geoapi/
. . .
# The simplest case: just add the domain name(s) and IP addresses of your Django server
# ALLOWED_HOSTS = [ '', '']
# To respond to '' and any subdomains, start the domain with a dot
# ALLOWED_HOSTS = ['', '']
ALLOWED_HOSTS = ['your_server_domain_or_IP', 'second_domain_or_IP', . . .]
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geoapi',
'USER': 'yourprojectuser',
'PASSWORD': 'yourprojcetpassword',
'HOST': 'localhost',
'PORT': ''
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
$ geoapi/ collectstatic
$ sudo ufw allow 8000
$ geoapi/ runserver

Step 6: Install Django Rest Framework-GIS

$ pip install djangorestframework
$ pip install djangorestframework-gis
# ...
# ...
$ geoapi/ createsuperuser

Step 7: Making Our Project Spatial

$ pip install psycopg2
$ pip install django-leaflet
$ pip freeze > requirements.txt

Step 8: Testing Gunicorn’s Ability to Serve the Project

$ gunicorn --bind geoapi.wsgi

Step 9: Create a Gunicorn systemd Service File

$ sudo nano /etc/systemd/system/gunicorn.service
Description=gunicorn daemon
ExecStart=/home/username/geoapi/geoapienv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/username/geoap/geoapi.sock geoapi.wsgi:application
$ sudo systemctl start gunicorn$ sudo systemctl enable gunicorn

Step 10: Configure Nginx to Proxy Pass to Gunicorn

$ sudo nano /etc/nginx/sites-available/geoapi
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/username/myproject;
location / {
include proxy_params;
proxy_pass http://unix:/home/username/geoapi/myproject.sock;
$ sudo ln -s /etc/nginx/sites-available/geoapi /etc/nginx/sites-enabled
$ sudo nginx -t
$ sudo systemctl restart nginx
$ sudo ufw allow 'Nginx Full'




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store