Released the simplest application created by Django, a Python WEB server, on ** Heroku **, which is one of ** PaaS ** (Platform as a Service), and displayed "Hello" on the screen. Review the steps to display "World ...".
Heroku is one of the most well-known PaaS, along with Salesforce.com's Force.com and Google's Google App Engine.
: link: Heorku's book site https://jp.heroku.com/
With Heroku, you can release and operate customized applications without having to worry about server procurement, setup, network construction, and infrastructure environment maintenance such as server maintenance.
Applications deployed on Heroku run in completely independent modules called ** Dyno ** and automatically scale in (decrease the number of dynos) and scale out (increase the number of dynos). It is possible to.
In addition, load distribution and routing processing are automatically performed according to the increase or decrease in the number of Dyno, and the user does not need to be aware of the settings and operations.
Heroku has many other features as well. Please refer to this Heroku site for details.
: moneybag: ** Free usage range ** Heroku has a free trial plan. -RAM capacity limit: up to 512MB -Number of concurrent Dynas: Up to 2 ・ Go to sleep after idling for 30 minutes ・ Dyno usage time per month: up to 1,000 hours
This time, we'll build a Django app on the Raspberry PI 3 and release it to Heroku. The details of the execution environment (WEB application construction environment) are as follows.
: wrench: ** Details of WEB application construction environment ** ・ Raspberry PI 3 model B (Memory 1GB) -OS version: Raspbian GNU / Linux 10 (buster) -Kernel version: Linux raspberrypi 4.19.97-v7 + -Python 3.7.3 ・ Git Version 2.20.1
Create a web app that displays "Hello World ..." in Django. When the web app is complete, we'll release it to Heroku.
Install ** django-toolbelt **. django-toolbelt is a library provided by PyPI, which is a package that contains a set of functions required for execution on Heroku. Install with pip install.
: link: django-toolbelt details https://pypi.org/project/django-toolbelt/
Execute the following command.
pip install django-toolbelt
It is OK when the following is displayed. The name of the installed software is displayed.
Create a Django project. The creation procedure is the same as the general Django procedure. This time, I proceeded with the following procedure.
(1) Set the path for the django-admin command
export PATH=~/.local/bin:$PATH
(2) Execute the django-admin command
django-admin startproject herokutest
The project folder "herokutest" is created.
(3) Edit settings.py
Modify the following part of settinigs.py.
(File location: [project folder] /herokutest/settings.py)
settings.py
ALLOWED_HOSTS = ['*']
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
(4) Creating a hello application
(Run in the created project folder heroku)
python manage.py startapp hello
(5) Editing views.py
Set views.py as follows.
(File location: in the created hello folder)
views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World Heroku Test!')
(6) Editing urls.py Set urls.py as follows. (File location: [project folder] /herokutest/urls.py)
urls.py
from django.contrib import admin
from django.urls import path
import hello.views as hello
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', hello.index),
]
(7) Check the screen display
Execute the following command to access the local environment with a browser and check the screen display. (The command is executed directly under [Project Folder])
python manage.py runserver
Local environment: localhost: 8000 / hello /
Screen display result ↓ This completes the construction of the WEB application environment.
Register for a new account on the Heroku site. Please enter your email address and password to register for an account. The procedure is omitted here.
To use the Heroku command, install the Heroku CLI. Download from the following site.
:link: https://devcenter.heroku.com/articles/heroku-cli
For Raspberry PI, download from Linux (arm) in the red line in the figure below.
↓ After downloading, unzip and put the path in the bin folder.
Log in to Heroku with the following command.
heroku login
The browser will open and the login button will be displayed. Please log in.
If the screen below is displayed, login is successful.
Here's how to create a new SSH key that distinguishes it from other keys for individual use on Heroku. The location of the SSH key to be created is ~ / herokukeys /.
Follow the steps below to create an SSH key.
(1) Create a folder to store the SSH key.
mkdir ~/herokukeys
(2) Create an SSH key pair.
ssh-keygen -f ~/herokukeys/id_rsa
A private key (id_rsa) and public key (id_rsa.pub) are created in the herokukeys folder.
(3) If you set a passphrase when creating the SSH key, you will be prompted to enter it when connecting. To omit this, execute the following command.
ssh-add ~/herokukeys/id_rsa
(4) Register the created SSH public key.
heroku keys:add ~/herokukeys/id_rsa.pub
(5) Make the following settings to use the SSH key created individually on the Heroku site.
File to be set: ~ / .ssh / config
~/.ssh/config
Host heroku
HostName heroku.com
User git
IdentityFile /home/pi/herokukeys/id_rsa
Some settings are required to deploy (release) a web application on Heroku.
Create a Procfile with the following command so that Heroku runs Web Dyno, which is a Dyno for the web. The location to create is directly under [Project Folder].
echo "web: gunicorn herokutest.wsgi --log-file -" > Procfile
Describes the version of Python to execute. As shown below, the characters in python are in lowercase and are described with a hyphen. The location to create is directly under [Project Folder].
echo "python-3.7.3" > runtime.txt
A file that defines the libraries needed to run Python. Heroku will look at this and install the required libraries. The location to create is directly under [Project Folder].
This time, the contents are as follows.
requirements.txt
asgiref==3.2.10
dj-database-url==0.5.0
dj-static==0.0.6
Django==3.1.2
django-toolbelt==0.0.1
gunicorn==20.0.4
psycopg2==2.8.6
pytz==2020.1
sqlparse==0.4.1
static3==0.7.0
: bulb: ** Tips on how to create requirements.txt **
The requirements.txt file can be created with the following command.
pip freeze > requirements.txt
The above file extracted only what I needed after running this command.
Make the initial settings of Git. Please use user.email as the email address registered on Heroku.
python
git config --global user.email "mail address"
git config --global user.name "User name"
(1) Initialize the Git repository.
Execute it directly under [Project Folder].
git init
(2) Commit the source.
git add .
git commit -m" (set commit comment) "
It's OK if the list of committed sources is displayed on the screen.
Create an application on Heroku with the following command.
Execute it directly under [Project Folder].
heroku create
Heroku automatically assigns the application name.
After the release is successful, to access the WEB application, access the given URL (in the paid version, you can acquire your own domain and replace it).
In the above, mysterious-citadel-41347 was given as the application name.
Adjust the Django source before deploying.
This time you need to set that you are not using Static Assets.
Execute the following command.
heroku config:set DISABLE_COLLECTSTATIC=1
Execute deploy (release) with the following command.
git push heroku master
If the deployment is successful without any errors, check the screen.
You can start the WEB application with the "heroku open" command, but this time it is necessary to add hello / to the end of the URL, so enter the URL directly in the browser to confirm.
URL: ** https: // (application name) .herokuapp.com/hello/**
Screen display result
It was displayed as above on the browser, and I was able to confirm the display of "Hello World Heroku Test!" Safely.
: link: django-toolbelt details https://pypi.org/project/django-toolbelt/
: link: Heorku's book site https://jp.heroku.com/
If you have any opinions or corrections, please let us know.
Recommended Posts