There are a lot of languages competing in Web development, but there aren’t many ways to deploy the Web. Simple, it is probably nginx front-end proxy, the middle WebService call program script. Nginx is, needless to say, a high-performance Web server. It is usually used as a reverse proxy server on the front end. The so-called “reverse” and “forward” are just English translation. Proxy service, in short, a request is sent from the LAN through a proxy server and then to a server on the Internet. The proxy for this process is forward proxy. If a request comes in from the Internet, it goes to a proxy server and is forwarded by the proxy server to the target server on the LAN, the proxy server is the reverse proxy (as opposed to forward). Forward proxy: {client — “Proxy Server} –” server Reverse proxy: Client — “{proxy server –” server} {} Indicates that THE LAN Nginx can perform both forward and reverse operations. The WebService approach also has many ways. Common ones are FastCGI, WSGI, etc. We used Gunicorn as the WSGI container. Python is the server script, using the Flask framework. The Supervisor is used to manage server processes. Create a python virtual environment: Nginx + Gunicorn + Flask + Supervisor create a Python virtual environment: mkdir myProject Used to create different Python isolation environments within a system. It doesn’t affect each other, and it’s incredibly simple to use. Mkdir myProject CD myProject Virtualenv venv Source venv/bin/activate Install python Web framework — Flask Flask is a Python Web Micro framework. Simple, efficient and easy to use. Flask relies on two libraries werkzeug and Jinjia2. You can install it in PIP mode. PIP Install Flask tests whether our Flask installation was successful and writes a simple Web service using Flask. vim myapp.py from flask import Flask app = Flask(__name__) @app.route(‘/’) def index(): return ‘hello world’ if __name__ == ‘__main__’: App.debug = True app.run() Start flask Python myapp.py. We have now started up the Web service using flask’s own server. In the production environment, the flask servers cannot meet the performance requirements. Here we use Gunicorn as the WSGI container for deploying Python. Gunicorn PIP is an important tool that Python uses to manage packages. Another best practice is to write the Requirement file every time a library is installed using PIP. The requirement file allows you to know what libraries you have installed and also allows others to install the libraries when they are deployed. PIP freeze > requirements. TXT will be freeze every time PIP installs a new library. After gunicorn is installed, we need to start the flask with Gunicorn. Note that the name in flask starts app.run(), which means that the flask server starts the app. Here we use Gunicorn. Myapp. py is a library file called by Gunicorn. Gunicron-w4-b0.0.0.0:8000 myapp:app Gunicron-w4-b0.0.0.0:8000 myapp:app Gunicron-w4-b0.0.0.0:8000 myapp:app In the deployment of Gunicorn, -w represents how many workers are enabled, and -b represents the access address developed by Gunicorn. To kill gunicorn, run the pkill gunicorn command. However, this is too cumbersome for a development, so another artifact — Supervisor, a tool specially used to manage processes, can also manage system tool processes. Install Supervisor PIP Install Supervisor echo_supervisord_conf > supervisor.conf Modify the Supervisor configuration file Add the Gunicorn process management to the bottom of the myApp Supervisor.conf configuration file (note that my working path is /home/rsj217/rsj217/) [program:myapp] The command = / home/rsj217 / rsj217 / myproject/venv/bin/gunicorn w4 – b0.0.0.0:2170 myapp: app; The supervisor startup command directory = / home/rsj217 / rsj217 / myproject; Project folder path startSecs =0; Start time stopWaitSecs =0; Autostart =false; Autorestart =false; Whether automatic restart stdout_logfile = / home/rsj217 rsj217 / myproject/log/gunicorn log; The log log stderr_logfile = / home/rsj217 / rsj217 / myproject/log/gunicorn err; Conf The supervisor supervisor is running in the supervisor supervisorctl -c supervisor.conf status configuration file Check the status of the supervisor supervisorctl – c supervisor. Conf reload reload the configuration file supervisorctl – c supervisor. Conf start [all] | (appname) Start supervisorctl specifies the program all the supervisor/management process – c supervisor. Conf stop [all] | (appname) close the supervisor specifies the program all the supervisor/management process There is also a Web admin interface that can be activated. Change the configuration [inet_http_server]; Inet (TCP) server disabled by default port=127.0.0.1:9001; (ip_address:port specifier, *:port for all iface) username=user ; (default is no username (open server)) password=123 ; (default is no password (open server)) [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; Use a Unix :// URL for a Unix socket serverurl=http://127.0.0.1:9001; use an http:// url to specify an inet socket username=user ; should be same as http_username if set password=123 ; should be same as http_password if set ; prompt=mysupervisor ; cmd line prompt (default “supervisor”) ; history_file=~/.sc_history ; Use readline history if available now starts Gunicorn using Supervsior. Run the supervisord-c supervisor.conf command to view the Supervisor web management interface of http://127.0.0.1:9001. Nginx is easiest to install using apt-get. Run sudo apt-get install nginx. The installed binaries for nginx are placed under the /usr/sbin/folder. The nginx configuration file is placed under /etc/nginx. Use Supervisor to manage nginx. There is one issue to be aware of here, Linux permissions. Nginx is installed using sudo and starts supervisor as user root. Therefore, we need to start Supervisor as user root. Add the following configuration file [Program :nginx] command=/usr/sbin/nginx startSecs =0 stopWaitSecs =0 autostart=false Autorestart =false stdout_logfile=/home/rsj217/rsj217/myproject/log/nginx.log stderr_logfile=/home/rsj217/rsj217/myproject/log/nginx.err At this point, the progressive Web ministry is complete. Eventually, of course, we need to farm the project code to the server. Batch automation requires another artifact, Fabric. The specific use of fabric is not covered in this note. The fabric file is included in the project source code. Download fabric, change the user name and secrets in it, and you can deploy it on your own or a remote server.

For more free technical information: annalin1203