aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
  • Tools
  • About
  • Software Engineering
  • Tech

How-To: Deploy Python Flask applications using NGINX and uWSGI – Deployment (Part 3 of 3)

  • relay
  • November 22, 2022
  • 2 minute read

This is the last part of the 3-part guide on setting up a Python/API application on Ubuntu 22.04 with NGINX as the web server and uWSGI and uWSGI Emperor which is responsible for loading the application. 

If you’ve missed the installation part, see here. If you’ve finished the installation but not the configuration go here.

Prerequisites

  • Ubuntu 22.04
  • Installed NGINX, uWSGI, and uWSGI-Emperor.
  • Performed basic configuration of NGINX, uWSGI and uWSGI-Emperor from the previous guides.

Deploy a Flask App/API using uWSGI and Nginx

Note: For this demo, we used the sample flask app and a python virtual environment. And it will be located under /var/www

01. Create the following directories and files

Directories and file structure

|__ /var/www
    |__ geek_flask_app
        |__ api
            |__ __init__.py
            |__ res
               |__ __init__.py
               |__ endpoints.py
        |__ app_wsgi.py
        |__ requirements.txt

Or create the files and directories using the following commands

$ cd /var/www
$ sudo mkdir -p geek_flask_app/api/res

02. Create the init file

$ sudo nano /var/www/geek_flask_app/api/__init__.py

Set the content as follows

from flask import Flask
application = Flask(__name__)

import api.res.endpoints

03.Create the WSGI python file

$ sudo nano /var/www/geek_flask_app/app_wsgi.py

Set the content as follows

from api import application

04. Create the endpoints python module. This will contain the exposed application service endpoints.

$ sudo nano /var/www/geek_flask_app/api/res/endpoints.py

Set the content as follows

# -*- coding: utf-8 -*-

from flask  import jsonify
from api    import application


@application.route('/', methods=['GET'])
def get_app_properties():
  name    = 'geek-flask-api'
  version = '0.0.1'

  app_properties = \
    {
      'name'      : name
      , 'version' : version
    }

  return jsonify(app_properties)

04. Setup the requirements text file. It contains the required python libraries. In this example, we only need flask.

$ sudo nano /var/www/geek_flask_app/requirements.txt

Set the content as follows

flask

05. Install python libraries that are required by your application.

Read More  The Future Of Cloud-Native Databases Begins With Apache Cassandra 4.0

Note: Since we are using a virtual environment, activate the virtual environment and install the python libraries. See the Setup the Python Virtual Environment section.

$ source ~/geek-venv/bin/activate
(geek-venv) [email protected]{host}:~$ cd /var/www/geek_flask_app
(geek-venv) [email protected]{host}:~$ pip install -r requirements.txt
(geek-venv) [email protected]{host}:~$ deactivate

03. Restart the uwsgi-emperor service.

$ sudo service uwsgi-emperor restart

04. To verify if the flask app is deployed, test using the http-socket defined in uwsgi configuration.

$ curl http://127.0.0.1:8081

It should return a JSON structured response

{"name":"geek-flask-api","version":"0.0.1"}

05. Next is to verify if NGINX can communicate with our Python Flask app on uWSGI. We now have to remove the default NGINX configuration.

$ sudo rm /etc/nginx/sites-enabled/default

06. Restart the NGINX server

$ sudo service nginx restart

07. Verify using cURL on the NGINX.

$ curl http://localhost

It should also show the same response when communicating with the port defined in uWSGI.

{"name":"geek-flask-api","version":"0.0.1"}

Next step is to configure your DNS and map it to your NGINX configuration. And you should next configure the previously commented gid and uid to use the proper user, an example is “www-data”.

relay

You May Also Like
View Post
  • Computing
  • Software Engineering

Oracle Helidon Taps Virtual Threads For ‘Pure Performance’

  • March 29, 2023
View Post
  • Engineering
  • Software Engineering

My First Pull Request At Age 14

  • March 24, 2023
View Post
  • Practices
  • Software Engineering

Pythonic Techniques For Handling Sequences

  • March 21, 2023
View Post
  • Software Engineering

Eclipse Java Downloads Skyrocket

  • March 19, 2023
View Post
  • Software
  • Software Engineering
  • Tools

How To Use Bash

  • March 17, 2023
View Post
  • Software Engineering

Ballerina: A Programming Language For The Cloud

  • March 16, 2023
View Post
  • Engineering
  • People
  • Software Engineering

A Guide To Managing Your Agile Engineering Team

  • March 15, 2023
View Post
  • Software
  • Software Engineering

Python 3.12.0 Alpha 6 Released

  • March 15, 2023

Stay Connected!
LATEST
  • 1
    Kubernetes K8s.gcr.io Redirect: What You Need To Know As An Anthos Or GKE User
    • March 30, 2023
  • 2
    Oracle Helidon Taps Virtual Threads For ‘Pure Performance’
    • March 29, 2023
  • 3
    2022 State Of DevOps Report Data Deep Dive: Good Team Culture
    • March 29, 2023
  • 4
    Google Data Cloud & AI Summit : In Less Than 12 Hours From Now
    • March 29, 2023
  • 5
    A 5-Minute Tour Of The Fediverse
    • March 28, 2023
  • 6
    Bringing Observability To Cloud Security
    • March 28, 2023
  • 7
    How AI Can Improve Digital Security
    • March 27, 2023
  • 8
    Docker’s Bad Week
    • March 27, 2023
  • 9
    My First Pull Request At Age 14
    • March 24, 2023
  • 10
    AWS Chatbot Now Integrated Into Microsoft Teams
    • March 24, 2023
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    Introducing GPT-4 In Azure OpenAI Service
    • March 21, 2023
  • 2
    IBM And Fundación Ikerbasque Partner To Launch Groundbreaking Quantum Computational Center
    • March 24, 2023
  • 3
    Cleveland Clinic And IBM Unveil First Quantum Computer Dedicated To Healthcare Research
    • March 20, 2023
  • 4
    Verify POST Endpoint Availability With Uptime Checks
    • March 24, 2023
  • 5
    Oracle Cloud Infrastructure to Increase the Reliability, Efficiency, and Simplicity of Large-Scale Kubernetes Environments at Reduced Costs
    • March 20, 2023
  • /
  • Platforms
  • Architecture
  • Engineering
  • Programming
  • Tools
  • About

Input your search keywords and press Enter.