DEV Community

Omoruyi Emmanuel Osakue
Omoruyi Emmanuel Osakue

Posted on

HNG Internship Week 1

So... As I said, beginnings are always easy, and HNG dropped a big one on me this week making me think. How about I try to push beyond my capabilities?
So far I have little prerequisite with Python and API technologies, lame I know. But if you're reading this it means I overcame so I implore you to read through this and I'll tell you how

Steps to Deploy on EC2:

Launch an EC2 Instance:

  1. Choose a Linux AMI.
  2. Select an appropriate instance type (t2.micro).
  3. Configure security groups to allow inbound HTTP traffic (port 80) and SSH (port 22).
  4. Connect to your EC2 Instance: Use SSH to connect to your instance.

Install Python and Dependencies:

sudo apt update -y  # Update system packages
sudo apt install python3.12-venv  # Create a virtual environment
Enter fullscreen mode Exit fullscreen mode

You can clone your GitHub repository and change into it, then activate virtual environment.

python3 -m venv venv 
pip install -r requirements.txt # Install dependencies
Enter fullscreen mode Exit fullscreen mode

Create a WSGI file (wsgi.py):

touch wsgi.py
Enter fullscreen mode Exit fullscreen mode
from app import app  # Replace app with your Flask app's name

if __name__ == "__main__":
    app.run()
Enter fullscreen mode Exit fullscreen mode

Run with Gunicorn (Recommended for Production):

gunicorn --bind :8000 wsgi:app  # Run Gunicorn on port 8000
Enter fullscreen mode Exit fullscreen mode

You could run this in the background using nohup or screen so it continues to run after you disconnect from SSH.

Configure a Reverse Proxy (Nginx or Apache): sudo apt install nignx -y

A reverse proxy is highly recommended for production. It handles static files, load balancing, and more. Here's a basic Nginx configuration example:

server {
    listen 80;
    server_name your_ec2_public_ip_or_dns;  # Replace with your EC2 instance's public IP or DNS

    location / {
        proxy_pass http://127.0.0.1:8000;  # Forward requests to Gunicorn
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
Enter fullscreen mode Exit fullscreen mode

Place this configuration in /etc/nginx/conf.d/your_app.conf and restart Nginx:

sudo systemctl restart nginx.
Enter fullscreen mode Exit fullscreen mode

Get your EC2 Public IP or DNS: You can find this in the EC2 console. Use this to access your API: http://54.204.77.176/api/classify-number?number=371

Top comments (0)