Gunicorn is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model, which makes it very efficient for handling multiple requests in parallel.
pip install gunicorn
The Procfile tells Heroku how to run your application.
echo "web: gunicorn myproject.wsgi" > Procfile
List all your Python dependencies. You can generate this file using pip.
pip freeze > requirements.txt
Specify the Python version.
echo "python-3.9.1" > runtime.txt
Set DEBUG to False.
Allow all hosts (or specify your domain).
DEBUG = False
ALLOWED_HOSTS = ['*']
Update your database configuration to use an environment variable. Heroku provides a DATABASE_URL environment variable.
import dj_database_url
DATABASES = {
'default': dj_database_url.config(conn_max_age=600, ssl_require=True)
}
Configure your static files to be served correctly.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
Install whitenoise to help serve static files on Heroku.
pip install whitenoise
Add whitenoise to your MIDDLEWARE settings in settings.py.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
Follow the instructions on the Heroku CLI installation page.
Log in to your Heroku account using the CLI.
heroku login
Create a new app on Heroku.
heroku create myappname
Initialize a git repository (if you haven't already), add your files, commit, and push to Heroku.
git init
git add .
git commit -m "Initial commit"
git push heroku master
Run your Django migrations on Heroku.
heroku run python manage.py migrate
Create a superuser to access the Django admin interface.
heroku run python manage.py createsuperuser
Open your deployed app in the web browser.
heroku open
If you have a custom domain, you can add it to your Heroku app.
heroku domains:add www.yourdomain.com
Use Heroku's monitoring tools to keep an eye on the performance and health of your application.
heroku logs --tail
heroku ps:scale web=0 -a your-app-name
To Restart the Server, Scale the Web Dyno Back Up:
heroku ps:scale web=1 -a your-app-name
Heroku Dashboard:
Go to the Heroku Dashboard.
Log in with your Heroku credentials.
Select your application to view its details, usage, and metrics.
in heroku push process, error cause.
cause : pycharm editor don't support utf-8 encoding.
solution : edit procfile in vscode. set utf-8, LF.
heroku : 'heroku' 용어가 cmdlet, 함수, 스크립트 파일 또는 실행할 수 있는 프로그램 이름으로 인식되지 않습니다. 이름이 정확한지 확인하고 경로가 포함된 경우 경로가
올바른지 검증한 다음 다시 시도하십시오.
위치 줄:1 문자:1
+ heroku login
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (heroku:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
solution : set heroku install path to system path. need to restart cmd and pycharm.
in heroku push process, error cause.
cause 1 : python version is to low.
cause 2 : pycharm editor don't support utf-8 encoding.
solution 1 : upgrade python to 3.12.4
solution 2 : edit runtime.txt in vscode. set utf-8, LF.