Original address https://www.cyberlight.xyz/pa…
In the previous note, under CentOS, we deployed the python flag website through gunicorn + nginx, and configured HTTPS and nginx visitor logs to implement the gunicorn deployment flag website. We used the method of creating a new independent command window to realize the background operation of gunicorn. However, in the actual production environment, we may need the gunicorn serverStart automaticallyOr system errorRestart in time。 Next, I will record the whole process of deploying the supervisor.
This note is based on the deployment of Python flask website through gunicorn + nginx under CentOS, and the optimization after HTTPS and nginx visitor log are configured. If you haven’t implemented the deployment of gunicorn online yet, please read it first.
Environment: CentOS 7, Python 3.6.8
Enter the CentOS command window and enter the following command to install the supervisor
yum install supervisor
Follow the prompts. After installation, the default location is / etc/ supervisord.conf 。 We need to create a new ini configuration file in the / etc / Supervisor. D directory. Use vi command here to create a new file, as follows
vi /etc/supervisord.d/ xxx.ini #XXX is a custom name
Press keyboard I to enter the editing page and enter the following information (press Shift + insert in SSH client window to paste)
[ program:xxx ];: project name on the right Directory = / www / flask / xxx; the starting directory of the program. This is the root directory of the flask website command = /www/flask/xxx/venv/bin/gunicorn -w 4 -t 30 -b 0.0.0.0:5000 app:app ; Start gunicorn in the virtual environment. Absolute path is recommended here Autostart = true; also starts automatically when supervisor starts Autorestart = true; restart automatically after program exits abnormally stdout_ logfile = /supervisor_ log/ supervisor.log ; log storage path, which needs to be an existing path Startsecs = 5; if there is no abnormal exit after starting for 5 seconds, it will be regarded as started normally Stopwaitsecs = 0; stop waiting time
In the above information
; is annotator
The command line / www / flask / xxx / venv / bin / gunicorn is the absolute path of gunicorn in the virtual environment of the project. Please adjust it flexibly according to the actual path (for gunicorn’s command, see the article at the first link of the note)
After entering the correct information, press the ESC key, the: key, the W key, the Q key, and then enter (save and exit)
At this time, the supervisor has been installed and configured successfully. It seems that the supervisor starts automatically. Enter in the command window
Supervisor CTL reload saves the configuration file and restarts it
As shown in the figure, the supervisor has been restarted
At this time, you can see the XXX running field, which means that the website has been started
Now try restarting the server! The site will start automatically.
If the flask source code is updated, enter “supervisor CTL reload” to restart the new code
The common commands of supervisor are as follows:
Close all tasks
#Start a process
supervisorctl start programxxx
#Restart a process
supervisorctl restart programxxx
#Stop all processes note: start, restart and stop will not load the latest configuration file
supervisorctl stop all
#Load the latest configuration file, stop the original process, start and manage all processes according to the new configuration.
#According to the latest configuration file, start the process with new configuration or changes. The process without changes will not be affected and restarted.