What should go in my Procfile for a Django application?

Viewed 2720

What should go in my Procfile for a Django application on Heroku?

I tried:

web: python appname.py

because I found an example like that for python apps.

Further searching didn't make things any clearer except for that I might need to use gunicorn instead of python. I found various posts suggesting various formats such as:

web gunicorn
web:gunicorn
web: gunicorn

I have no clue what should come after gunicorn, some posts have the programming language, some have an IP address, some have various other things.

Some suggest running:

heroku ps:scale web=1

but that results in an error:

Scaling dynos... !
 !    Couldn't find that process type (web).

I just haven't got a clue and don't know where to turn.

Since posting I have watched some videos about this and tried:

web: gunicorn appname.wsgi

in my Procfile but it still doesn't work, still resulting in:

at=error code=H14 desc="No web processes running"
2 Answers

You will need 3 files in order to successfully deploy a django app to Heroku.

  • Procfile
  • runtime.txt
  • requirements.txt

Those 2 modules should be in your requirements.txt

In Procfile, type

release: python manage.py migrate
web: gunicorn yourprojectname.wsgi
  • The first line explains the type of the deploy release which means a production release, followed by migrate which I supposed you know what will do.

  • The second line explains that Gunicorn is the Python WSGI HTTP Server for UNIX

  • In runtime.txt type your python version like this

    python-3.9.6

You can see which python version you have with this terminal command python --version

  • And finally you will need requirements.txt, you can generate it with pip freeze > requirements.txt while venv is activated in case you are operating from virtual environment.

Project Launch Steps, the easiest way:

  • Deactivate your venv in case there is any
  • Go to heroku dashboard
  • Create an app and chose a meaningful name and free plan which will allows you to run 1 worker for free
  • copy its url: herokuappname.herokuapp.com then in settings.py, paste it in ALLOWED_HOSTS = ['herokuappname.herokuapp.com']
  • Set DEBUG = False, debug shouldn't be allowed in production environment
  • Then in your terminal navigate to project folder and type the following commands one by one
  • Heroku login # to login to your Heroku account
  • heroku git:remote -a yourAppname # to connect to your already created app
  • git init # Initialize your repo
  • git add . # add all project's files to the initialized repo
  • git commit -m "first push" # commit
  • git push heroku master # push project files to the remote Heroku app repo

After successful deploy, type - enter

  • heroku logout # to logout

Which storage are you going to use?

Because Heroku does not host static files.

  • You can use Azure storage for free. Full tutorial here

Here's a project model of mine deployed to Heroku with azure storage, you will find all needed details.

Heroku's Procfile format is quite simple. As described in the documentation:

A Procfile declares its process types on individual lines, each with the following format:

<process type>: <command>

You can see that there should be a colon after the process type, so the

web gunicorn

example in your question is not going to work properly. You'll want to start the line with web:.

<command> indicates the command that every dyno of the process type should execute on startup, such as rake jobs:work

For Django, in development you'd typically use python manage.py runserver to run the application, so a reasonable attempt for Django would be

web: python manage.py runserver

This should work, but it's not appropriate for production work:

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)

Instead, you should use a production-grade web server in production. Gunicorn is a common choice, and you can run your Django application with Gunicorn like so:

gunicorn myproject.wsgi

Putting that all together, a Procfile for Django on Heroku might look like

web: gunicorn myproject.wsgi

where myproject is the name of your Django project. This is exactly what Heroku's documentation suggests for Django applications.

Note that you'll have to add Gunicorn to your project dependencies so Heroku will install it. I recommend also installing it locally so you can use heroku local to test your application on your dev machine in a way more similar to Heroku's production environment.

heroku ps:scale is used to change the number and type of dynos for process types you have already defined. It has nothing to do with defining those process types. That's what your Procfile is for.

Related