yemoski / Fantasy-Premier-League-Cheat

0 stars 0 forks source link

host my fpl app on aws #11

Open yemoski opened 1 month ago

yemoski commented 3 weeks ago

https://www.youtube.com/watch?v=ct1GbTvgVNM

yemoski commented 3 weeks ago

To migrate your Flask app from Heroku to AWS EC2, follow these steps:

Step 1: Set Up EC2 Instance

  1. Log into AWS: Go to the AWS Management Console.

  2. Launch an EC2 instance:

    • Choose an Amazon Machine Image (AMI). The most common choice is Ubuntu or Amazon Linux.
    • Choose an instance type (e.g., t2.micro for free-tier eligible).
    • Configure instance details, storage, and security groups:
      • Security Groups: Allow inbound traffic on HTTP (port 80) and SSH (port 22) for remote access.
    • Launch the instance and download the .pem key file.
  3. SSH into the EC2 instance:

    • Run the following command in your terminal:
      ssh -i "your-key.pem" ubuntu@your-ec2-public-ip

Step 2: Set Up Your Environment on EC2

  1. Install necessary packages: Update and install packages like Python, Git, and Pip:

    sudo apt update
    sudo apt install python3-pip python3-dev nginx git
  2. Clone your Flask app:

    git clone https://github.com/your-repository-url.git
  3. Install Flask app dependencies: Navigate to your project directory and install the required packages:

    cd your-app-directory
    pip3 install -r requirements.txt

Step 3: Configure a Web Server (Nginx + Gunicorn)

  1. Install Gunicorn (WSGI server):

    pip3 install gunicorn
  2. Test running your app with Gunicorn:

    gunicorn --bind 0.0.0.0:8000 wsgi:app

    This assumes your app entry point is named app in wsgi.py.

  3. Set up Nginx:

    • Edit Nginx config to proxy requests to Gunicorn:

      sudo nano /etc/nginx/sites-available/flask_app

      Add the following configuration:

      server {
       listen 80;
       server_name your-ec2-public-ip;
      
       location / {
           proxy_pass http://127.0.0.1:8000;
           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;
       }
      }
    • Enable this configuration:

      sudo ln -s /etc/nginx/sites-available/flask_app /etc/nginx/sites-enabled
      sudo nginx -t
      sudo systemctl restart nginx

Step 4: Set Up a Process Manager

  1. Install Supervisor:
    sudo apt install supervisor
  2. Configure Supervisor to manage Gunicorn:

    sudo nano /etc/supervisor/conf.d/flask_app.conf

    Add the following:

    [program:flask_app]
    command=/usr/local/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/flask_app.sock wsgi:app
    directory=/home/ubuntu/your-app-directory
    user=ubuntu
    autostart=true
    autorestart=true
    stderr_logfile=/var/log/flask_app.err.log
    stdout_logfile=/var/log/flask_app.out.log
    • Start the service:
      sudo supervisorctl reread
      sudo supervisorctl update
      sudo supervisorctl start flask_app

Step 5: Update DNS and Domain (Optional)

After these steps, your Flask app should be running on AWS EC2, accessible through the instance's public IP or domain.