rest/flask/nginx/uwsgi/supervisor

Keywords: Python Nginx supervisor pip

Thank you for the original author http://www.cnblogs.com/Ray-liang/p/4173923.html?Utm_source=tuicool&utm_medium=referral


My deployment plan is:

  • Web server adopts uwsgi host Flask
  • Using Supervisor to Refer to uwsgi as a Routine Startup Service
  • Reverse proxy based on Nginx

First, Aliyun servers can be remotely connected to local terminals through SSH instructions.

ssh root@cloud server address

After entering the password, all operations are completely one-to-one with the local terminal.

Install the Python environment

Next comes python. Ubuntu's default environment is pre-installed with python 2.7, so you just need to install python's pip installation tool. pip is used to install some software tools based on Python applications, which will be used frequently in the following sections.

PIP

If you use python and don't understand [pip | http://pypi.python.org/ It's better to make brain repairs as soon as possible. The instructions are as follows:

sudo apt-get install pip

VirtualEnv

Different projects may refer to different dependency packages to avoid "dependency hell" caused by conflicts between versions and applications.
[Virtualenv | https://virtualenv.readthedocs.org/en/latest/ It's a must for our Python project. VirtualEnv can create a separate development environment for each Python application so that they don't interfere with each other, Virtualenv Can do:

  • Install a new suite without permission
  • Different applications can use different suite versions
  • Suite upgrades do not affect other applications

Installation:

sudo pip install virtualenv

After installing VirtualEnv, you only need to run virtualenv in the project directory The directory name can create a virtual environment folder, and then enable the python virtual environment by enabling the activate command, as follows:

Assuming my project directory is / home/www/my_flask, first install the virtual environment (The virtual environment directory I'm used to using is called venv)

my_flask root$ virtualenv venv

>> New python executable in venv/bin/python
>> Installing setuptools, pip...done.

A new venv directory will be created under the project directory, which contains tools and instructions, and packages for running python's basic environment. Then enabling the environment, using the current command line state to enter the virtual environment, after entering the virtual environment, all operations installing Python will package and reference in the virtual environment, without affecting the overall Python environment.

my_flask root$ source venv/bin/activate 

(venv)my_flask root$ 

After invoking the activate command, the command appears before the command (venv) wording. You can exit the virtual environment through deactivate.

Install uWSGI

Flask's actual production and operation environment choice is not many, the more mature is [Gunicorn | | | | | uuuuuuuuu http://gunicorn.org/ ] and [uWSGI | https://github.com/unbit/uwsgi] I heard that the configuration of Gunicorn is very simple, but unfortunately I haven't been able to successfully configure it. Now I use uWSGI. Next, install uWSGI.

(venv)my_flask root$ pip install uwsgi

Suo is not required in a virtual environment because virtualenv There is no right to ask.

This installation is very fast, basically in seconds. After installation, we can put down the uWSGI table and configure it back later, because we first need to transfer the key Flask environment and our project files to the server directory.

Install Flask

I installed Flask and its dependencies in a single install using the manifest file, which would be faster. My citation list is as follows:
requirements.txt

Flask==0.10.1
Flask-Login==0.2.11
Flask-Mail==0.9.1
Flask-Moment==0.4.0
Flask-PageDown==0.1.5
Flask-SQLAlchemy==2.0
Flask-Script==2.0.5
Flask-WTF==0.10.2
Flask-Cache==0.13.1
Flask-Restless==0.15.0
Flask-Uploads==0.1.3
Jinja2==2.7.3
Mako==1.0.0
Markdown==2.5.1
MarkupSafe==0.23
SQLAlchemy==0.9.8
WTForms==2.0.1
Werkzeug==0.9.6
html5lib==1.0b3
itsdangerous==0.24
six==1.8.0
awesome-slugify==1.6

You can imagine if one pretends not to be mad.
Install manifest file:

(venv)my_flask root$ pip install -r requirements.txt

Here again, it is important to note that the python virtual environment is enabled before running this installation, otherwise it will be installed directly to the whole world! uuuuuuuuuuu

Project documents

Next is to upload Flask project files, before I found a lot of information in the major "reprint professional users". In this step, most of them just add a standard Flask running file on it. Although it is possible to make an example, it is really confusing. Why? Look at the code first.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

In the production environment, who will use such code? This is only the most basic example of Flask. Apps in my Flask project are made in packages. I believe many people do this. Outside the packages, we use Flask Script to write a management.py file. As a startup file, it is more convenient to support various projects, including FastCGI, which can be installed in Windows.

So my manager.py looks like this:

#!/usr/bin/env python
import os

if os.path.exists('.env'):
    print('Importing environment from .env...')
    for line in open('.env'):
        var = line.strip().split('=')
        if len(var) == 2:
            os.environ[var[0]] = var[1]

from app import create_app
from flask.ext.script import Manager, Shell

# Create app s through configuration 
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)

def make_shell_context():
    return dict(app=app)


manager.add_command("shell", Shell(make_context=make_shell_context))

@manager.command
def deploy():
    """Run deployment tasks."""
    pass


if __name__ == '__main__':
    manager.run()

In this way, our development environment can run Flask as follows:

python manage.py runserver

For more details, please refer to [Flask Script | http://flask-script.readthedocs.org/en/latest/ Expand

Well, we can upload Flask project files to the server using FTP tools that support SSH. Here is the whole project file structure:

www/
└── my_flask  
│   ├── logs
│   └── venv  //Virtual directory
│   │   ├── bin
│   │   │         ├── activate
│   │   │         ├── easy_install
│   │   │         ├── gunicorn
│   │   │         ├── pip
│   │   │         └── python
│   │   ├── include
│   │   │          └── python2.7 -> /usr/include/python2.7
│   │   ├── lib
│   │   │         └── python2.7
│   │   ├── local
│   │   │         ├── bin -> /home/shenye/shenyefuli/bin
│   │   │         ├── include -> /home/shenye/shenyefuli/include
│   │   │         └── lib -> /home/shenye/shenyefuli/lib
│   └── app  //Flask Program Directory
│   │           └──  __init__.py //This is the package file. There are other files in this directory that are skipped here.
│   ├── manage.py   
│   ├── requirements.txt                

Configure uwsgi

Now that the project is ready, it's time to go back and configure uwsgi. Its specific instructions can be seen in its official documents. Here we use one of the instructions: configure startup. I use the.Ini file as the configuration to create a confg.ini (see below) in the project directory that can be executed after it is written.

(venv)my_flask root$ uwsgi config.ini

I think it's the easiest way and easy to change. Well, here comes the important part, config.ini wrote as follows:

[uwsgi]

# Address and port used when uwsgi is started
socket = 127.0.0.1:8001 

# Point to the website directory
chdir = /home/www/ 

# python starter file
wsgi-file = manage.py 

# application variable name to start in python program
callable = app 

# Processor number
processes = 4

# Thread count
threads = 2

#Status Detection Address
stats = 127.0.0.1:9191

Note: callable=app This app is A variable in the management.py program file whose type is Flask's application class.

Run uwsgi

"`
(venv)my_flask root$ uwsgi config.ini

[uWSGI] getting INI configuration from config.ini

*** Starting uWSGI 2.0.8 (64bit) on [Fri Dec 19 14:34:11 2014] 
// Leave out the useless startup information here
Stats server enabled on 127.0.0.1:9191 fd: 15 ***
"`

OK, uwsgi has been started and the Flask project has been loaded into it. ctrl+c closes the program. But this is only the form of command startup. It is the actual requirement of the operating environment to make it start with the server and run as a back-end service. So next we need to install another tool to boot uwsgi.

Install Supervisor

[Supervisor|http://supervisord.org/configuration.html Multiple applications can be launched at the same time, and most importantly, when an application Crash, it can automatically restart the application to ensure availability.

sudo apt-get install supervisor

Supervisor's global configuration file location is:

/etc/supervisor/supervisor.conf

Normally we don't need to make any changes to it, just add a new *. conf file to it.

/etc/supervisor/conf.d/

Next, we will create a new supervisor configuration for uwsgi to start my_flask project (named my_flask_supervisor.conf):

[program:my_flask]
# Start command entry
command=/home/www/my_flask/venv/bin/uwsgi /home/www/my_flask/config.ini

# The directory where the command program is located
directory=/home/www/my_flask
#User name to run commands
user=root

autostart=true
autorestart=true
#Log address
stdout_logfile=/home/www/my_flask/logs/uwsgi_supervisor.log        

Startup service

sudo service supervisor start

Termination of service

sudo service supervisor stop

Install Nginx

[Nginx|http://nginx.com/ It is a lightweight, high performance, less resource-consuming reverse proxy software that can handle high concurrency well.

sudo apt-get install nginx

Configure Nginx

Configuring Nginx on Ubuntu is also simple. Don't change the default nginx.conf, just add

/ext/nginx/sites-available/default

Files can be replaced.

Create a new default file:

    server {
      listen  80;
      server_name XXX.XXX.XXX; #alternate public address

      location / {
        include      uwsgi_params;
        uwsgi_pass   127.0.0.1:8001;  # Point to the internal address of the uwsgi application, and all requests will be forwarded to the uwsgi process
        uwsgi_param UWSGI_PYHOME /home/www/my_flask/venv; # Point to the virtual environment directory
        uwsgi_param UWSGI_CHDIR  /home/www/my_flask; # Point to the website root directory
        uwsgi_param UWSGI_SCRIPT manage:app; # Specify a starter
      }
    }

Replace the default configuration file and you're done!
Also, to change the configuration, remember to restart nginx:

sudo service nginx restart

summary

Aliyun's server runs very fast. With Flask+uWSGI+nginx, this high-performance structure, the whole website responds very fast, with a response speed of less than 0.1s per page. C ompared with the Web site I used to deploy on Avarix (the same program, implemented in different languages), the response speed per page is at least 2s, which is almost 200:1 performance, of course, I haven't been idle yet. We use software to measure the difference between the two. The purpose of this paper is also to record it for future review.

                </div>

Posted by ahmedkl on Thu, 03 Jan 2019 15:51:10 -0800