This guide walks you through the steps to deploy a Flask application on an AWS EC2 instance using Ubuntu Server 20.04 LTS.
-
Go to EC2 Dashboard:
- Sign in to AWS Management Console.
- Navigate to the EC2 Dashboard.
-
Launch Instance:
- Click on "Launch Instance".
- Choose "Ubuntu Server 20.04 LTS" (or latest LTS version).
- Select instance type "t2.medium".
-
Configure Instance:
- Use default settings for VPC and subnet.
- Add storage as needed (default is fine for most cases).
- Add tags for easier identification (optional).
-
Configure Security Group:
- Create a new security group or use an existing one.
- Configure inbound rules:
- SSH (port 22) from your IP (to access the instance).
- HTTP (port 80) from anywhere (to allow web traffic).
- HTTPS (port 443) from anywhere (if using SSL).
-
Review and Launch:
- Review your instance configuration.
- Click "Launch" and select or create a new key pair to connect to your instance securely.
-
Allocate an Elastic IP:
- In the EC2 Dashboard, go to "Elastic IPs".
- Click "Allocate Elastic IP address".
- Select the allocated IP and associate it with your instance.
-
Update Domain DNS:
- Go to your domain registrar's website.
- Update the A record to point to your Elastic IP.
Use the following command to SSH into your instance:
ssh -i your-key.pem ubuntu@your-elastic-ip
Replace your-key.pem
with the path to your private key file and your-elastic-ip
with your instance's Elastic IP address.
- Update and Install Dependencies:
sudo apt update && sudo apt upgrade -y
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev \
python3-openssl git
- Install pyenv:
curl https://pyenv.run | bash
- Add pyenv to your path:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc
- Install Python 3.9.6:
pyenv install 3.9.6
pyenv global 3.9.6
- Create a Virtual Environment:
python -m venv venv
source venv/bin/activate
- Install wkhtmltopdf:
sudo apt-get update
sudo apt-get install -y wkhtmltopdf
- Verify the installation:
wkhtmltopdf --version
- Clone your repository and install dependencies:
git clone your-repo-url
cd your-repo-directory
pip install -r requirements.txt
- Create .env file:
nano .env
# Add your environment variables here
- Install and Configure Nginx:
sudo apt install nginx
sudo cp configs/nginx.conf /etc/nginx/nginx.conf
sudo nginx -t
sudo systemctl enable nginx
- Install Gunicorn:
pip install gunicorn
- Make your run scripts executable:
chmod +x run.sh runwithlogs.sh runnginx.sh
- Create a systemd service file for your application:
sudo nano /etc/systemd/system/flask_app.service
Add the following content:
[Unit]
Description=Gunicorn instance to serve flask app
After=network.target
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/your-repo-directory
Environment="PATH=/home/ubuntu/venv/bin"
ExecStart=/home/ubuntu/your-repo-directory/runwithlogs.sh
[Install]
WantedBy=multi-user.target
- Start and Enable the Service:
sudo systemctl start flask_app
sudo systemctl enable flask_app
- Start Nginx:
sudo systemctl start nginx
- Install Certbot and the Nginx plugin:
sudo apt update
sudo apt install certbot python3-certbot-nginx
- Obtain SSL Certificate, Replace your-domain.com with your actual domain:
sudo certbot --nginx -d your-domain.com
- Configure SSH to use the SSL Certificate, Edit the SSH configuration file:
sudo nano /etc/ssh/sshd_config
Your Flask application should now be running on your EC2 instance. Access it via your domain name or Elastic IP address in a web browser.