部署Django应用至Heroku时遭遇H10崩溃错误的排查求助
Hey there, let's break down how to fix that H10 crash you're seeing. Based on your directory structure and Procfile setup, the most likely culprit is an incorrect path to your WSGI module—let's tackle that first, then cover other common fixes.
1. Correct Your Procfile WSGI Path
Looking at your directory structure:
ROOT/ ├─ .git/ ├─ Procfile ├─ api_project/ │ ├─ api_project/ # Contains wsgi.py │ ├─ manage.py
Your current Procfile uses web: gunicorn api_project.wsgi, but from the ROOT directory (where Heroku runs commands), gunicorn can't find that WSGI module directly. Here are two working fixes:
Option A: Use --chdir to Point to the Django Project Root
Update your Procfile to first change directory to where manage.py lives, then reference the WSGI module:
web: gunicorn --chdir api_project api_project.wsgi
This tells gunicorn to switch into the api_project/ folder (with manage.py) first, then look for the inner api_project/wsgi.py module.
Option B: Use the Full Module Path from ROOT
If you don't want to use --chdir, you can reference the WSGI module using its full path from the ROOT directory. Since the inner api_project is a subfolder of the outer api_project, the module path becomes:
web: gunicorn api_project.api_project.wsgi
Just make sure the outer api_project folder has an empty __init__.py file (Django usually adds this automatically, but double-check—if it's missing, create it).
2. Verify Other Common H10 Crash Causes
Even with a fixed Procfile, these issues can still trigger an H10 error:
- Missing Dependencies: Ensure your
requirements.txtincludesgunicornand all other packages your Django app needs. Runpip freeze > requirements.txtlocally to update it, then commit and push to Heroku. - Incorrect Django Settings:
- Set
DEBUG = False(required for production on Heroku). - Add
.herokuapp.comtoALLOWED_HOSTSinsettings.py, like:ALLOWED_HOSTS = ['your-app-name.herokuapp.com', 'localhost']
- Set
- Unapplied Migrations: Run
heroku run python manage.py migrateto apply database migrations—missing migrations can cause the app to crash on startup. - Check for Hidden Errors: Sometimes the H10 log doesn't show full details. Run
heroku run python manage.py checkto scan for Django configuration issues, orheroku run bashto access the app's shell and debug directly (try runninggunicornmanually to see exact error messages).
3. Confirm Procfile Placement
You already fixed the H14 error by moving Procfile to the ROOT (with .git), which is correct—Heroku requires the Procfile to be in the root of your Git repository, so keep it there.
Once you make these changes, commit your updates and push to Heroku with git push heroku main, then check if the app starts up. If you still see errors, run heroku logs --tail again—you should now get more detailed error messages to debug further.
内容的提问来源于stack exchange,提问作者jokonoo

