deploy django project with heroku

우진·2024년 7월 26일

step1. Prepare Django Project for Deployment

1. Install Gunicorn:

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

2. Create a Procfile:

The Procfile tells Heroku how to run your application.

echo "web: gunicorn myproject.wsgi" > Procfile

3. Create a requirements.txt file:

List all your Python dependencies. You can generate this file using pip.

pip freeze > requirements.txt

4. Add runtime.txt file:

Specify the Python version.

echo "python-3.9.1" > runtime.txt

5. Update settings.py:

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)
}

6. Static Files:

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',
    # ...
]

Step 2: Deploy to Heroku

1. Install the Heroku CLI:

Follow the instructions on the Heroku CLI installation page.

2. Log in to Heroku:

Log in to your Heroku account using the CLI.

heroku login

3. Create a Heroku App:

Create a new app on Heroku.

heroku create myappname

4. Deploy Your Code:

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

5. Run Migrations:

Run your Django migrations on Heroku.

heroku run python manage.py migrate

6. Create a Superuser:

Create a superuser to access the Django admin interface.

heroku run python manage.py createsuperuser

7. Open Your App:

Open your deployed app in the web browser.

heroku open

Step 3: Additional Configuration

1. Domain Configuration:

If you have a custom domain, you can add it to your Heroku app.

heroku domains:add www.yourdomain.com

2. Monitoring:

Use Heroku's monitoring tools to keep an eye on the performance and health of your application.

heroku logs --tail

3. How Do I Shut Down the Server?

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

4. How to Manage Server Usage and Fees

Heroku Dashboard:

Go to the Heroku Dashboard.
Log in with your Heroku credentials.
Select your application to view its details, usage, and metrics.

❌ Error

1. cannot parse procfile

in heroku push process, error cause.

cause : pycharm editor don't support utf-8 encoding.

solution : edit procfile in vscode. set utf-8, LF.

2. heroku system path error

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.

3. Requested runtime '?ython-3.9.1' is not available for this stack (heroku-22).

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.

0개의 댓글