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
  • Programming

How To Create A Simple Python Web App Using NGINX, uWSGI, And Flask – Part 03 of 03

  • root
  • August 23, 2019
  • 2 minute read

Overview

This is the 2nd part of the 3-part guide on Installing, Configuring and Deploying a Python Web/Flask application on an Ubuntu system that uses NGINX and uWSGI. For the installation part go here. For the configuration part go here.

 

Prerequisites

  • Ubuntu 18.04
  • Python 3.x has been installed. For instructions see here.
  • Pip and Virtual Environment has been installed. For instructions see here.
  • Installed NGINX, uWSGI, and uWSGI-Emperor from previous guides.
  • Configured NGINX, uWSGI and uWSGI-Emperor from 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

 

/var/www/geek_flask_app/api/__init__.py content

from flask import Flask

application = Flask(__name__)

import api.res.endpoints

 

/var/www/geek_flask_app/app_wsgi.py

from api import application

 

/var/www/geek_flask_app/api/res/endpoints.py

# -*- 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)

 

/var/www/geek_flask_app/requirements.txt

flask

 

02. Install python libraries that is required by your application.

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/flask_app

(geek-venv) [email protected]{host}:~$ pip install -r requirements.txt

(geek-venv) [email protected]{host}:~$ deactivate

 

03. Reload the uwsgi-emperor service.

$ sudo service uwsgi-emperor reload

 

Read More  Calling Google APIs From Serverless (Part I: Cloud APIs)

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. Restart the NGINX server

$ sudo service nginx restart

 

06. Verify using cURL on the NGINX.

$ curl http://localhost

 

It should also show the same response

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

 

Next steps 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”.

Click here for Part 01 and here for Part 02.

root

Related Topics
  • App Deployment
  • Flask
  • How To
  • NGINX
  • Python
  • uWSGI
You May Also Like
View Post
  • Automation
  • Programming

Learn Expect By Writing And Automating A Simple Game

  • March 14, 2023
SQL
View Post
  • Data
  • Programming

Infrastructure from Code: the New Wave of Cloud Infrastructure Management

  • February 16, 2023
View Post
  • Programming

Go 1.20 Is Released!

  • February 13, 2023
View Post
  • Computing
  • Programming

Tiny Snippets Of Code That Changed The World

  • January 23, 2023
View Post
  • Computing
  • Programming

How To Migrate Your Code From PHP 7.4 to 8.1

  • December 26, 2022
View Post
  • Programming
  • Software Engineering

10 Tips For Writing Clean Code

  • December 15, 2022
View Post
  • Programming
  • Software
  • Technology

Compose For Wear OS 1.1 Is Now Stable: Check Out New Features!

  • December 12, 2022
View Post
  • Architecture
  • Programming

Introducing The Architecture Templates

  • December 12, 2022

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.