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 updatesudo apt-get updatesudo apt-get update
Enter fullscreen mode Exit fullscreen mode
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-venvsudo apt-get install python3-venvsudo apt-get install python3-venv
Enter fullscreen mode Exit fullscreen mode
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_namecd project_namecd project_name
Enter fullscreen mode Exit fullscreen mode
Step 5: Create a Virtual Environment
Now, let’s create a virtual environment for our project. Execute the following command:
python3 -m venv venvpython3 -m venv venvpython3 -m venv venv
Enter fullscreen mode Exit fullscreen mode
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. venv/bin/activate. venv/bin/activate
Enter fullscreen mode Exit fullscreen mode
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.txtpip install -r requirements.txtpip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode
or
pip install Flaskpip install Flaskpip install Flask
Enter fullscreen mode Exit fullscreen mode
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.pypython app.pypython app.py
Enter fullscreen mode Exit fullscreen mode
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 gunicornpip install gunicornpip install gunicorn
Enter fullscreen mode Exit fullscreen mode
Step 10: Run Gunicorn
To run your Flask application with Gunicorn, execute the following command:
gunicorn -b 0.0.0.0:8000 app:appgunicorn -b 0.0.0.0:8000 app:appgunicorn -b 0.0.0.0:8000 app:app
Enter fullscreen mode Exit fullscreen mode
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.servicesudo nano /etc/systemd/system/project_name.servicesudo nano /etc/systemd/system/project_name.service
Enter fullscreen mode Exit fullscreen mode
Step 12: Add Code to the Service File
Inside the service file, add the following code:
[Unit]Description=Gunicorn instance for a simple appAfter=network.target[Service]User=ubuntuGroup=www-dataWorkingDirectory=/home/ubuntu/project_nameExecStart=/home/ubuntu/project_name/venv/bin/gunicorn -b localhost:8000 app:appRestart=always[Install]WantedBy=multi-user.target[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[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
Enter fullscreen mode Exit fullscreen mode
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-reloadsudo systemctl daemon-reloadsudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode
Step 14: Start the Service
To start the Flask application service, run the following command:
sudo systemctl start project_namesudo systemctl start project_namesudo systemctl start project_name
Enter fullscreen mode Exit fullscreen mode
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_namesudo systemctl enable project_namesudo systemctl enable project_name
Enter fullscreen mode Exit fullscreen mode
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:8000curl localhost:8000curl localhost:8000
Enter fullscreen mode Exit fullscreen mode
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 nginxsudo apt-get install nginxsudo apt-get install nginx
Enter fullscreen mode Exit fullscreen mode
Step 18: Start Nginx
To start the Nginx service, run the following command:
sudo systemctl start nginxsudo systemctl start nginxsudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode
Step 19: Enable Nginx on System Startup
To ensure that Nginx starts automatically on system boot, execute the following command:
sudo systemctl enable nginxsudo systemctl enable nginxsudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode
Step 20: Configure Nginx
Open the Nginx configuration file using the nano editor:
sudo nano /etc/nginx/sites-available/defaultsudo nano /etc/nginx/sites-available/defaultsudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode
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;}upstream flaskhelloworld { server 127.0.0.1:8000; }upstream flaskhelloworld { server 127.0.0.1:8000; }
Enter fullscreen mode Exit fullscreen mode
Step 22: Configure Proxy Pass
Inside the location / block, add the following code:
location / {proxy_pass http://project_name;}location / { proxy_pass http://project_name; }location / { proxy_pass http://project_name; }
Enter fullscreen mode Exit fullscreen mode
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 nginxsudo systemctl restart nginxsudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode
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.
暂无评论内容