How to deploy multiple flask projects on a linux server based on python+gunicorn+nginx
environment and tools
This article is based on the nginx tool that has been installed, so you need to install the nginx tool in advance ( note that it is installed under the root user of the server ). At the same time, the python package that the flask project depends on has been installed by default.
The tool versions involved in this article are as follows:
python 3.7.2
gunicorn 20.1.0
nginx 1.18.0
Deployment process
1. Establish a virtual environment for python
1) View the python installation path
2) Switch the path to the python installation path
3) Install the python virtual environment package
4) Create a virtual environment
5) Switch to the path of the virtual environment and activate the environment
2. Install gunicorn
Directly switch to the project path in the virtual environment to install gunicorn
pip3 install gunicorn
Here you can use gunicorn to open the flask project, the code is as follows:
gunicorn -w 2 -b :5000 run:app
Reference link for explanation of various parameters: https://xugaoxiang.com/2020/07/21/flask-12-deployment/
But there may be problems, and you can use the following methods to solve them, but I still don’t know the specific reasons. If you know, I hope you can give me some advice! !
gunicorn -w 2 -b :5000 run:app --preload
3. Configure nginx file
I won’t go into detail about the importance of nginx. I searched a lot online and found out how to operate it. Hahaha.
How to query the valid ngin configuration file, you can refer to my blog: https://blog.csdn.net/LJ1120142576/article /details/121205308?spm=1001.2014.3001.5501
Find the address of the nginx configuration file and modify it. The relevant code is as follows:
1) Modify the nginx.conf file
cd /etc/nginx
vim nginx.conf
2) Modify the default file
cd /etc/nginx
ls
cd sites-available
vim default
##The mosaic is the address of the server
3) Verify the modification
/usr/sbin/nginx -t
If the following words appear, the modification is successful
4. Configure gunicorn.conf file
Switch back to the project path and configure gunicorn.conf. If there are multiple flask projects, create a .conf file respectively, such as: face.conf and tongue.conf
vim gunicorn_face.conf
The specific contents of the configuration file are as follows:
5. Start gunicorn service
After the above configuration is completed, you can run the flask project in each project path. The code is as follows:
nohup gunicorn -c gunicorn_tongue.conf label:app --preload &
Then enter: server ip: port number in the web page , and the deployment of the flask project is completed.