Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3

: rocket: Purpose of this article

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 ...".

: rocket: What is Heroku?

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/ image.png

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

: rocket: Execution environment

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

: rocket: Building a WEB application environment

Create a web app that displays "Hello World ..." in Django. When the web app is complete, we'll release it to Heroku.

1. Install django-toolbelt

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. image.png

2. Create a Django project

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 ↓ image.png This completes the construction of the WEB application environment.

: rocket: Heroku account registration

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.

: rocket: Install Heroku CLI

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.

image.pngimage.png After downloading, unzip and put the path in the bin folder.

: rocket: Heroku login

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. image.png

: rocket: Create ssh key

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

: rocket: Required settings when deploying to Heroku

Some settings are required to deploy (release) a web application on Heroku.

1. Create a Procfile file

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

2. Create a runtime.txt file

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

3. Create a requirements.txt file

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.

: rocket: WEB application Git

1. Git initialization

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"

2. Source commit

(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.

: rocket: Deploy to Heroku (Release)

1. Creating an application

Create an application on Heroku with the following command. Execute it directly under [Project Folder]. heroku create image.png 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.

2. Deploy (release)

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 image.png

Execute deploy (release) with the following command. git push heroku master image.png

3. Check the screen display

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 image.png

It was displayed as above on the browser, and I was able to confirm the display of "Hello World Heroku Test!" Safely.

: rocket: Related information

: link: django-toolbelt details https://pypi.org/project/django-toolbelt/

: link: Heorku's book site https://jp.heroku.com/

: rocket: Opinions etc.

If you have any opinions or corrections, please let us know.

Recommended Posts

Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Getting Started with Python Django (5)
Hello World with Raspberry Pi + Minecraft Pi Edition
Django 1.11 started with Python3.6
1.1 Getting Started with Python
Getting Started with Python
Getting Started with Django 1
Getting Started with Python
Getting Started with Django 2
Getting started with AWS IoT easily in Python
Settings for getting started with MongoDB in python
Getting Started with Python Functions
Getting Started with Django with PyCharm
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Python starting with Hello world!
Python3 | Getting Started with numpy
Getting Started with Python responder v2
Use vl53l0x with Raspberry Pi (python)
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Python Basics of Python
[Note] Hello world output with python
cout << "Hello, World! \ N" in python
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Get started with Python in Blender
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Python Web Scraping Practice
Getting Started with Python for PHPer-Super Basics
Getting Started with Python Web Scraping Practice
Getting started with Dynamo from Python boto
Get Started with TopCoder in Python (2020 Edition)
Hello world instead of localhost in Django
How to display Hello world in python
Working with GPS on Raspberry Pi 3 Python
RabbitMQ message notification app in Python with Growl ~ with Raspberry Pi and Julius ~
Hello World and face detection with OpenCV 4.3 + Python
Hello World with gRPC / go in Docker environment
Discord bot with python raspberry pi zero with [Notes]
[Amateur remarks] Raspberry Pi 3, Wordpress vs Raspberry Pi 3, python, Django
MongoDB Basics: Getting Started with CRUD in JAVA
[Translation] Getting Started with Rust for Python Programmers
I tried L-Chika with Raspberry Pi 4 (Python edition)
Say hello to the world with Python with IntelliJ
Hello World with nginx + uwsgi + python on EC2
Django Getting Started Part 2 with eclipse Plugin (PyDev)
Get CPU information of Raspberry Pi with Python
Create a one-file hello world application with django
First python ① Environment construction with pythonbrew & Hello World !!
Django Getting Started Part 3 about Python3 & MySQL Connector
Materials to read when getting started with Python
Connect to MySQL with Python on Raspberry Pi
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Measure CPU temperature of Raspberry Pi with Python
I got an error when I put opencv in python3 with Raspberry Pi [Remedy]