Deploying your Flask server online involves several steps, including setting up a virtual private server (VPS), configuring a web server with a reverse proxy, and ensuring your server is secure and performant. Below, I'll guide you through deploying your Flask app using a popular VPS provider, DigitalOcean, along with Nginx and Gunicorn.
Step-by-Step Guide
1. Set Up a Virtual Private Server (VPS)
First, you'll need to create an account with a cloud provider such as DigitalOcean, AWS, or any other provider of your choice.
For DigitalOcean, follow these steps:
-
Create a Droplet:
- Log in to your DigitalOcean account.
- Click on "Create" and choose "Droplets".
- Choose an Ubuntu image.
- Select a plan that suits your needs.
- Choose a data center region.
- Add your SSH keys (recommended) or set a root password.
- Click "Create Droplet".
-
Access Your Droplet:
- Use SSH to access your droplet:
ssh root@your_droplet_ip
2. Install Required Software on VPS
Update and upgrade the server first:
sudo apt update
sudo apt upgrade -y
Install essential packages:
sudo apt install python3-pip python3-dev nginx git
3. Set Up Your Flask Application on VPS
-
Clone Your Flask App:
- You can use git to clone your project repository (contains your
app.py file) or simply SCP/FTP the files:
git clone https://github.com/yourusername/yourrepository.git
cd yourrepository
Replace https://github.com/yourusername/yourrepository.git with the URL of your repository.
-
Set Up a Virtual Environment:
sudo apt install -y python3-venv
python3 -m venv venv
source venv/bin/activate
-
Install Requirements:
pip install -r requirements.txt
Ensure your requirements.txt includes Flask, Gunicorn, and other dependencies.
4. Set Up Gunicorn
Gunicorn will serve your Flask application:
pip install gunicorn
Run your app using Gunicorn to test:
gunicorn --bind 0.0.0.0:8000 wsgi:app
Note: wsgi.py should be created as an entry point for Gunicorn.
Create a wsgi.py file:
from app import app
if __name__ == "__main__":
app.run()
5. Configure Nginx
Nginx will act as a reverse proxy, directing traffic to Gunicorn:
-
Create Nginx Configuration:
sudo nano /etc/nginx/sites-available/yourapp
Add the following configuration:
server {
listen 80;
server_name your_domain_or_IP;
location / {
proxy_pass http://127.0.0.1:8000;
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;
}
}
Replace your_domain_or_IP with your server’s domain or IP address.
-
Enable the Configuration:
sudo ln -s /etc/nginx/sites-available/yourapp /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
6. Set Up Systemd Service for Gunicorn
Create a systemd service file to manage Gunicorn:
sudo nano /etc/systemd/system/yourapp.service
Add the following configuration:
[Unit]
Description=Gunicorn instance to serve yourapp
After=network.target
[Service]
User=yourusername
Group=www-data
WorkingDirectory=/path/to/your/app
Environment="PATH=/path/to/your/app/venv/bin"
ExecStart=/path/to/your/app/venv/bin/gunicorn --workers 3 --bind unix:yourapp.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
Replace /path/to/your/app with the path to your Flask app and yourusername with your username.
Start and enable the service:
sudo systemctl start yourapp
sudo systemctl enable yourapp
7. Open Firewall Ports
If you have a firewall enabled, open port 80 for HTTP traffic:
sudo ufw allow 'Nginx Full'
sudo ufw reload
Conclusion
Your Flask application should now be online and accessible via your server’s IP address or domain name.
Note: This guide is a basic deployment setup, and there are many more advanced configurations and best practices to consider, such as SSL configuration, application monitoring, and scaling. For SSL, you can use Let's Encrypt to obtain and install a free SSL certificate.