• After browsing a large number of online articles, consulting relevant documents and testing myself, I finally wrote this article.
  • System: Debian9 x64
  • Python version: Python3.5 +
  • Install and configure using root – saves sudo the hassle.

1. Obtain the latest software package or upgrade all installed software packages

  • apt-get update
  • apt-get upgrade

Pip3 (Python3.5 installed in Debian9)

  • apt-get install python3-pip

Install: Python virtualEnv

  • pip3 install virtualenv

Install Nginx

  • apt-get install nginx

Install: UWSGi (note that piP3 is used and does not need to be installed in a virtual environment)

  • pip3 install uwsgi

Create a virtual environment: VirtualEnv

  • The requirements.txt package in the real project is not used for debugging purposes
  • Create a virtual environment under SRV
  • cd /srv
  • Check the python3 path
  • which python3
  • Flask-env = flask-env = flask-env = flask-env = flask-env
  • virtualenv -p /usr/bin/python3 flask-env
  • Install flask in the virtual environment because you need it for debugging
  • . flask-env/bin/activate
  • pip install flask(PIP is used for both PY2 and Py3 in virtual environments)
  • deactivate(Exit after successful installation)
  • Create a project under SRV (after tuning, replace with a production project)
  • mkdir myproject

Create a flask project for debugging (after debugging successfully, pull the project from Git or Svn)

  • cd /myproject
  • vim run.py
from flask import Flask app = Flask(__name__) @app.route("/") def helloWorld(): Return "Hello World" if __name__ = = "__main__ ': app. The run (host =' 0.0.0.0 ', the port = 5000)Copy the code
  • Save the configuration and exit: esc: wq

Test Nginx

  • Activation: Nginx
  • service nginx start
  • Access IP +80 in the browser, for example, 59.12.32.4:80
  • If the following page is displayed, the system succeeds

Use: UWSGI to start the project

  • Create the uwsGi configuration file in the project root directory. Ini: File name: whatever I want to call it: test.ini
  • cd /srv/myproject/
  • vim test.ini
  • Chdir: path of the project
  • The name of the startup file does not require.py
  • Home: path to Python’s virtual environment
  • Master: Indicates whether to enable the mater mode
  • Processes: The number of processes (depending on your server’s configuration)
  • Socket: socket file address used to communicate with Nginx (do not need to be manually created · just set)
  • Wsgi-file: wsgi file (equivalent to the flask startup file run.py)
  • Callable: APP variable in wsGi file (i.e., flask instance)
  • Chmod – socket:
  • Plugins: Call the python35 plug-in
  • Log-maxsize: Cuts log files with a fixed file size (in KB). For example, if log-maxsize = 50000000, a log file is 50M
  • Disable-logging: indicates that request information is not logged. Only errors and uWSGI internal messages are logged.
  • Pidfile: Specifies the location of the PID file and records the PID number of the main process (this item can be omitted).
  • Vacuum: Automatically clean the environment when the server exits, and remove Unix socket files and PID files (try to remove all of the generated files /sockets).
[uwsgi]
chdir           = /srv/myproject
module          = run
home            = /srv/flask-env
master          = true
processes       = 10
socket          = /srv/myproject/myproject.sock
wsgi-file       = /srv/myproject/run.py
callable        = app
chmod-socket    = 666
plugins 	= python35

log-maxsize     = 50000000
disable-logging = true
pidfile         = /tmp/uwsgi.pid
vacuum          = true
Copy the code
  • Save the exit
  • Since you need to communicate with Nginx to start, let’s configure Nginx.

Configuration: Nginx

  • Create the configuration file (name is optional) in the following path: myproject.conf
  • Create multiple profiles (different ports) if you need to use the Nginx proxy for multiple projects
  • cd /etc/nginx/conf.d/
  • vim myproject.conf
  • Upstream myproject: Conversion of requests between the same server using socket communication. Configure the communication mode and name between Nginx and UWSGi. The name is: myProject which is the name of the project
  • server unix:///srv/okc/okc.sock; Nginx uses socket to communicate with UWSGi, pointing to the cocket file in the project directory, which is configured by UWSGi. Automatic runtime creation)
  • Listen: listening port (the port needs to be opened after the configuration; otherwise, it cannot be accessed)
  • Server_name: indicates the IP address or domain name
  • Charset: encoding
  • Client_max_body_size: maximum upload
  • Uwsgi_pass: myproject to be consistent with upstream
  • Include: parameter files
