Bobcares

Flask Gunicorn with Nginx in Digitalocean | How to ?

by | Jun 7, 2022

Digitalocean Flask Gunicorn with Nginx is an extremely flexible framework designed to provide functionality to our applications without being overly restrictive in terms of structure and design.

As part of our Digitalocean managed service, Bobcares responds to all inquiries, no matter how big or small.

Let’s look at how our Support team helped one of our customers set up the Gunicorn application server and configure Nginx to serve as a front-end reverse proxy.

Digitalocean Flask Gunicorn with Nginx

digitalocean flask gunicorn nginx

Step 1: Installing Components from Ubuntu Repositories

The first step is to download and install all of the required packages from the Ubuntu default repositories. PIP, the Python package manager, will be used to manage our Python components. We’ll also get the Python development files we’ll need to put some of the Gunicorn pieces together.

  1. Firstly, update the local package as follows:

    sudo apt update

  2. Then, we’ll need to install the packages that will enable us to set up our Python environment.

    sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools

Move on to creating a virtual environment for our project now that these packages are in place.

Step 2: Creating a Virtual Python Environment

Set up a virtual environment to keep our Flask application separate from the rest of the Python files on the system.

  1. Firstly, install the python3-venv package, which includes the venv module:

    sudo apt install python3-venv

  2. Then, for our Flask project, create a parent directory:

    mkdir ~/bobcaresproject

  3. Then, after we’ve created the directory, change into it:

    cd ~/bobcaresproject

  4. Enter the following to create a virtual environment to store our Flask project’s Python requirements:

    python3.6 -m venv bobcaresprojectenv

    This will create a directory called bobcaresprojectenv in our project directory, which will contain a local copy of Python and PIP.

  5. Before we can install applications in the virtual environment, we must first activate it by running the commands below:

    source bobcaresprojectenv/bin/activate

  6. Finally, our prompt will change to reflect the fact that we are now in a virtual environment. It will look something like this:

    (bobcaresprojectenv)\bobcares@host:~/bobcaresproject$

Step 3: Configuring a Flask Application

Now that we’ve set up our virtual environment, we can install Flask and Gunicorn and begin working on our application design.

  1. Firstly, to ensure that our packages will install even if they are missing wheel archives, install wheel with the local instance of PIP:

    pip install wheel

  2. Then, set up Flask and Gunicorn as follows:

    pip install gunicorn flask

  3. Make a file that will serve as the application’s entry point. This instructs the Gunicorn server on how to interact with the app.

    nano ~/myproject/wsgi.py

  4. Then, import the Flask instance from our app into the file and run it.
  5. Finally, save and close the file.

Step 4: Configuring Gunicorn

We can now proceed to configuring Gunicorn because the application has been written and an entry point has been established.

  1. Firstly, navigate to the appropriate directory:

    cd ~/bobcaresproject

  2. Next, we’ll use the name of our entry point to see if Gunicorn can correctly serve the application. To start the application on a publicly accessible interface, we can specify the interface and port to bind to:

    gunicorn --bind 0.0.0.0:5000 wsgi:app

  3. Then, Deactivate our virtual environment now that we’ve finished with it:

    deactivate

Any Python commands will now use the Python environment provided by the system.Then, the systemd service unit file must be created. When we create a systemd unit file, Ubuntu’s init system will start Gunicorn and serve the Flask application automatically whenever the server boots.

  1. Firstly, create a unit file in the /etc/systemd/system directory that ends in.service:

    sudo nano /etc/systemd/system/bobcaresproject.service

  2. Then, start the Gunicorn service we created:

    sudo systemctl start bobcaresproject

  3. Then enable it so that it runs each time the server is started:

    sudo systemctl enable bobcaresproject

  4. Finally, check the status:

    sudo systemctl status bobcaresproject

Step 5: Configuring Nginx to Proxy Requests

The Gunicorn application server should now be up and running, waiting for requests on the project directory’s socket file. Next, make some minor changes to Nginx’s configuration file to direct web requests to that socket.

  1. Firstly, create a new server block configuration file in the sites-available directory of Nginx.

    sudo nano /etc/nginx/sites-available/bobcaresproject

  2. Then, link the file to the sites-enabled directory to enable the Nginx server block configuration we’ve created.

    sudo ln -s /etc/nginx/sites-available/bobcaresproject /etc/nginx/sites-enabled

  3. We can check for syntax errors using the link in that directory:

    sudo nginx -t

  4. Then restart the Nginx process to read the new configuration if this returns without any errors:

    sudo systemctl restart nginx

  5. Finally, make another adjustment to the firewall. Remove the following rule because we no longer require access via port 5000:

    sudo ufw delete allow 5000

  6. Then give the Nginx server complete access:

    sudo ufw allow 'Nginx Full'

  7. In the web browser, we should now be able to navigate to our server’s domain name:

    http://domain_name

If we run into any problems, take a look at the following.

  • sudo less /var/log/nginx/error.log : examines Nginx’s error logs
  • sudo less /var/log/nginx/access.log : examines Nginx’s access logs
  • sudo journalctl -u nginx : examines the Nginx process logs
  • sudo journalctl -u bobcaresproject : examines the Gunicorn logs in our Flask app

Step 6: Securing the Application

We should get an SSL certificate for our domain to ensure that traffic to our server is secure.

  1. Firstly, use snap to install Certbot:

    sudo snap install --classic certbot

  2. The output will show the current version of Certbot and indicate whether or not the installation was successful.
  3. Then, from the /usr/bin/ directory, create a symbolic link to the newly installed /snap/bin/certbot executable.

    sudo ln -s /snap/bin/certbot /usr/bin/certbot

  4. Certbot offers a number of plugins that can be used to obtain SSL certificates. The Nginx plugin will take care of reloading the configuration and reconfiguring Nginx as needed.

    sudo certbot --nginx -d domain_name -d www.domain_name

This uses the –nginx plugin to run certbot, and the -d option to specify the names for which the certificate should be valid.

[Looking for a solution to another query? We are just a click away.]

Conclusion

To summarise, our Support team demonstrated how to install and configure the flask gunicorn with nginx in digitalocean.

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.