python deployment has always been a problem, but now it's 2018, and there are many mature solutions. The most commonly used is nginx + upervisor + virtualenv + gunicorn / uwsgi.
The advantage is that it is very convenient to control program start / stop / restart, especially when you have many services; but abusing restart is another problem, which is not necessarily to hope you abuse it.
It is divided into two parts:
Supervisor CTL: client
socket or http can be configured for communication between client and server. If it is a stand-alone device, this configuration is not needed, as follows:
[supervisorctl] serverurl=unix:///tmp/supervisor.sock ;
For multiple computers, it is better to configure http mode communication as follows:
[supervisorctl] serverurl=http://127.0.0.1:9001 ;
pip install supervisor
Note: at present, the supervisor does not support python3, and can only use python2, but does not affect its services that control python3 or even Java PHP.
sudo mkdir /etc/supervisor echo_supervisord_conf > /etc/supervisor/supervisord.conf
[program:Project name] command=python3 run.py; Program start command autostart=true ; stay supervisord It starts automatically when it starts startsecs=10 ; If there is no abnormal exit after 10 seconds of starting, it means that the process is started normally. The default is 1 second autorestart=true ; Automatic restart after program exit,Optional values:[unexpected,true,false]，Default is unexpected，Indicates that the process is not restarted until it is accidentally killed startretries=3 ; Number of automatic retries for startup failure, default is 3 user=tomcat ; Which user is used to start the process? The default is root priority=999 ; Process start priority, 999 by default, priority start with small value redirect_stderr=true ; hold stderr Redirect to stdout，default false stdout_logfile_maxbytes=20MB ; stdout Log file size, default 50 MB stdout_logfile_backups = 20 ; stdout Number of log file backups, default is 10 ; stdout For log files, it should be noted that when the specified directory does not exist, it cannot be started normally, so you need to create the directory manually( supervisord Automatically create log file) stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out stopasgroup=false ;Default is false,Whether to send to this process group when the process is killed stop Signals, including subprocesses killasgroup=false ;Default is false，Send to process group kill Signals, including subprocesses
Suppose I have a python server, such as flask and cellery.
[program:celery] command=celery -A application.celery worker -c 6 --beat -l info directory=/home/rd/username/X_server/ autostart=true autorestart=true stopsignal=KILL redirect_stderr=true priority=1 stopasgroup=true killasgroup=true [program: X_server] command=gunicorn -b 0.0.0.0:5000 -w 4 run:app info --error-logfile logs/error.log --log-file logs/app.log --access-logfile logs/access.log directory=/home/rd/username/X_server/ autostart=true autorestart=true stopsignal=KILL priority=10 redirect_stderr=true
priority is configured here to ensure that you can start cellery first, and then start x? Server.
python2 /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
Start / stop / restart, "all" represents all services.
supervisorctl start celery supervisorctl stop celery supervisorctl restart celery supervisorctl start X_server supervisorctl stop X_server supervisorctl restart X_server supervisorctl restart all
WEB management page
Generally, it is not used, but if there are too many services, it is another matter. Modify the configuration as follows:
[inet_http_server] port=0.0.0.0:9001 ; ip & port The default is 9001. If it's occupied, change it username=user password=123 ; There is no user name or password by default. Add it if necessary
Then access to port 9001.