What should go in my Procfile for a Django application?
Asked Answered
D

2

6

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"
Disingenuous answered 17/10, 2021 at 15:3 Comment(2)
This reads as a rant, not a question. I understand that you're frustrated, but please leave the attitude behind when posting here. See How to Ask.Safeguard
(FWIW, I actually think Heroku's documentation is quite good.)Safeguard
S
1

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.

Safeguard answered 17/10, 2021 at 15:59 Comment(9)
I read the documentation and I have to disagree with you about its quality. I really didn't help me in the least, not a rant, just didn't help me at all. Nevertheless, thank you for responding, I appreciate it! I tried adding web: gunicorn myproject.wsgi but still have the same problem. Trying heroku local was a great idea! Apparently heroku doesn't see my Procfile but I don't understand why. It is in my root directory, in the git repository where I pushed from, I named it "Procfile", not "Procfile.txt". Any idea what might be causing that?Disingenuous
@VaultDweller, is it UTF-8 encoded? Assuming you're on Windows, are you entirely sure it is named Procfile and not Procfile.txt? By default, Windows hides common file extensions in the file explorer. Are you able to share a link to your repository?Safeguard
I assume it is UTF-8 encoded, I didn't change any settings and yes I'm on Windows. I made sure to name it "Procfile" and not "Procfile.txt" because the instructions I followed explicitly stated that it should be a .txt file but the extension shouldn't be included when naming it. Here is the repository: github.com/DrJekl/store.git Please let me know when you've had a look because I would like to set it back to private when you're done. Thanks for your help!Disingenuous
@VaultDweller, your Procfile is called Procfile.txt. GitHub shows the real name. Try git mv Procfile.txt Procfile, then git commit -m "Fix Procfile name", then git push.Safeguard
The contents of the file appear to be correct. Renaming it will probably solve your problem.Safeguard
That fixed it, thank you! Now I get a 500 error though, is it possible I need to do the same to my requirements file? Running heroku local got me an error saying: No module named 'fcntl'. I have never heard of that module before but maybe it is part of django or gunicorn or something. Should I include it in my requirements?Disingenuous
web: python manage.py runserver doesn't work for me. After I debug it, turns out that this command will run the Django on port 8000. AFAIK, to deploy on heroku, we must host it on a particular port defined in the PORT env variable (CMIIW).Buckley
@Hzzkygcs, as noted in my answer, you shouldn't be using manage.py runserver on Heroku in the first place: "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.)" Use a production-grade WSGI server instead. Gunicorn is common, and used in the question and this answer.Safeguard
But yes, you don't get to choose your port on Heroku. See https://mcmap.net/q/1777352/-heroku-port-variable-in-procfileSafeguard
O
5

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.

Oklahoma answered 17/10, 2021 at 23:37 Comment(2)
This is far too much information for what OP is asking. I appreciate you are trying to help, but this could be very confusing for some. Your answer describes an entire Heroku deploy solution instead of simply answering the main question. Please consider a shorter edit.Elimination
It seems that runtime.txt is (or will be) deprecated, use .python-version instead. devcenter.heroku.com/articles/python-runtimesFlyn
S
1

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.

Safeguard answered 17/10, 2021 at 15:59 Comment(9)
I read the documentation and I have to disagree with you about its quality. I really didn't help me in the least, not a rant, just didn't help me at all. Nevertheless, thank you for responding, I appreciate it! I tried adding web: gunicorn myproject.wsgi but still have the same problem. Trying heroku local was a great idea! Apparently heroku doesn't see my Procfile but I don't understand why. It is in my root directory, in the git repository where I pushed from, I named it "Procfile", not "Procfile.txt". Any idea what might be causing that?Disingenuous
@VaultDweller, is it UTF-8 encoded? Assuming you're on Windows, are you entirely sure it is named Procfile and not Procfile.txt? By default, Windows hides common file extensions in the file explorer. Are you able to share a link to your repository?Safeguard
I assume it is UTF-8 encoded, I didn't change any settings and yes I'm on Windows. I made sure to name it "Procfile" and not "Procfile.txt" because the instructions I followed explicitly stated that it should be a .txt file but the extension shouldn't be included when naming it. Here is the repository: github.com/DrJekl/store.git Please let me know when you've had a look because I would like to set it back to private when you're done. Thanks for your help!Disingenuous
@VaultDweller, your Procfile is called Procfile.txt. GitHub shows the real name. Try git mv Procfile.txt Procfile, then git commit -m "Fix Procfile name", then git push.Safeguard
The contents of the file appear to be correct. Renaming it will probably solve your problem.Safeguard
That fixed it, thank you! Now I get a 500 error though, is it possible I need to do the same to my requirements file? Running heroku local got me an error saying: No module named 'fcntl'. I have never heard of that module before but maybe it is part of django or gunicorn or something. Should I include it in my requirements?Disingenuous
web: python manage.py runserver doesn't work for me. After I debug it, turns out that this command will run the Django on port 8000. AFAIK, to deploy on heroku, we must host it on a particular port defined in the PORT env variable (CMIIW).Buckley
@Hzzkygcs, as noted in my answer, you shouldn't be using manage.py runserver on Heroku in the first place: "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.)" Use a production-grade WSGI server instead. Gunicorn is common, and used in the question and this answer.Safeguard
But yes, you don't get to choose your port on Heroku. See https://mcmap.net/q/1777352/-heroku-port-variable-in-procfileSafeguard

© 2022 - 2024 — McMap. All rights reserved.