Flask Gunicorn in Digitalocean allows us to use a microframework model to use Python WSGI HTTP Server.
Bobcares responds to all inquiries, no matter how big or small, as part of our DigitalOcean Managed Service.
Take a look at how our Support team explained how to use gunicorn to deploy a Flask app to Digitalocean.
Digitalocean Flask Gunicorn
Set up the Flask app’s environment.
- Firstly, we must create a virtual environment on our machine.
$ virtualenv venv -p Python3
- Then, turn on the virtual environment.
$ source venv/bin/activate
- Then, we sgould Install Flask and gunicorn
$ pip install Flask gunicorn
- We’ll need to save this requirement and its dependencies now that we’ve installed the flask package, so App Platform can install them later. Do this right now with pip, then save the data to a requirements.txt file.
$ pip freeze > requirements.txt
- In app.py, write the code for our Flask app. Here’s a simple “hello world” app to get us started.
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!'
Configuring the Gunicorn
We’ll use Gunicron web server because we won’t be able to use Flask’s built-in server in a production environment. Flask’s built-in server, while lightweight and simple to use, is not suitable for production use because it does not scale well and by default only serves one request at a time.
- Firstly, make the gunicorn configuration file.
$ touch gunicorn_config.py
- Then, in the gunicorn config.py file, add this configuration.
# inside gunicorn_config.py
bind = "0.0.0.0:8080"
workers = 2
Adding the Site to GitHub
The Digital Ocean app platform works by syncing with a GitHub repository. Any changes to this GitHub repository will be automatically pushed to our app, which is hosted on Digital Ocean’s app platform.
- Firstly, create a new repository on GitHub.com. To do so, navigate to GitHub, log in with our profile, and create a new repository called flask-app.
- Then, create a.gitignore file and add.pyc to prevent these files from being pushed to GitHub.
$ git init
$ nano .gitignore
# inside .gitignore
.pyc
- Then, add files to the repository and save the changes.
$ git add app.py gunicorn_config.py requirements.txt .gitignore
$ git commit -m "Initial Flask App"
- As the remote repository, add the GitHub repo we created earlier.
$ git remote add origin https://github.com/your_username/flask-app
- Finally, upload to GitHub.
$ git branch -M main
$ git push -u origin main
Using Digital Ocean’s app platform for deployment
- Firstly, go to Digital Ocean and create a new “app.”
- Then, as a source, select GitHub.
- Then select the GitHub repository we just made.
- To make the app, follow the instructions.
- Finally, To do so, change the run command to this.
- gunicorn --worker-tmp-dir /dev/shm app:app
[Looking for a solution to another query? We are just a click away.]
Conclusion
To sum up, our Support team demonstrated how to deploy a Flask app to Digitalocean using gunicorn.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
var google_conversion_label = "owonCMyG5nEQ0aD71QM";
0 Comments