Last active
August 6, 2019 10:25
-
-
Save wcarhart/7b802db4019cc8c34640bc0df7419d1b to your computer and use it in GitHub Desktop.
How to deploy a Django application to Heroku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to deploy a Django application to Heroku with PostgreSQL | |
#========== Pre-requirements ==========# | |
### 1. make sure Heroku CLI is installed | |
### read more here: https://devcenter.heroku.com/articles/heroku-cli | |
### 2. use virtualenv to control python packages | |
### 3. replace 'projectname' with your project's name throughout this script | |
### 4. this script is for MacOS and Bash, but is very similar for Linux and other shells | |
#========== if you're lazy like me, make an alias ==========# | |
alias django='python manage.py' | |
#========== start venv ==========# | |
### if we don't have virtualenv installed, let's install it | |
pip install virtualenv | |
### create new venv, if it doesn't exist already (ignore this step if it does) | |
virtualenv -p `which python3` venv | |
echo "venv/" >> .gitignore | |
### activate venv | |
source venv/bin/activate | |
#========== setup staticfile management ==========# | |
### first, turn off debug settings to simulate production environment | |
### put this in settings.py | |
""" | |
debug = False | |
allowed_hosts = ['*'] | |
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'local_insecure_key') | |
""" | |
### make sure you have your static files configured properly in settings.py | |
cat <<EOT >> settings.py | |
STATICFILES_DIRS = ( | |
os.path.join(BASE_DIR, '<YOUR STATIC DIRS HERE>'), | |
) | |
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') | |
EOT | |
### install whitenoise for production static file management | |
pip install whitenoise | |
### make sure local still works | |
django collectstatic | |
django runserver | |
#========== switch from SQLite to PostgreSQL ==========# | |
### install psycopg2 for django ORM | |
pip install psycopg2-binary | |
### for MacOS, you might need Homebrew | |
### install Homebrew from here: https://brew.sh/ | |
### then, do: | |
brew install postgresql | |
### make local database | |
psql | |
> CREATE DATABASE projectname_db; | |
> CREATE USER projectname_admin WITH PASSWORD 'projectname_db_password'; | |
> ALTER ROLE projectname_admin SET client_encoding TO 'utf8'; | |
> ALTER ROLE projectname_admin SET default_transaction_isolation TO 'read committed'; | |
> ALTER ROLE projectname_admin SET timezone TO 'UTC'; | |
> GRANT ALL PRIVILEGES ON DATABASE projectname_db TO projectname_admin; | |
> ALTER DATABASE projectname_db OWNER TO projectname_admin; | |
> \q | |
### set up credentials | |
export DB_PASSWORD=projectname_db_password | |
### configure Django db management | |
pip install dj_database_url | |
### add the following to settings.py IN PLACE OF your previous `DATABASES = ...` | |
""" | |
DATABASES = { | |
'default': dj_database_url.config() | |
} | |
try: | |
from local_settings import * | |
except ImportError: | |
pass | |
""" | |
### then, add this to a new local_settings.py file | |
""" | |
import os | |
DATABASES = { | |
"default": { | |
"ENGINE": "django.db.backends.postgresql_psycopg2", | |
'NAME': 'projectname_db', | |
'USER': 'projectname_admin', | |
'PASSWORD': os.environ.get('DB_PASSWORD'), | |
'HOST': 'localhost', | |
'PORT': '5432', | |
} | |
} | |
""" | |
### make sure still works on local | |
django makemigrations | |
django migrate | |
django runserver | |
#========== save deployment ready app to dev git remote ==========# | |
echo "web: gunicorn projectname.wsgi" > Procfile | |
pip install gunicorn | |
pip freeze > requirements.txt | |
echo "staticfiles/" >> .gitignore | |
git add -A | |
git commit -m "Ready for deploy" | |
git push | |
#========== configure heroku ==========# | |
### create app(s), usually one for production and one for development | |
### make sure you repeat each of these steps for each app you create | |
### it's good to start with dev, then repeat with prod | |
heroku create projectname-dev | |
heroku create projectname-prod | |
### initial push to Heroku | |
git push https://git.heroku.com/projectname-dev.git master | |
### setup necessary environment variables | |
### go to https://djskgen.herokuapp.com/ to generate a secure Django secret ID | |
heroku config:set --app projectname-dev DJANGO_SECRET_KEY="YOUR SECRET KEY" | |
heroku config:set --app projectname-dev DB_PASSWORD=$DB_PASSWORD | |
### configure PostgreSQL addon | |
heroku addons:create --app projectname-dev heroku-postgresql:hobby-dev | |
### migrate the database | |
heroku run --app projectname-dev python manage.py makemigrations | |
heroku run --app projectname-dev python manage.py migrate | |
### if you have a script to initialize database values, run that too | |
heroku run --app projectname-dev python your_db_script.py | |
### check the Heroku deployment | |
open https://projectname-dev.herokuapp.com | |
### unwind | |
open https://www.ballastpoint.com/location/ballast-point-miramar/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment