Flask is a lightweight and flexible Python web framework, and it’s often used with Gunicorn (a Python WSGI HTTP server) and Nginx (a high-performance web server) for production deployments. This guide will walk you through installing Flask with Nginx and Gunicorn on Ubuntu 22.04. Whether you’re hosting your Flask app locally or on a Windows VPS UK, this setup provides excellent scalability and performance.
Step 1: Update Your System
Before starting, ensure your Ubuntu system is up to date. Run the following commands to update your package list and upgrade any existing packages:
sudo apt update && sudo apt upgrade -y
Regular updates ensure that your server is secure and running smoothly, which is especially important for applications running on platforms like VPS Windows Servers.
Step 2: Install Python, Flask, and Virtual Environment
Install Python and the necessary tools for creating a virtual environment:
sudo apt install python3 python3-pip python3-venv -y
Next, create a project directory for your Flask app and navigate into it:
mkdir ~/myflaskapp
cd ~/myflaskapp
Create a Python virtual environment to keep your project dependencies isolated:
python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
Install Flask within the virtual environment:
pip install Flask
Step 3: Create a Simple Flask Application
Create a simple Flask app by creating a new Python file:
nano ~/myflaskapp/app.py
Add the following content to create a basic Flask app:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, World!"
if __name__ == '__main__':
app.run(host='0.0.0.0')
Save and close the file.
Step 4: Install and Configure Gunicorn
Install Gunicorn inside your virtual environment:
pip install gunicorn
Now, run Gunicorn to serve your Flask application:
gunicorn --bind 0.0.0.0:8000 app:app
This will bind Gunicorn to port 8000 and run your Flask app. At this point, you can access your Flask app by navigating to http://your_server_ip:8000
in your browser.
Step 5: Set Up Gunicorn as a Systemd Service
To run Gunicorn as a background service, create a systemd service file:
sudo nano /etc/systemd/system/myflaskapp.service
Add the following content, replacing /home/your_username
with the correct path to your Flask app:
[Unit]
Description=Gunicorn instance to serve Flask app
After=network.target
[Service]
User=your_username
Group=www-data
WorkingDirectory=/home/your_username/myflaskapp
Environment="PATH=/home/your_username/myflaskapp/venv/bin"
ExecStart=/home/your_username/myflaskapp/venv/bin/gunicorn --workers 3 --bind unix:myflaskapp.sock -m 007 app:app
[Install]
WantedBy=multi-user.target
Save and close the file, then reload systemd to apply the new service:
sudo systemctl daemon-reload
Start the Gunicorn service and enable it to start at boot:
sudo systemctl start myflaskapp
sudo systemctl enable myflaskapp
Step 6: Install and Configure Nginx
Install Nginx with the following command:
sudo apt install nginx -y
Create an Nginx server block for your Flask app:
sudo nano /etc/nginx/sites-available/myflaskapp
Add the following configuration:
server {
listen 80;
server_name your_domain_or_ip;
location / {
proxy_pass http://unix:/home/your_username/myflaskapp/myflaskapp.sock;
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;
}
location /static/ {
alias /home/your_username/myflaskapp/static/;
}
}
Enable the Nginx configuration and restart the service:
sudo ln -s /etc/nginx/sites-available/myflaskapp /etc/nginx/sites-enabled/
sudo systemctl restart nginx
Step 7: Obtain an SSL Certificate with Let's Encrypt (Optional)
To secure your Flask app with HTTPS, you can use Let's Encrypt to obtain a free SSL certificate. First, install Certbot:
sudo apt install certbot python3-certbot-nginx -y
Then, run Certbot to obtain and configure SSL:
sudo certbot --nginx -d your_domain
Follow the prompts to set up SSL. Certbot will automatically update your Nginx configuration.