Deploying a Flask Application on AWS EC2
Tags: flask, aws, nginx, python
Published: 2023-07-01 11:14:42

In this blog post, we will walk through the process of deploying a Flask application on an AWS EC2 instance. We will cover each step from setting up the environment to configuring Nginx as a reverse proxy. By following these steps, you will be able to successfully deploy your Flask application and make it accessible to the outside world.

##### Step 1: Update the System
To ensure that our system has the latest package information, run the following command:
```
sudo apt-get update
```
This command updates the package lists for upgrades and new installations.

##### Step 2: Install Python Virtual Environment
To create an isolated Python environment for our project, we need to install the python3-venv package. Execute the following command:

```
sudo apt-get install python3-venv
```
Python virtual environments allow us to manage dependencies and isolate our project's environment from the system Python installation.

##### Step 3: Clone or Create the Project Directory
You have two options here. Either clone an existing project from GitHub using the git clone command, or create a new project directory using the mkdir command followed by the desired project name.

##### Step 4: Navigate to the Project Directory
Change your current working directory to the project directory using the cd command:
```
cd project_name
```

##### Step 5: Create a Virtual Environment
Now, let's create a virtual environment for our project. Execute the following command:
```
python3 -m venv venv
```
This command creates a virtual environment named "venv" inside your project directory.

##### Step 6: Activate the Virtual Environment
To activate the virtual environment, run the following command:
```
. venv/bin/activate
```
Once activated, your command prompt will reflect the virtual environment's name.

##### Step 7: Install Flask and Dependencies
Next, we need to install Flask and any other project dependencies. Use either of the following commands:
```
pip install -r requirements.txt
```
or
```
pip install Flask
```
The first command is used if you have a requirements.txt file listing all the dependencies. The second command installs Flask directly.

##### Step 8: Run the Flask Application
To ensure everything is working correctly, run the Flask application using the following command:
```
python app.py
```
This command starts the Flask development server, allowing you to test your application locally.

##### Step 9: Install Gunicorn
Gunicorn is a production-ready web server for running Flask applications. Install it using the following command:
```
pip install gunicorn
```

##### Step 10: Run Gunicorn
To run your Flask application with Gunicorn, execute the following command:
```
gunicorn -b 0.0.0.0:8000 app:app
```
This command starts Gunicorn, binding it to all available network interfaces on port 8000.

##### Step 11: Create a Systemd Service File
To run the Flask application as a service, create a systemd service file using the following command:
```
sudo nano /etc/systemd/system/project_name.service
```

##### Step 12: Add Code to the Service File
Inside the service file, add the following code:
```
[Unit]
Description=Gunicorn instance for a simple app
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/project_name
ExecStart=/home/ubuntu/project_name/venv/bin/gunicorn -b localhost:8000 app:app
Restart=always

[Install]
WantedBy=multi-user.target
```
This code specifies the service description, user, group, working directory, and the Gunicorn command to start the Flask application.

##### Step 13: Reload Systemd Daemon
After creating the service file, reload the systemd daemon to make it aware of the new service:
```
sudo systemctl daemon-reload
```

##### Step 14: Start the Service
To start the Flask application service, run the following command:
```
sudo systemctl start project_name
```

##### Step 15: Enable the Service on System Startup
To ensure that the Flask application service starts automatically on system boot, execute the following command:
```
sudo systemctl enable project_name
```

##### Step 16: Test the Application
To verify if the Flask application is running correctly, use the curl command to send a request to the local server:
```
curl localhost:8000
```
If everything is set up properly, you should see the response from your Flask application.

##### Step 17: Install Nginx
To set up Nginx as a reverse proxy, install it using the following command:
```
sudo apt-get install nginx
```

##### Step 18: Start Nginx
To start the Nginx service, run the following command:
```
sudo systemctl start nginx
```

##### Step 19: Enable Nginx on System Startup
To ensure that Nginx starts automatically on system boot, execute the following command:
```
sudo systemctl enable nginx
```

##### Step 20: Configure Nginx
Open the Nginx configuration file using the nano editor:
```
sudo nano /etc/nginx/sites-available/default
```

##### Step 21: Add Upstream and Proxy Pass
Add the following code at the top of the file, below the default comments:
```
upstream flaskhelloworld {
    server 127.0.0.1:8000;
}
```

##### Step 22: Configure Proxy Pass
Inside the location / block, add the following code:
```
location / {
    proxy_pass http://project_name;
}
```
This code sets up the reverse proxy to forward requests to the Gunicorn server running on port 8000.

##### Step 23: Restart Nginx
To apply the Nginx configuration changes, restart the Nginx service:
```
sudo systemctl restart nginx
```

Congratulations! You have successfully deployed a Flask application on an AWS EC2 instance using Gunicorn and Nginx as a reverse proxy. Your Flask application is now accessible through the Nginx server, providing a robust and scalable setup for your application.