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!
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:
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:
Anyway, the standard approach is using the first article I gave you.
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:
Install Gunicorn:
pip3 install gunicorn
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.
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.
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.
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.