Most of my career is using Microsoft's architecture. Recently I decided to step out of the comfort zone of technology and enter the Open Source Software world. My daily work project is a RESTful service, which needs to run on mainstream hardware and be horizontally expanded as needed. To do this, I decided to use Flask and Nginx. Flask is a lightweight PythonWeb framework. Nginx is a very stable Web server that works well on cheap hardware platforms. In this article, I will guide you through using Ngin
Most of my career is using Microsoft's architecture. Recently I decided to step out of the comfort zone of technology and enter the Open Source Software world. My daily work project is a RESTful service, which needs to run on mainstream hardware and be horizontally expanded as needed. To do this, I decided to use Flask and Nginx. Flask is a lightweight Python Web framework. Nginx is a very stable Web server that works well on cheap hardware platforms.
In this article, I will guide you through the installation and configuration process of using the Nginx server to host the Flask application. The operating system I use is Ubuntu 13.04.
Prerequisites
Install some prerequisite software before installing Nginx and other required software. First, we need PIP and virtualenv:
Sudo apt-get install python-setuptools
Sudo easy_install pip
Sudo pip install virtualenv
To install Nginx with apt-get, we need to add the Nginx library to apt-get source:
Sudo add-apt-repository ppa: nginx/stable
Note: If the "add-apt-repository" command does not exist in your Ubuntu version, install the "software-properties-common" package and run the following command: sudo apt-get software-properties-common (thanks to get_with_it mentioned in the comment)
Upgrade existing packages to ensure that uWSGI compilers and tools are available on the system:
Sudo apt-get update & sudo apt-get upgrade
Sudo apt-get install build-essential python-dev
Nginx
Install and run Nginx:
Sudo apt-get install nginx
Sudo/etc/init. d/nginx start
Nginx is a web service that provides static file access. However, it cannot directly host Python applications, and uWSGI solves this problem. Let's install uWSGI first, and then configure interaction between Nginx and uWSGI later.
Sudo pip install uwsgi
Milestone #1
Open your browser to access your server. You should be able to see the Nginx welcome page:
Example Application
The hosted applications are the classic "Hello, world !". This application has only one page. You have already guessed what the page contains. Store all application-related files in the/var/www/demoapp folder. Create this folder and initialize a virtual environment:
Sudo mkdir/var/www
Sudo mkdir/var/www/demoapp
Because we have created this folder with the root permission, it is currently owned by the root user. Let's change its ownership to the user you log on to (ubuntu in my example)
Sudo chown-R ubuntu: ubuntu/var/www/demoapp/
Create and activate a virtual environment and install Flask:
Cd/var/www/demoapp
Virtualenv venv
. Venv/bin/activate
Pip install flask
Use the following code to create the hello. py file:
From flask import Flask
App = Flask (_ name __)
@ App. route ("/")
Def hello ():
Return "Hello World! "
If _ name _ = "_ main __":
App. run (host = '0. 0.0.0 ', port = 8080)
Milestone #2
Let's execute the script we just created:
Python hello. py
Now you can access the port 8080 of your server through a browser. The application takes effect:
Note: because port 80 has been used by Nginx, port 8080 is used here.
Currently, applications are hosted by the built-in web Service of Flask. It is indeed a good tool for development and debugging, but it is not recommended in the production environment. Let's configure Nginx to shoulder this burden.
For more details, refer to the highlights on the next page.: Http://www.linuxidc.com/Linux/2015-09/123597p2.htm
Ubuntu Server 14.04.2 LTS configure Nginx + Uwsgi + Django http://www.linuxidc.com/Linux/2015-04/116397.htm
Deployment of Nginx + uWSGI + Flask http://www.linuxidc.com/Linux/2014-01/96007.htm in Ubuntu 12.04
You should use Nginx + uWSGI http://www.linuxidc.com/Linux/2013-07/87286.htm
UWSGI + Nginx deploy Flask Web application http://www.linuxidc.com/Linux/2013-06/85828.htm
Django + Nginx + uWSGI deployment http://www.linuxidc.com/Linux/2013-02/79862.htm
Linux Nginx + uWSGI deployment Python application http://www.linuxidc.com/Linux/2012-10/72443.htm
Install Nginx + uWSGI + Django environment http://www.linuxidc.com/Linux/2012-05/60639.htm on Ubuntu Server 12.04
CentOS 5.5 + Nginx 0.8.50 + uWSGI + Django 1.2.3 deploy Django project http://www.linuxidc.com/Linux/2011-05/36399.htm