How to deploy Python Application on AWS EC2 instance. ~Part1~

First of all

I guess there are many engineers who feel that it is a little hassle to release your application to the Internet user. For such people, let me share how to deploy your application to AWS EC2 instance. I would be glad if it facilitate your development life.

Environment Information

OS:CentOS Linux 7 x86_64
Web:Nginx 1.20.0
AP:Gunicorn
DB:PostgreSQL 9.2.24
Python:Python 3.6.8

Development Procedure

Firstly, Install necessary packages on AWS EC2 instances. Let me omit the procedure how to prepare AWS EC2 instances. Below is the result of SSH login.

[root@ip-172-31-47-96 ~]# yum install -y python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx[/terminal]
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirror.centos.org
* extras: mirror.centos.org
* updates: mirror.centos.org
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 236 kB 00:00:00
(3/4): updates/7/x86_64/primary_db | 8.0 MB 00:00:00
(4/4): base/7/x86_64/primary_db | 6.1 MB 00:00:00
No package python3-dev available.
No package libpq-dev available.
No package nginx available.
Resolving Dependencies
–> Running transaction check一部省略Installing:
postgresql x86_64 9.2.24-4.el7_8 base 3.0 M
postgresql-contrib x86_64 9.2.24-4.el7_8 base 552 k
python3-pip noarch 9.0.3-8.el7 base 1.6 M
Installing for dependencies:
libxslt x86_64 1.1.28-6.el7 base 242 k
postgresql-libs x86_64 9.2.24-4.el7_8 base 234 k
python3 x86_64 3.6.8-18.el7 updates 70 k
python3-libs x86_64 3.6.8-18.el7 updates 6.9 M
python3-setuptools noarch 39.2.0-10.el7 base 629 k
uuid x86_64 1.6.2-26.el7 base 55 k
Transaction Summary
=====================================================================================================================================================================================================
Install 3 Packages (+6 Dependent packages)
Total download size: 13 M
Installed size: 66 M
Is this ok [y/d/N]:y
Downloading packages:
warning: /var/cache/yum/x86_64/7/updates/packages/python3-3.6.8-18.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for python3-3.6.8-18.el7.x86_64.rpm is not installed
(1/9): python3-3.6.8-18.el7.x86_64.rpm | 70 kB 00:00:00
Public key for libxslt-1.1.28-6.el7.x86_64.rpm is not installed
(2/9): libxslt-1.1.28-6.el7.x86_64.rpm | 242 kB 00:00:00
(3/9): postgresql-contrib-9.2.24-4.el7_8.x86_64.rpm | 552 kB 00:00:00
(4/9): postgresql-libs-9.2.24-4.el7_8.x86_64.rpm | 234 kB 00:00:00
(5/9): python3-setuptools-39.2.0-10.el7.noarch.rpm | 629 kB 00:00:00
(6/9): python3-pip-9.0.3-8.el7.noarch.rpm | 1.6 MB 00:00:00
(7/9): uuid-1.6.2-26.el7.x86_64.rpm | 55 kB 00:00:00
(8/9): postgresql-9.2.24-4.el7_8.x86_64.rpm | 3.0 MB 00:00:00
(9/9): python3-libs-3.6.8-18.el7.x86_64.rpm | 6.9 MB 00:00:00
—————————————————————————————————————————————————————————————————–
Total 25 MB/s | 13 MB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : “CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>”
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-7.1908.0.el7.centos.x86_64 (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : python3-libs-3.6.8-18.el7.x86_64 1/9
Installing : python3-setuptools-39.2.0-10.el7.noarch 2/9
Installing : python3-pip-9.0.3-8.el7.noarch 3/9
Installing : python3-3.6.8-18.el7.x86_64 4/9
Installing : postgresql-libs-9.2.24-4.el7_8.x86_64 5/9
Installing : postgresql-9.2.24-4.el7_8.x86_64 6/9
Installing : uuid-1.6.2-26.el7.x86_64 7/9
Installing : libxslt-1.1.28-6.el7.x86_64 8/9
Installing : postgresql-contrib-9.2.24-4.el7_8.x86_64 9/9
Verifying : postgresql-9.2.24-4.el7_8.x86_64 1/9
Verifying : python3-3.6.8-18.el7.x86_64 2/9
Verifying : python3-libs-3.6.8-18.el7.x86_64 3/9
Verifying : postgresql-contrib-9.2.24-4.el7_8.x86_64 4/9
Verifying : libxslt-1.1.28-6.el7.x86_64 5/9
Verifying : python3-setuptools-39.2.0-10.el7.noarch 6/9
Verifying : uuid-1.6.2-26.el7.x86_64 7/9
Verifying : postgresql-libs-9.2.24-4.el7_8.x86_64 8/9
Verifying : python3-pip-9.0.3-8.el7.noarch 9/9
Installed:
postgresql.x86_64 0:9.2.24-4.el7_8 postgresql-contrib.x86_64 0:9.2.24-4.el7_8 python3-pip.noarch 0:9.0.3-8.el7
Dependency Installed:
libxslt.x86_64 0:1.1.28-6.el7 postgresql-libs.x86_64 0:9.2.24-4.el7_8 python3.x86_64 0:3.6.8-18.el7 python3-libs.x86_64 0:3.6.8-18.el7 python3-setuptools.noarch 0:39.2.0-10.el7
uuid.x86_64 0:1.6.2-26.el7
Complete!
[root@ip-172-31-47-96 ~]#

postgresql and python seem to be installed successfully.
You can check if the installation was succeeded by executing bellow command.

[root@ip-172-31-47-96 ~]# python3 –version
Python 3.6.8
[root@ip-172-31-47-96 ~]# psql –version
psql (PostgreSQL) 9.2.24

However, nginx could not be installed…
This is because this server does not refer to nginx package repository in default setting.  You can resolve it by adding nginx repository settting.
Create “nginx.repo” file under /etc/yum.repos.d/ directory

[root@ip-172-31-47-96 ~]# vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

OK. Let’s try to install nginx again.

[root@ip-172-31-47-96 ~]# yum install -y nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.centos.org
* extras: mirror.centos.org
* updates: mirror.centos.org
nginx-stable | 2.9 kB 00:00:00
nginx-stable/7/x86_64/primary_db | 63 kB 00:00:00
Resolving Dependencies
–> Running transaction check
—> Package nginx.x86_64 1:1.20.0-1.el7.ngx will be installed
–> Finished Dependency Resolution
Dependencies Resolved
==============================================================Package Arch Version Repository Size
==============================================================Installing:
nginx x86_64 1:1.20.0-1.el7.ngx nginx-stable 790 k
Transaction Summary
==============================================================Install 1 Package
Total download size: 790 k
Installed size: 2.8 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/nginx-stable/packages/nginx-1.20.0-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY ] 0.0 B/s | 213 kB –:–:– ETA
Public key for nginx-1.20.0-1.el7.ngx.x86_64.rpm is not installed
nginx-1.20.0-1.el7.ngx.x86_64.rpm | 790 kB 00:00:00
Retrieving key from https://nginx.org/keys/nginx_signing.key
Importing GPG key 0x7BD9BF62:
Userid : “nginx signing key <signing-key@nginx.com>”
Fingerprint: 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62
From : https://nginx.org/keys/nginx_signing.key
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:nginx-1.20.0-1.el7.ngx.x86_64 1/1
———————————————————————-
Thanks for using nginx!
Please find the official documentation for nginx here:
* https://nginx.org/en/docs/
Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* https://nginx.org/en/support.html
Commercial subscriptions for nginx are available on:
* https://nginx.com/products/
———————————————————————-
Verifying : 1:nginx-1.20.0-1.el7.ngx.x86_64 1/1
Installed:
nginx.x86_64 1:1.20.0-1.el7.ngx
Complete!

It seems the installation was succeeded. Let’s check it just to be safe.

[root@ip-172-31-47-96 ~]# nginx -v nginx version: nginx/1.20.0

It is OK!!

You can install necessary packages in a short time. In my next article, I will explain how to deploy Python application. I would be glad if you check it.

コメント