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

Why You Should Use Django REST Framework

Prerequisites

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

$ django-admin.py startproject geoapi
geoapi/
manage.py
geoapi/
__init__.py
settings.py
urls.py
wsgi.py

Step 5: Create a Django Application

$ geoapi/manage.py startapp mygeoapi
mygeoapi/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
$ nano geoapi/geoapi/settings.py
. . .
# The simplest case: just add the domain name(s) and IP addresses of your Django server
# ALLOWED_HOSTS = [ 'example.com', '203.0.113.5']
# To respond to 'example.com' and any subdomains, start the domain with a dot
# ALLOWED_HOSTS = ['.example.com', '203.0.113.5']
ALLOWED_HOSTS = ['your_server_domain_or_IP', 'second_domain_or_IP', . . .]
DATABASES = {
'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/manage.py collectstatic
$ sudo ufw allow 8000
$ geoapi/manage.py runserver 0.0.0.0:8000
http://server_domain_or_IP:8000

Step 6: Install Django Rest Framework-GIS

$ pip install djangorestframework
$ pip install djangorestframework-gis
INSTALLED_APPS = [
# ...
'rest_framework',
'rest_framework_gis',
# ...
]
$ geoapi/manage.py createsuperuser

Step 7: Making Our Project Spatial

$ pip install psycopg2
$ pip install django-leaflet
INSTALLED_APPS = [
...
'leaflet',
]
$ pip freeze > requirements.txt

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

$ gunicorn --bind 0.0.0.0:8000 geoapi.wsgi

Step 9: Create a Gunicorn systemd Service File

$ sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=username
Group=www-data
WorkingDirectory=/home/username/geoapi
ExecStart=/home/username/geoapi/geoapienv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/username/geoap/geoapi.sock geoapi.wsgi:application
[Install]
WantedBy=multi-user.target
$ 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'

Conclusion

--

--

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