Bobcares

Digitalocean Flask Gunicorn

by | May 28, 2022

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.

  1. Firstly, we must create a virtual environment on our machine.

    $ virtualenv venv -p Python3

  2. Then, turn on the virtual environment.

    $ source venv/bin/activate

  3. Then, we sgould Install Flask and gunicorn

    $ pip install Flask gunicorn

  4. 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

  5. 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.

  1. Firstly, make the gunicorn configuration file.

    $ touch gunicorn_config.py

  2. 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.

  1. 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.
  2. Then, create a.gitignore file and add.pyc to prevent these files from being pushed to GitHub.


    $ git init
    $ nano .gitignore


    # inside .gitignore
    .pyc

  3. 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"

  4. As the remote repository, add the GitHub repo we created earlier.

    $ git remote add origin https://github.com/your_username/flask-app

  5. Finally, upload to GitHub.


    $ git branch -M main
    $ git push -u origin main

Using Digital Ocean’s app platform for deployment

  1. Firstly, go to Digital Ocean and create a new “app.”
  2. Then, as a source, select GitHub.
  3. Then select the GitHub repository we just made.
  4. To make the app, follow the instructions.
  5. 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.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.