정리 요망
Heroku
You can sign up for a free Heroku account on their website. After you confirm your email Heroku will redirect you to the dashboard section of the site.
Heroku dashboard
Now we need to install Heroku’s Command Line Interface (CLI) so we can deploy from the command line. We want to install Heroku globally so it is available across our entire computer. Open up a new command line tab: Command+t on a Mac, Control+t on Windows. If we installed Heroku within our virtual environment, it would only be available there.
Within this new tab, on a Mac use Homebrew to install Heroku:
Command Line
SHAPE \* MERGEFORMAT
$ brew install heroku/brew/heroku
SHAPE \* MERGEFORMAT
On Windows, see the Heroku CLI page to correctly install either the 32-bit or 64-bit version. If you are using Linux there are specific install instructions available on the Heroku website.
Once installation is complete you can close our new command line tab and return to the initial tab with the pages virtual environment active.
Type the command heroku login and use the email and password for Heroku you just set.
Command Line
SHAPE \* MERGEFORMAT
(pages) $ heroku login
Enter your Heroku credentials: Email: will@wsvincent.com
Password: ********************************* Logged in as will@wsvincent.com
Additional Files
We need to make the following four changes to our Pages project so it’s ready to deploy online with Heroku:
• update Pipfile.lock
• make a new Procfile file
• install Gunicorn as our web server
• make a one-line change to settings.py file
Within your existing Pipfile specify the version of Python we’re using, which is 3.7. Add these two lines at the bottom of the file.
Pipfile
SHAPE \* MERGEFORMAT
[requires] python_version = "3.7"
Then run pipenv lock to generate the appropriate Pipfile.lock.
Command Line
SHAPE \* MERGEFORMAT
(pages) $ pipenv lock
SHAPE \* MERGEFORMAT
Heroku actually looks in our Pipfile.lock for information on our virtual environment, which is why we add the language setting here.
Next create a Procfile, which is configuration file specific to Heroku.
Command Line
SHAPE \* MERGEFORMAT
(pages) $ touch Procfile
SHAPE \* MERGEFORMAT
Open the Procfile with your text editor and add the following:
Procfile
SHAPE \* MERGEFORMAT
web: gunicorn pages_project.wsgi --log-file -
SHAPE \* MERGEFORMAT
This says to use Gunicorn, which is a web server suitable for production, instead of Django’s own server which is only suitable for local development. Install it using Pipenv.
Command Line
SHAPE \* MERGEFORMAT
(pages) $ pipenv install gunicorn==19.9.0
SHAPE \* MERGEFORMAT
The configuration for the server is contained in a wsgi.py file that Django automati- cally creates for every new project. It resides at the top-most, project level of our code. Since our project’s name is pages_project the file is located at pages_project/wsgi.py file.
The final step is a one-line change to settings.py. Scroll down to the section called
ALLOWED_HOSTS and add a '*' so it looks as follows:
Code
SHAPE \* MERGEFORMAT
# pages_project/settings.py ALLOWED_HOSTS = ['*']
The ALLOWED_HOSTS setting represents which host/domain names our Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations. However we’ve used the wildcard asterisk, *, which means all domains are acceptable to keep things simple. In a production-level Django site you would explicitly list which domains were allowed instead!
Use git status to check our changes, add the new files, and then commit them:
Command Line
SHAPE \* MERGEFORMAT
(pages) $ git status (pages) $ git add -A
(pages) $ git commit -m "New updates for Heroku deployment"
SHAPE \* MERGEFORMAT
Finally push to GitHub so we have an online backup of our code changes.
Command Line
SHAPE \* MERGEFORMAT
(pages) $ git push -u origin master
SHAPE \* MERGEFORMAT
'DoItDJango' 카테고리의 다른 글
텍스트 콘텐츠를 저장하는 모델 만들기 (0) | 2023.04.14 |
---|---|
Django Admin 사용 (0) | 2023.04.14 |
template 사용 - class기반 뷰어 생성 (0) | 2023.04.13 |
데이터 저장 (0) | 2023.04.13 |
URL 별칭 (0) | 2023.04.13 |
댓글