aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • 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
    • Learning
  • Tools
  • About
  • Software Engineering
  • Tech

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

  • aster.cloud
  • 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.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

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  Retailers Unwrap A Successful 2021 Holiday Season With Google’s Black Friday/Cyber Monday Program

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) ubuntu@{host}:~$ cd /var/www/geek_flask_app
(geek-venv) ubuntu@{host}:~$ pip install -r requirements.txt
(geek-venv) ubuntu@{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”.


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

You May Also Like
Getting things done makes her feel amazing
View Post
  • Computing
  • Data
  • Featured
  • Learning
  • Tech
  • Technology

Nurturing Minds in the Digital Revolution

  • April 25, 2025
View Post
  • Software Engineering
  • Technology

Claude 3.7 Sonnet and Claude Code

  • February 25, 2025
View Post
  • Tech

Deep dive into AI with Google Cloud’s global generative AI roadshow

  • February 18, 2025
View Post
  • Engineering
  • Software Engineering

This Month in Julia World

  • January 17, 2025
View Post
  • Engineering
  • Software Engineering

Google Summer of Code 2025 is here!

  • January 17, 2025
Volvo Group: Confidently ahead at CES
View Post
  • Tech

Volvo Group: Confidently ahead at CES

  • January 8, 2025
zedreviews-ces-2025-social-meta
View Post
  • Featured
  • Gears
  • Tech
  • Technology

What Not to Miss at CES 2025

  • January 6, 2025
View Post
  • Tech

IBM and Pasqal Plan to Expand Quantum-Centric Supercomputing Initiative

  • November 21, 2024

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • oracle-ibm 3
    IBM and Oracle Expand Partnership to Advance Agentic AI and Hybrid Cloud
    • May 6, 2025
  • 4
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 5
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 6
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 7
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 8
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 9
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 10
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
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
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
  • 2
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 3
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 4
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 5
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.