• Flask Python’s server framework
  • Gunicorn WebService, a WSGI container
  • Supervisor Process management tool
  • Nginx is a high-performance Web server

Create a project

mkdir server
Copy the code

Create a path for your application

Build Python’s virtual environment

We use VirtualEnv to build a separate Python isolation environment on a system. It is very easy to use virtualEnv

cd server // cdSwitch to our project directory virtualenv venv // to build our virtual environmentCopy the code

Once the VENv environment is created, we need to activate it to use it (sometimes automatically). Once activated, we can see (VENv) in front of the console.

source venv/bin/activate
Copy the code

Close the environment and use deactivate directly

deactivate
Copy the code

Install flask frame

PIP is automatically installed in the virtual environment, so you can easily install flask using PIP

pip install flask
Copy the code

Flask has been installed and we can test our flask framework with a small application, ‘vim myapp.py’ creating a Python file

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index(a):
    return 'hello world ! '

if __name__ = '__main__':
    app.debug = True
    app.run()
Copy the code

The startup script

python myapp.py
Copy the code

At this point, use your browser to visit http://127.0.0.1:5000 and you will see hello World!

Deploy Python Web using Gunicorn

Flask’s own server has just been opened and the Web service has been started. Flask’s own servers, however, are generally used for debugging and perform poorly. Here we use Gunicorn as the WSGI container for deploying Python.

Install the gunicorn
pip install gunicorn
Copy the code

PIP is python’s management package tool. As your project grows, you’ll find that its dependency list grows as well. It’s not surprising that you need tens of dependency packages before you can run a Flask application. The easiest way to manage dependencies is to use a simple text file. PIP can generate a text file that lists all installed packages. It can also parse the file and install each package on a new system (or new environment).

pip freeze > requirements.txt Create a TXT file

pip install -r requirements.txt You can install all the packages directly when others use them
Copy the code

In the future, each time PIP installs a new library, a freeze will be required to ensure the update

Then we start the Flask with Gunicorn

gunicorn -w 4 -b 0.0. 0. 0:8000 myapp:app
Copy the code

At this point, port 8000 is used for access. -w represents how many workers are enabled, and -b represents the access address. Myapp is the filename of myapp.py, and mypp.py is a library file called by Gunicorn. The app is created in Myapp. py so that Gunicorn can locate the Flask app. Gunicorn can be killed by executing pkill Gunicorn, and sometimes by finding the PID. This operation is too tedious, so we use another supervisor to manage the process of the system.

The installation supervisor

pip install supervisor
echo_supervisord_conf > supervisor.conf Generate the Supervisor default configuration file
vim supervisor.conf Modify the Supervisor configuration file to add the Gunicorn process management
Copy the code

At the bottom of the supervisor.conf configuration file add (note that my working path is /var/www/server)

[program:myapp]
command= / var/WWW/server/venv/bin/gunicorn w4 - b0.0.0.0:2170 myapp: app; Supervisor Start command directory=/var/www/server; Project folder path startSecs =0; Start time stopWaitSecs =0; End wait time autostart=false; Whether to automatically start autorestart=false; Whether to automatically restart stdout_logfile=/var/www/server/log/gunicorn.log                           ; logLog stderr_logfile = / var/WWW server /log/gunicorn.err  
Copy the code

The log directory is used to record logs. We need to create a log directory first, otherwise we will encounter an error

mkdir log
Copy the code

##### Supervisor basic usage commands

Conf Is designed to run the supervisor supervisorctl -c supervisor.conf status supervisorctl - c supervisor. Conf reload reload the configuration file supervisorctl - c supervisor. Conf start | [all] [appname] started to specify all the supervisor/management process Supervisorctl - c supervisor. Conf stop [all] | (appname) close to specify all the supervisor/management processCopy the code

Deployed Nginx

Nginx is a high-performance HTTP and reverse proxy server that performs well with high concurrency.

Install nginx

sudo apt-get install nginx
Copy the code

After nginx is installed, you can use the following commands to enable and disable nginx

D /nginx restart sudo /etc/init.d/nginx start Sudo /etc/init.d/nginx stop Stop the serviceCopy the code

# # # # configure nginx

cd /etc/nginx/sites-available/default
cd /etc/nginx/sites-enabled/default
Copy the code

This is the nginx application specific configuration file, easy to manage. Modify the default file

server {
  Listen on port 80
    listen 80;
# define access using www.xx.comserver_name www.app.com; // Address (http://118.89.235.150/) client_max_body_size 10M;# Set the access log of this virtual host
    access_log logs/app.log main;
 
  # default request
    location / {
        Request redirected to local IP :8888Proxy_pass http://0.0.0.0:8000; proxy_redirect off; proxy_set_header Host$host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    Configure static file forwarding
    location ~.*(js|css|png|gif|jpg|mp3|ogg)$ {
        root /home/zhoujianghai/temp/data/app/medias/;
    }
    Configure static page forwardinglocation ~.*(html)$ { root /home/zhoujianghai/temp/data/app/app_static_pages/; }}Copy the code

Restart your Nginx to access your application in your browser at http://118.89.235.150/.

# # # reference:

  • Flask+Nginx+Gunicorn+Redis+Mysql build a small station

  • Python Web deployment: Nginx + Gunicorn + Supervisor + Flask Deployment notes

  • Flask trip