Skip to content

Instantly share code, notes, and snippets.

@bayoishola20
Last active June 5, 2021 19:56
Show Gist options
  • Save bayoishola20/8a822dacaf978086359571ddc4a83b6c to your computer and use it in GitHub Desktop.
Save bayoishola20/8a822dacaf978086359571ddc4a83b6c to your computer and use it in GitHub Desktop.
geodjango-heroku deploy
## Active virtual environment
`$ source bin/activate`
## Git initialization
`$ git init`
## Heroku create app
`$ heroku create map-app`
## Assigning remote target
`$ heroku git:remote map-app`
## Confirm
`$ git remote -v`
## pip installation
`$ pip install python-decouple`
`$ pip install dj-database-url`
`$ pip install dj-static`
`$ pip install psycopg2==2.7` or `$ pip install psycopg2` #this is for the database connection. As at March 2018, use the below
`$ psycopg2-binary==2.7.4`
## settings.py
* from decouple import config
* import dj_database_url
* ALLOWED_HOSTS = ['map-app.herokuapp.com']
* DATABASES = { 'default': dj_database_url.config(conn_max_age=500) }
* DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
* GDAL_LIBRARY_PATH = os.getenv('GDAL_LIBRARY_PATH')
* GEOS_LIBRARY_PATH = os.getenv('GEOS_LIBRARY_PATH')
* STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
* SECRET_KEY = config('SECRET_KEY') #heroku
# You must set settings.ALLOWED_HOSTS if DEBUG is False.
* DEBUG = config('DEBUG', default=False, cast=bool) #heroku
* ALLOWED_HOSTS = ['map-app.herokuapp.com']
## wsgi.py
* from dj_static import Cling
* application = Cling(get_wsgi_application())
## Create a requirements.txt
`$ pip freeze > requirements.txt` #add gunicorn to list
## Create Procfile
* web: gunicorn startproject.wsgi --log-file -
## runtime.txt
* python-2.7.14
## CONFIG VARS
`$ heroku config:set BUILD_WITH_GEO_LIBRARIES=1'`
`$ heroku buildpacks:set https://github.com/dschep/heroku-geo-buildpack`
`$ heroku config:set DEBUG=True` #set to False for production
`$ heroku config:set SECRET_KEY='sdfsswetw44t342at5d56r6-=7-=d'` #as found in settings.py <--- Key used here is random
`$ heroku config:set DISABLE_COLLECTSTATIC=1`
`$ heroku addons:create heroku-postgresql:hobby-dev` # provision free tier of postgresql
# ========================== These two lines are now not needed
`$ heroku config:set GDAL_LIBRARY_PATH=/app/.heroku/vendor/lib/libgdal.so`
`$ heroku config:set GEOS_LIBRARY_PATH=/app/.heroku/vendor/lib/libgeos_c.so`
# ===========================
`$ heroku config` #to see all config
## Deploy
* heroku git:remote -a map-app
* git add -A
* git commit -m 'first'
* git push heroku master
## Database migrate before superuser creation can work
`$ heroku run python manage.py migrate`
`$ heroku run python manage.py createsuperuser`
## Working on heroku locally
`$ heroku run python`
PS: This is for stack-16
PS: If using PostgreSQL 10.1 above, set at least `psycopg2==2.7.1` in requirements.txt
Additional reading: <a href="https://devcenter.heroku.com/articles/postgis#geodjango-setup">postgis geodjango-setup</a>
>> from django.contrib.gis import gdal
>>> gdal.HAS_GDAL
False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment