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!
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!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
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 withsystemd
allows your Flask app to auto-start on boot and restart in case it crashes. Let’s set this up:Install Gunicorn:
Test Run Gunicorn: First, ensure Gunicorn can serve your Flask app correctly. Replace
main:app
with your file and application name.Here,
main:app
indicates that your Flask application instance is namedapp
and is located in themain.py
file. The command starts your app with 3 worker processes, using a Unix socket for communication, which is both efficient and secure.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: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.Enable and Start Your Flask App Service:
These commands start the service and enable it to start on boot, respectively.
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:
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:
Hope that this helps and happy coding and deployment!
Best,
Bobby
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.