upstream myproject { server unix:///srv/myproject/myproject.sock; } server { listen 8099; Server_name 66.42.100.165; charset utf-8; client_max_body_size 75M; location / { uwsgi_pass myproject; include /etc/nginx/uwsgi_params; }}Copy the code
  • Save and exit
  • Verify that the configuration file works properly using the following command
  • nginx -torservice nginx configtest
  • If the following message is displayed, the configuration is ok

  • Don’t forget to turn on the port number you configured
  • iptables -A INPUT -p tcp --dport 8099 -j ACCEPT
  • If you want to turn it off
  • iptables -A INPUT -p tcp --dport 8099 -j DROP
  • Restart the following Nginx
  • service nginx restart

11. Start the project

  • Go into the project directory
  • cd /srv/myproject/
  • Start the project
  • uwsgi --ini test.ini
  • Open a browser and enter IP+ port number
  • 66.42.100.165:8099

  • At this point our project deployment is complete. Next, we use the Supervisor to manage it.
  • Stop all UWSGi processes first
  • Ctrl + c

12, the supervisor

Ps: this is really a pit, online tutorials are Tm short weight of two kinds of impassability, I refer to a large number of articles – documentation and debugging, intermittent spend nearly two weeks – finally come to the following configuration, I hope big partners carefully read and follow the operation.


  • Supervisor: is a tool for managing processes in Linux written by Python2.
  • It is divided into the server container, which is designed for running processes, and the client container, which is designed for running processes, which is designed for running processes.
  • First, install PIP (although python2 is written, it doesn’t matter to use it. Because Supervisor can also manage non-Python project processes.)
  • apt-get install supervisor
  • cd /etc/supervisor/

  • Supervisord. conf: is the main configuration file
  • Conf. d: the container where the configuration files for each project are stored (each managed project requires a configuration file in this container). If you want to change the directory where the configuration files are stored, it is handled in the container’s [include] container. Generally no modification is required.
  • Creating a Configuration Filecd /etc/supervisor/conf.d
  • vim myproject.confTake any name
  • [Program: myProject] : the name of the application (but this is not mandatory, just a flag. Of course, you can take whatever you want. Just be happy)
  • Command: uwsgi –ini myproject.ini
  • Directory: Indicates the directory of the project
  • Startsecs: The number of seconds to wait at the beginning
  • Stopwaitsecs: The number of seconds to wait before stopping
  • Startretries: indicates the number of attempts
  • Autostart: automatically starts (starts the project when the Supervisor is started)
  • Autorestart: Automatically restarts if the program hangs
  • Stdout_logfile: outputs logs
  • Stderr_logfile: output error log
  • User: Start with that user
  • Stdout_logfile_maxbytes: indicates the maximum size of a logfile
  • Stdout_logfile_backups: number of backup log files
  • Environment: The environment variable of this child is not shared with other child processes (use this command to view the environment variable which python3)
  • Redirect_stderr: If true, the stderr log is merged into the stdout log. Default is false
  • Stopasgroup: a child process that is designed to be managed in the container, and it is designed to be a child process. It’s safe to say that if we just kill the children it is handling, the children it is handling will be orphaned. So we can set an option to kill the entire process group of the entire child process. If set to true, killasGroup will also be set to true. Note that this option sends a stop signal, which defaults to false
  • Killasgroup: similar to stopasGroup, but sends a kill signal.
[program:myproject]
command         = uwsgi --ini /srv/myproject/test.ini
directory       = /srv/myproject
startsecs       = 0
stopwaitsecs    = 0
startretries    = 3
autostart       = true
autorestart     = true
stdout_logfile  = /srv/myproject/super_log/supervisord.log
stderr_logfile  = /srv/myproject/super_log/supervisord.err
user            = root 
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
environment=PYTHONPATH = "/usr/bin/python3"
redirect_stderr = false
stopasgroup     = false
killasgroup     = false
Copy the code
  • Creates the container log directory
  • cd /srv/myproject
  • mkdir super_log
  • Put the configuration file into effect – Restart – View the status.
  • supervisorctl update
  • supervisorctl reload
  • supervisorctl status

  • Appear above ~ project already started
  • Finally, the container is configured with a visual management process for the Web
  • cd /etc/supervisor/
  • vim supervisord.conf
  • Press Shift +G to skip to end add
  • Port: IP address and bond port
  • Username: indicates the administrator name
  • Password: indicates the administrator password
  • If you don’t need a password you can comment it on the front of the machine; No.
  • ; username = user
  • ; password = 666666
 [inet_http_server]
 port = IP:9001   
 username = user 
 password = 666666
Copy the code
  • Put the configuration file into effect. – Restart
  • supervisorctl update
  • supervisorctl reload
  • Note: if presentError: < class 'socket. The error >, [2] Errno No to the file or directory: file: / usr/lib/python2.7 / socket. Py line: 228Don’t panic and restart the server to continue with the update and reload above
  • Open port 9001
  • iptables -A INPUT -p tcp --dport 9001 -j ACCEPT
  • Start IP +9001 port in browser (enter account password if set)

  • If you want to start or restart the Supervisor (your project starts with it) from the server
  • cd /etc/rc.local
  • Add to the line before exit 0 in the configuration fileservice supervisor startSave!!!

Xiii. Suggestions

  • First open a virtual machine to operate, follow my steps to operate once all run with the project.

If you have any questions can comment attached questions yo!

  • Qq-wechat same number: 417993207
  • If you like it, you can praise it. Thank you!