In this article, we will be deploying our app on Heroku. Heroku is a Platform as a Service (PAAS) and they offer web server and database hosting which is sufficient to deploy our app to the web.
Before moving on with this tutorial, there is a prerequisite which is detailed in this guide. You can call it pre-deployment. Please go through it and come back once it's checked ☑️
Also, you need to create an account on Heroku(obviously).
Now, let's begin
Procfile
This tells heroku how to use our app.
touch Procfile
The configuration varies by the app but this should do. Add the configuration below to your Procfile
release: python manage.py makemigrations bookgrid_app
release: python manage.py migrate
web: gunicorn bookgrid_proj.wsgi
Where ‘bookgrid_proj’ is the name of your django project and ‘bookgrid_app’ is the name of your Django app.
Runtime File
This tells Heroku what version of Python to use for our app
touch runtime.txt
Open the file and paste the python version below. Or anyone most compatible with your app and heroku from this list
python-3.8.12
Django-Heroku
This is a Django library for Heroku applications that ensures a seamless deployment and development experience. It is very much needed if you are deploying your app on Heroku
To install the package run
pip install django-heroku
In your settings.py file, at the very bottom add
import django_heroku
django_heroku.settings(locals())
Heroku Deployment
You need to have an account with heroku. If you don't, create one here.
Login via the command line or terminal with
heroku login
Then create a new heroku app, where acel-app is the name you wish to give your app
heroku create acel-app
Now configure git so that when you push to Heroku, it goes to your new app name
heroku git:remote -a acel-app
Then create a PostgresSQL database on heroku for our app
heroku addons:create heroku-postgresql:hobby-dev
And manually add our environment variables except DEBUG and DATABASE_URL
heroku config:set SECRET_KEY='(-e%3z3yp5qsirfl6_+9=ko#!r6%0am8=^x9a))p2)3y-24g%*'
If we had other things in our .env file like email host or cloud storage configuration for images and videos, we’d have to manually add it to heroku as well
Using email host user and email host password in the screenshot above as an example
heroku config:set EMAIL_HOST_USER='mattew@gmail.com'
heroku config:set EMAIL_HOST_PASSWORD='yyuiuilo7tuy'
Now it’s time to push our code up to Heroku itself and start a web process so our Heroku dyno is running.
git push heroku main
heroku ps:scale web=1
The URL of your new app will be in the command line output or you can run heroku open to find it.
Now we need to migrate our PostgresSQL database and create a superuser
heroku run python manage.py makemigrations bookgrid_app
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
AND WE ARE DONE AMIGOS!
Thanks for following to the end and I hope it was helpful.
¡Hasta luego!