Deploying your website to Heroku can seem like a hassle. However if you follow this guide, you should be up and running in no time.
I will assume that you have a complete Django website that you are ready to publish. You will also need Git.
First we need to set up version control. It is important to initiate Git inside of the same folder as your manage.py file.
Enter the following commands into your Git Bash terminal and make sure to use your name and email address instead of the sample text.
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL ADDRESS"
git config --global push.default matching
git config --global alias.co checkout
You will now see a hidden .git folder in your directory.
To add your project, you simply type
git add .
The next step is to download the Heroku Toolbelt, which you can get here: https://devcenter.heroku.com/articles/heroku-cli
Once downloaded, restart your terminal. Navigate back to your main folder and turn on your virtual environment.
To make sure Heroku is installed correctly, type
If you get a version number, you have installed it correctly.
Next, we need to create our SSH keys.
In your terminal, type
We then need to go into this directory by typing
To create our SSH key, we simply type
You will be asked where you want to save your key and whether you want to password protect it. We will simply hit enter for each prompt to save the file in the folder we have created and we will choose not to password protect the key. You will then have to hit enter a third time to confirm that you do not want to set a password.
You can make sure that your SSH key has been created by typing
ls to show the contents of your .ssh folder.
Next, we will go back to our Django project. You should have the virtual environment running and be inside the main folder.
Now we need to create a Heroku account. You can do this here: https://www.heroku.com/
Go back to your terminal and type
heroku login to connect to your Heroku account.
The next step is to add our SSH key to Heroku by typing
You will see a prompt that says the key has been found and you need to confirm the upload by pressing y then enter.
Once the SSH key is uploaded, hit Ctrl + C to get back to your Django folder.
Next, we will create a Procfile that will tell Heroku how our website works. In the terminal, type
You will see the file in the same folder as your manage.py file.
Open the file and add the following command to set the webserver to gunicorn:
web: gunicorn DJANGO_PROJECT_NAME.wsgi
Make sure to replace DJANGO_PROJECT_NAME with the name of your project.
Now we need to use pip to install a few more things:
pip install django_heroku
pip install python-decouple
pip install dj_database_url
pip install gunicorn
You can use
pip freeze to make sure that everything has been installed.
Next, we need to import everything into our settings.py file:
from decouple import config
We also need to set debug mode to false and add heroku to allowed hosts:
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '.herokuapp.com']
Inside middleware, add the following AFTER Django security middleware:
Your middleware list should look like this:
MIDDLEWARE = [
And finally add the following to the bottom of your settings.py file:
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
The next step is to create a requirements file for Heroku. This is done by typing
pip freeze > requirements.txt in your terminal. This will create the requirements file and place it into your main folder.
Now we can push this commit to Heroku. Type the following into your terminal:
git add .
git commit -am "Initial commit"
Next, we will create the heroku app by typing
Once the app is created, you will be presented with a link to your Heroku hosted website.
You will most likely want to rename this app unless you have your own domain. To rename the app, simply type
heroku rename YOUR_CUSTOM_NAME
The url will be YOUR_CUSTOM_NAME.herokuapp.com
We are now ready to push our code to Heroku by typing
git push heroku master
Once complete, you should be able to visit your website by typing the url into your browser. If you get an Application error, you can see the issue by typing
heroku logs into your terminal.
Congratulations! Your website is now live!
Big thanks to John Elder and Codemy. If you prefer video tutorials, you can see his great tutorial here
If you would like to see the source code of Crypto Ledger, a Django website I host on Heroku, you can go to my GitHub: https://github.com/TMDStudios/crypto_ledger
You can see the website here: https://crypto-ledger.herokuapp.com/