Video Tutorial: https://youtu.be/_3AKAdHUY1M
- Create Heroku Account : https://www.heroku.com/
- Download and Install Git : https://git-scm.com/downloads
- Download and Install Heroku CLI : https://devcenter.heroku.com/articles/heroku-cli#download-and-install
- Open Terminal
- Login into Heroku CLI. Run below command it will open Browser then Click on Login
heroku login
- Create Repo for Project
git init
- Add All Files to Repo
git add .
- Commit All Changes
git commit -m "any comment"
- Create an App using Dashboard or Shell (I am creating using Shell)
heroku create heroku_app_name
- Set Repo
heroku git:remote -a heroku_app_name
- Install gunicorn or waitress - This will be our production server as we can not use development server which we were using by runing python manage.py runserver. Waitress is meant to be a production-quality pure-Python WSGI server with very acceptable performance. For More: https://docs.pylonsproject.org/projects/waitress/en/latest/
pip install waitress
- Run wsgi.py file using waitress to test everything works fine on Local Machine (Before Pushing to Heroku)
waitress-serve --port=8000 inner_project_folder_name.wsgi:application
- You will get a link in terminal just open it. If everything works then you will be able to see your project running on Web Browser
- If you get an error: Disallowed Host Invalid HTTP_HOST header then do below change in Django's Settings.py file and re-run wsgi.py file as Step 12
ALLOWED_HOSTS = ['*']
- Create a file named Procfile then write below code in the file
web: waitress-serve --port=8000 inner_project_folder_name.wsgi:application
if this don't work then try this one:
javascript web: waitress-serve --listen=127.0.0.1:8000 webapp.wsgi:application
-
Run below command - This will use Procfile to run the project. You will see an URL open it if everything file you will see project in browser
heroku local
-
Now go to your Django project's settings and do below change
DEBUG = False ALLOWED_HOSTS = ['heroku_app_name.herokuapp.com', 'localhost']
There is sometimes a error relating to CSRF, then try this one as well
CSRF_TRUSTED_ORIGINS = ['https://heroku_app_name.herokuapp.com']
As you have created an Heroku App so you have your app url e.g. https://heroku_app_name.herokuapp.com/ You can find it follwoing Heroku's Dashboard -> Setting
-
We will also create Config Var for Django Project's Secret Key by following
- Copy SECRET_KEY from Django's settings.py File
- Go to Heroku App Setting then click Reveal Config vars then write
SECRET_KEY r6t3d0udsdsdew5656+u9d+%o#^uo0su-i3x3_5zs5-5r7r9a1_mhwfi!2b+^
- Click Add
- Go to Django Project Settings.py and do below changes
import os SECRET_KEY = os.environ['SECRET_KEY']
-
If you have static files must include STATIC_ROOT in Django's settings.py file
STATIC_ROOT = BASE_DIR / "static"
-
Install whitenoise - 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: http://whitenoise.evans.io/en/stable/
pip install whitenoise
-
Open Django's settings.py file and Add Whitenoise Middleware
MIDDLEWARE = [ # 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # ... ]
-
Bundle all requirements
pip freeze > requirements.txt
-
Make sure 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 to heroku
if this don't work then try this one:
javascript web: waitress-serve --listen=*:$PORT webapp.wsgi:application
- Run below command
git add . git commit -m "any comment" git push heroku master
- Done
Error 1: django-assets rejected
Cause: You haven't specified STATIC_ROOT
Solution: Either Provide STATIC_ROOT or Disable it by running heroku config:set DISABLE_COLLECTSTATIC=1
Error 2: No web processes running
Cause: Unable to find wsgi or havent configured Procfile
Solution: Config Properly Procfile