2017.07.14 Flask uses port 80 service, Nginx+uWSGI

1. Operating system environment: Ubuntu Server 16.04.1 LTS 64-bit

2. Preparation before installation:

(1) Install Python environment

Next is python. The default environment of Ubuntu has pre-installed python 2.7, so you only need to install the pip installation tool of python. pip is used to install some software tools based on python applications, which will be used frequently in the following.

PIP

If you use python but don’t understand [pip|http://pypi.python.org/], you’d better make up your mind quickly, the instructions are as follows:

sudo apt-get install pip<br>

(2)VirtualEnv

Different projects may refer to various dependent packages, in order to avoid “dependency hell” caused by conflicts between versions and applications
[Virtualenv | https://virtualenv.readthedocs.org/en/latest/] is a must for our python project. VirtualEnv can create an independent development environment for each Python application, so that they do not affect each other, Virtualenv can do:

  • Install new packages without permissions
  • Different applications can use different kit versions
  • Kit upgrade does not affect other applications

Install:

sudo pipinstall virtualenv</code><br><br>

(3) Install 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 execute hosted Python applications, and uWSGI solves this problem. Let’s install uWSGI first and configure the interaction between Nginx and uWSGI later.

(4) Install uWSGI

sudo pip install uwsgi

3. Deploy and create file structure:

(1) The application we will be hosting is the classic “Hello, world!”. This app has only one page, so you can already guess what will be on the page. Store all application-related files in the /var/www folder. Let’s create this folder and initialize a virtual environment in it:

sudo mkdir /var/www

(2) Since we created this folder with root privileges, it is currently owned by the root user, let’s change its ownership to the user you are logged in with (ubuntu in my case): //Give permissions This step is very important

sudo chown -R ubuntu:ubuntu /var/www/

(3) Create and activate a virtual environment, install Flask in it: you can also not add sudo, if it fails, add sudo and try again

cd /var/www //Enter the folder where the web is located

sudo virtualenv venv //Create a virtual environment

sudo source venv/bin/activate //Activate the virtual environment

pip install flask

(4) Use the following code to create a hello.py file in the /var/www directory:

#coding=utf-8
from flask import Flask

app = Flask(__name__)

@app.route(‘/’)
def index():
return ‘

Hello World!

if __name__ == ‘__main__’:
app.run(host=’0.0.0.0′,port=8080)

(5) Test the script, let us execute the script we just created, and execute it in the /var/www directory: python hello.py

Note: Because port 80 is already used by Nginx, here I use port 8080.

Now the application is hosted by Flask’s built-in web service, which is really a good tool for development and debugging, but it is not recommended for use in a production environment. Let’s configure Nginx to do the heavy lifting.

(6) Configure Nginx

Directly replace the default file in the /etc/nginx/sites-enabled/ directory

Create a new default file:

server {
listen 80; //Listen to port 80
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location / { try_files $uri @yourapplication; }
location @yourapplication {
include uwsgi_params;
uwsgi_pass unix:/var/www/demoapp_uwsgi.sock; //No at present, configure this file later
}
}

Replace the original /etc/nginx/sites-enabled/ directory with the new default file. After the default file, restart the service:

sudo /etc/init.d/nginx restart

(7) Access the public ip address of the server, you will see an error:

Don’t worry, this error is normal, it means that Nginx is already using our newly created configuration file, but is having trouble linking to uWSGI, our Python application gateway. The link to uWSGI is defined on line 10 of the Nginx configuration file:

uwsgi_pass unix:/var/www/demoapp/demoapp_uwsgi.sock;

This means that the link between Nginx and uWSGI is through a socket file, which is located at /var/www/demoapp/demoapp_uwsgi.sock. Since we haven’t configured uWSGI yet, this file doesn’t exist yet, so Nginx returns a “bad gateway” error, let’s fix that right away.

(8) Configure uWSGI: Create a file in the /var/www directory: demoapp_uwsgi.ini:

[uwsgi]
#application’s base folder base = /var/www
#python module to importapp = hello
module = %(app)
home = %(base)/venv
pythonpath = %(base)
#socket file’s locations socket = /var/www/%n.sock
#permissions for the socket file
chmod-socket=666
#the variable that holds a flask application inside the module imported at line #6
callable = app
#location of log files
logto = /var/log/uwsgi/%n.log

Create a new folder to store uWSGI logs, change the ownership of the folder:

sudo mkdir -p /var/log/uwsgi

sudo chown -R ubuntu:ubuntu /var/log/uwsgi

(9) Execute uWSGI in the /var/www directory, using the newly created configuration file as a parameter:

uwsgi –ini /var/www/demoapp_uwsgi.ini

Next access your server, and now Nginx can connect to the uWSGI process:

We are now almost done, the only thing left is to configure uWSGI to run in the background;

So far, the file structure under /var/www is as follows:

4. Now we let it run in the background. It is the actual need of the operating environment to make it start with the server and run as a background service. So next we need to install another tool to boot uwsgi.