Skip to content

Deploy on AWS EC2 for testing

Benoit G. Regamey edited this page Jan 23, 2024 · 2 revisions

Launch EC2 Instance

  • Choose an Amazon Linux t2.micro instance
  • Open port 80 (for traffic coming from http)
  • Open port 22 for ssh connection (by default)

Install python web app

ssh into the EC2 instance

ssh -i ~/.ssh/ec2.pem ec2-user@ec2-3-125-18-132.eu-central-1.compute.amazonaws.com

Install the app

sudo yum update
sudo yum install python3-pip
sudo yum install git

git clone https://github.com/geoadmin/web-swiss-geometa-validator.git
cd web-swiss-geometa-validator

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Systemd to start app w/ gunicorn

In sudo nano /etc/systemd/system/flaskapp.service, write :

[Unit]
Description=Gunicorn daemon to serve my flaskapp
After=network.target
[Service]
User=ec2-user
Group=ec2-user
WorkingDirectory=/home/ec2-user/web-swiss-geometa-validator
ExecStart=/home/ec2-user/web-swiss-geometa-validator/.venv/bin/gunicorn -c gunicorn.conf.py '__init__:create_app()'
[Install]
WantedBy=multi-user.target

Ctrl + x to save.

Start app and get status

sudo systemctl start flaskapp
sudo systemctl status flaskapp

Start app at boot

sudo systemctl enable flaskapp

Install and run nginx

sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx

In sudo nano /etc/nginx/conf.d/app.conf write :

server {
    listen 80 default_server;
    server_name _;
    access_log  /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    location / {
        proxy_pass         http://127.0.0.1:5000/;
        proxy_redirect     off;

        proxy_set_header   Host                 $host;
        proxy_set_header   X-Real-IP            $remote_addr;
        proxy_set_header   X-Forwarded-For	$proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto    $scheme;
    }
}

In sudo nano /etc/nginx/conf.d/client_max_body_size.conf write :

client_max_body_size 50M;

Reload Nginx

sudo systemctl reload nginx

Clone this wiki locally