Tuesday, December 2, 2025

thumbnail

How to Deploy Flask Applications to DigitalOcean

 How to Deploy Flask Applications to DigitalOcean


DigitalOcean is a popular choice for hosting Flask apps because it’s simple, affordable, and flexible. You can deploy using:


Droplets (virtual servers)


App Platform (managed PaaS — easiest)


Docker containers


Kubernetes


Below you'll find instructions for the two most common methods:

1️⃣ Droplet Deployment (Manual Server Setup)

2️⃣ App Platform Deployment (Fully Managed)


๐ŸŒ Option 1: Deploy Flask on a DigitalOcean Droplet (Manual, More Control)


This method gives you full control of the server and environment.


๐Ÿงฑ Step 1: Create a Droplet


Go to DigitalOcean Dashboard → Droplets


Choose Ubuntu 22.04 LTS (recommended)


Select plan (Basic / Shared CPU is fine)


Add SSH key or create a root password


Create Droplet


SSH into the Droplet:


ssh root@YOUR_DROPLET_IP


๐Ÿ› ️ Step 2: Install System Requirements


Update system:


apt update && apt upgrade -y



Install Python + tools:


apt install python3 python3-pip python3-venv -y



Install Git if needed:


apt install git -y


๐Ÿ“ Step 3: Clone Your Flask App


Example:


git clone https://github.com/your-username/your-flask-app.git

cd your-flask-app



Create a virtual environment:


python3 -m venv venv

source venv/bin/activate



Install dependencies:


pip install -r requirements.txt


๐Ÿ”ฅ Step 4: Use Gunicorn to Run Flask in Production


Install Gunicorn:


pip install gunicorn



Run test server:


gunicorn --bind 0.0.0.0:5000 app:app



๐Ÿ’ก Replace app:app with your Python file and Flask variable name.


Stop with CTRL+C.


๐Ÿงฑ Step 5: Create a Gunicorn Systemd Service


Create file:


nano /etc/systemd/system/flaskapp.service



Paste:


[Unit]

Description=Gunicorn instance for Flask

After=network.target


[Service]

User=root

WorkingDirectory=/root/your-flask-app

Environment="PATH=/root/your-flask-app/venv/bin"

ExecStart=/root/your-flask-app/venv/bin/gunicorn --workers 3 --bind unix:flaskapp.sock -m 007 app:app


[Install]

WantedBy=multi-user.target



Save and exit.


Start service:


systemctl start flaskapp

systemctl enable flaskapp


๐ŸŒ Step 6: Install and Configure Nginx


Install Nginx:


apt install nginx -y



Create a site config:


nano /etc/nginx/sites-available/flaskapp



Paste:


server {

    listen 80;

    server_name YOUR_DOMAIN_OR_IP;


    location / {

        include proxy_params;

        proxy_pass http://unix:/root/your-flask-app/flaskapp.sock;

    }

}



Enable config:


ln -s /etc/nginx/sites-available/flaskapp /etc/nginx/sites-enabled

nginx -t

systemctl restart nginx



Your Flask app is now live!


๐Ÿ” Step 7: (Optional) Enable HTTPS with Let’s Encrypt


Install certbot:


apt install certbot python3-certbot-nginx -y



Run:


certbot --nginx -d yourdomain.com -d www.yourdomain.com



Now your site is fully secured.


๐ŸŽ‰ Your Flask app is now deployed with Nginx + Gunicorn on DigitalOcean!

☁️ Option 2: Deploy Flask Using DigitalOcean App Platform (Easiest)


This is a fully managed platform like Heroku.


๐Ÿงฑ Step 1: Push Code to GitHub


Your Flask app should include:


requirements.txt


Procfile (or Dockerfile)


A sample Procfile:


web: gunicorn app:app


๐Ÿš€ Step 2: Create an App in App Platform


Go to DigitalOcean → Apps


Click Create App


Connect your GitHub repo


Select region, instance size


App Platform detects Python automatically


Set build & run commands if needed:


Build: pip install -r requirements.txt


Run: gunicorn app:app


๐ŸŒ Step 3: Configure Environment Variables


In the App Platform UI:


Add secrets (API keys, DB URLs)


Add environment variables


๐Ÿ” Step 4: HTTPS Enabled Automatically


App Platform gives:


Free HTTPS


Autoscaling


Log viewing


Automatic deployments


๐ŸŽ‰ Your Flask App Is Live on App Platform!


This is the simplest and most beginner-friendly method.


๐Ÿงฉ Which Deployment Method Should You Choose?

Method Pros Cons

Droplet Full control, customizable, cheaper Requires server management

App Platform Easiest, auto HTTPS, auto deploy Slightly more expensive

Docker Portable, consistent Requires Docker knowledge

Kubernetes Scalable Overkill for small apps

๐Ÿ Conclusion


Deploying a Flask application to DigitalOcean is flexible and straightforward.

You can choose a fully managed option (App Platform) or manually configure a production-grade environment with Nginx and Gunicorn on a Droplet.


Both approaches work well — it just depends on how much control you want.

Learn Fullstack Python Training in Hyderabad

Read More

Using Nginx and Gunicorn for Python Web Application Deployment

How to Deploy a Django Application on AWS

Setting Up Continuous Deployment (CD) for Full Stack Python Projects

Using Docker to Containerize Your Full Stack Python Application

At Our Quality Thought Training Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive