Question

How to Deploy Flask App on Droplets

How could I deploy a flask app and keep it online 24/7? I setup a nignx server already on the droplet and have the project located in /var/www/. I then deployed it by running python3 main.py. I have it running at port 8080. I can access the app by going to https://<ip>:8080. However, it doesn’t stay online 24/7 and also I want add a subdomain pointing to it. I tried creating an A record and pointing it to the IP but it doesn’t work. Any help is appreciated!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
February 7, 2024

Hey!

Great job on getting your Flask app running on a DigitalOcean Droplet! To ensure your app stays online 24/7 and to set up a subdomain pointing to it, you’ll need to follow a few important steps.

To keep your app running continuously, especially after closing the terminal or in case of errors, using a process manager like Gunicorn is crucial. Moreover, integrating Gunicorn with systemd allows your Flask app to auto-start on boot and restart in case it crashes. Let’s set this up:

  1. Install Gunicorn:

    pip3 install gunicorn
    
  2. Test Run Gunicorn: First, ensure Gunicorn can serve your Flask app correctly. Replace main:app with your file and application name.

    gunicorn --workers 3 --bind unix:myapp.sock -m 007 main:app
    

    Here, main:app indicates that your Flask application instance is named app and is located in the main.py file. The command starts your app with 3 worker processes, using a Unix socket for communication, which is both efficient and secure.

  3. Create a systemd Service File for Your App: Next, create a systemd service file to manage your app. Create a new file named /etc/systemd/system/yourapp.service with the following content:

    [Unit]
    Description=Gunicorn instance to serve your Flask app
    After=network.target
    
    [Service]
    User=your_user
    Group=www-data
    WorkingDirectory=/var/www/your_project
    Environment="PATH=/var/www/your_project/venv/bin"
    ExecStart=/var/www/your_project/venv/bin/gunicorn --workers 3 --bind unix:yourapp.sock -m 007 main:app
    
    [Install]
    WantedBy=multi-user.target
    

    Replace your_user, /var/www/your_project, and other paths as necessary for your setup. This service file tells systemd how to start and manage your Gunicorn process.

  4. Enable and Start Your Flask App Service:

    sudo systemctl start yourapp.service
    sudo systemctl enable yourapp.service
    

    These commands start the service and enable it to start on boot, respectively.

  5. Configure Nginx to Proxy Requests to Gunicorn: Update your Nginx configuration to proxy requests to the Gunicorn socket as described in the previous message. Make sure to adjust the proxy_pass directive to point to the correct socket file path.

You can also follow a more in-depth guide on how to do that here:

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-22-04

For the subdomain setup, the process remains the same. Ensure your DNS A record points to the Droplet’s IP, configure Nginx with your subdomain, and consider securing it with an SSL certificate using Let’s Encrypt:

https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-nginx-in-ubuntu-22-04#step-2-–-configuring-nginx-to-use-ssl

Hope that this helps and happy coding and deployment!

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
February 7, 2024

Heya @fa452710b73e4632997145b18bc820,

First, you’ll need to configure Nginx to run as a reverse proxy so that you don’t have to call the port by only the IP or your domain to open the flask app:

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-20-04

In the tutorial you’ll see how to configure your Flask app with Nginx and Gunicorn. Which should do the trick for you and keep the app online 24/7

Alternatively, you can take a look at pm2. You can check that tutorial and while it’s for nodeJS, it can easily be done with your flask app as well:

https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps

Anyway, the standard approach is using the first article I gave you.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel