[PYTHON] What I found by deploying Django on EC2

Introduction

Until now, I used Heroku as the cloud platform, but since I had the opportunity to migrate to EC2, I summarized the solution that I stumbled upon during the migration work and points to note when deploying web applications on EC2. It was.

This time, the following directory structure is assumed.

root
├── etc
│   └── systemd
│       └── system
│           └── sampled.service
└── home
    └── ubuntu
        └── sample
            └── sample.py

It is a Python program that outputs "OK" every minute.

sample.py


#!/usr/bin/env python3
import syslog
from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler()

@sched.scheduled_job("interval", minutes=1)
def sample():
    syslog.syslog("OK")

sched.start()

Digression: I couldn't create an AWS account right away

In my case, I got an error in the phone authentication and could not proceed, so I made a support case and asked for support.

After all, it took almost a week for the account to be available by telephone from the call center, so if you get an error, make a support case and have it dealt with immediately!

Precautions when creating an EC2 instance

First, I will write about the points to note when creating an EC2 instance.

When creating an EC2 instance, you select the AMI to use, but basically it is better to select the Amazon Linux system, but this will change the way to create the daemon process that will be introduced later.

So when deploying while referring to some site, first check which AMI you are using before proceeding!

Precautions when making a daemon

The next point to note when making a program into a daemon.

As I wrote earlier, the method of making will change depending on the AMI used.

Since Ubuntu 20.04 was used this time, the service configuration file for daemonizing the program was in the following location.

Depends on the AMI used
Ubuntu: /etc/systemd/system/sampled.service

Daemon environment variables

The next point to note when handling environment variables in the daemon.

The environment variable setting of the daemon was the most stumbling point when proceeding.

Basically, there is no problem if you type the following command to run the program from the terminal.

$ SAMPLE=123456789
$ export SAMPLE
$ echo $SAMPLE
>>> 123456789

If you want to make environment variables persistent

sudo vi /etc/profile

If you list the environment variables you want to set in as follows, they will be applied from the next startup.

/etc/profile


SAMPLE=123456789
export SAMPLE

However!!

The daemon does not read these environment variables <<< important here !!

So let's create an environment variable setting dedicated to the daemon as follows

/etc/sysconfig/sampled_env


SAMPLE=123456789

Also, add the specified environment variable to the service settings so that the daemon can read it.

/etc/systemd/system/sampled.service


'''
abridgement
'''
[Service]
#Postscript
EnvironmentFile=/etc/sysconfig/sampled_env
'''
abridgement
'''

After that, type the following command to reload the service settings and restart the daemon.

$ sudo systemctl daemon-reload
$ sudo systemctl restart sampled

After that, type the following command and if the Active item is active (running), it is successful.

$ sudo systemctl status sampled 
>>> sampled.service - sampled daemon
>>>   Loaded: loaded (/lib/systemd/system/sampled.service; enabled; vendor preset: enabled)
>>>   Active: active (running)
>>> '''
>>>abridgement
>>> '''

If you want to check the program log, you can check it with the following command!

$ sudo journalctl -u sampled
>>> Jul 29 17:50:24 ip-Omitted sampled.py[abridgement]: OK
>>> Jul 29 17:51:24 ip-Omitted sampled.py[abridgement]: OK

File permission settings

Next, there are some notes related to file permissions.

I stumbled because I wasn't properly setting file permissions when daemonizing the program.

If you type the following command as before and the Active item is failed (Result: exit-code), suspect a permission error.

$ sudo systemctl status sampled 
>>> sampled.service - sampled daemon
>>>   Loaded: loaded (/lib/systemd/system/sampled.service; enabled; vendor preset: enabled)
>>>   Active: failed (Result: exit-code)
>>> '''
>>>abridgement
>>> '''

First, if a Permission denied error appears, move to the directory containing the file you want to daemonize and type the following command.

~/sampled


$ ls -l
>>> -rw-r--r-- 1 root  root  1052 Jul 29 17:16 sample.py

Check the access authority of the file displayed by the command, and if it is different from the User and Group described in the service setting file of the daemon, enter the following command.

/etc/systemd/system/sampled.service


'''
abridgement
'''
[Service]
User=sample
Group=sample-group
'''
abridgement
'''

With the above settings

#Change file permissions
$ sudo chown sample:sample-group /home/ubuntu/sample/sample.py
$ sudo chmod 755 /home/ubuntu/sample/sample.py

#Confirmation of file permission change
$ ls -l
>>> -rwxr-xr-x 1 sample  sample-group  1052 Jul 29 17:16 sample.py

#Daemon restart
$ sudo systemctl restart sampled

#Daemon operating status display
$ systemctl status sampled
>>> sampled.service - sampled daemon
>>>   Loaded: loaded (/lib/systemd/system/sampled.service; enabled; vendor preset: enabled)
>>>   Active: active (running)
>>> '''
>>>abridgement
>>> '''

If the Active item is now activr (running), you're done !!

in conclusion

It was difficult until I could do various things because I don't usually use Linux, but it will be very convenient if I can master it, so I will continue to learn!

After all AWS is amazing

Thank you for visiting.

Recommended Posts

What I found by deploying Django on EC2
What I did when I stumbled on a Django tutorial
What I learned at hackerrank on 1/30 days.
I made a webAPI! Build environment from Django Rest Framework 1 on EC2
I tried to reduce costs by starting / stopping EC2 collectively on AWS Lambda
What Python beginners got hooked on with Django
What is Django? .. ..
To you who are cornered by deploying Django. Django 2.2 deployment full version on Ubuntu 18.04 + Nginx
I tried Django
Memo of deploying Django × Postgresql on Docker to Heroku
Miscellaneous notes about deploying the django app on Heroku
What I learned by participating in the ISUCON10 qualifying