The easiest way: Django App Deployment on Heroku
God damn, why does deploying your app on the web have to be SO difficult? If you ever tried it, did you enjoy running into issues with static files, WhiteNoise, not installing the middleware in the right location, or a whole host of said fun stuff? Yeah, me neither.
Well, not anymore as this ultimate guide is going to solve all the problems that have been annoying you on your path of deploying your Django app on the Heroku Cloud Application Platform and get it running on the server just by following these simple steps.
- Create Heroku Account.
- Download and Install Git.
- Download and Install Heroku CLI.
- Open the terminal window.
- Login into Heroku CLI. Run below command it will open Browser then click on Login:
6. Create a Git repository for your project:
7. Add all files to the git repository:
git add .
8. Commit all the changes:
git commit -m “any comment”
9. Create an app using the shell as follows:
heroku create heroku_app_name
10. Add Heroku remote for your project:
heroku git:remote -a heroku_app_name
11. Install gunicorn or waitress. This will be our production server as we can not use the development server which we were using by running the python manage.py runserver command. Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. For More information check out the official documentation.
pip install waitress
12. Run wsgi.py file using waitress to test everything works fine on the local machine (Before pushing your project to Heroku):
waitress-serve — port=8000 inner_project_folder_name.wsgi:application
13. You will get a link in the terminal just open it. If everything works then you will be able to see your project running on the Web Browser.
14. If you get an error: Disallowed Host at / or Invalid HTTP_HOST header then do the below change in Django’s settings.py file and re-run the wsgi.py file as we did in step 12
ALLOWED_HOSTS = [‘*’]
15. Create a file named Procfile and then write the below code in the file:
web: waitress-serve — port=8000 inner_project_folder_name.wsgi:application
16. Run the below command. This will use Procfile to run the project locally in the browser:
17. Now go to your Django project’s settings.py file and do the below changes:
DEBUG = False
ALLOWED_HOSTS = [‘heroku_app_name.herokuapp.com’, ‘localhost’]
Note: As you have created a Heroku App so you will be having your app URL which will look like this https://heroku_app_name.herokuapp.com/ You can find it by going to your Heroku’s Dashboard ->Your App -> Settings (Domain Section).
18. We will also create Config Var for Django Project’s secret key as follows:
=> Copy SECRET_KEY from Django’s settings.py file.
=> Now go to Heroku App Settings and then click Reveal Config vars and then write SECRET_KEY in KEY section and the actual key which looks like this section r6t3d0udsdsdew5656+u9d+%o#^uo0su-i3x3_5zs5–5r7r9a1_mhwfi!2b+^ in the VALUE section.
=> Click Add.
=> Go to the Django project settings.py file and do the changes as follows:
SECRET_KEY = os.environ[‘SECRET_KEY’]
19. If you have static files must include STATIC_ROOT in Django’s settings.py file as follows:
STATIC_ROOT = BASE_DIR / “static”
20. Install whitenoise by running the following command. WhiteNoise allows your web app to serve its own static files, making it a self-contained unit that can be deployed anywhere without relying on Nginx, Amazon S3, or any other external service. (Especially useful on Heroku, OpenShift, and other PaaS providers) For more information check out the official documentation.
pip install whitenoise
21. Open Django's settings.py file and Add Whitenoise Middleware as follows:
MIDDLEWARE = [
22. Bundle all the requirements for your project as follows:
pip freeze > requirements.txt
23. Make sure that you have changed web: waitress-serve — port=8000 inner_project_folder_name.wsgi:application to web: waitress-serve — port=$PORT inner_project_folder_name.wsgi:application in Procfile before pushing your app to Heroku.
24. Run the following commands and voila! 🎉
git add .
git commit -m “any comment”
git push heroku master
Solutions to some common error while deploying:
Error 1: django-assets rejected
Cause: You haven’t specified STATIC_ROOT
Solution: Either Provide STATIC_ROOT or disable it by running the following command in the terminal
heroku config:set DISABLE_COLLECTSTATIC=1
Error 2: No web processes running
Cause: Unable to find WSGI or haven't configured Procfile
Solution: Properly config Procfile
That’s it. BOOM. Your app is now online.