Gunicorn+Nginx deploys Flask project on Linux

1. Create the Flask project folder

mkdir Flaskproject
cd Flaskproject

2. Configure Flask source code and virtual environment

Example app.py:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello World!"

if __name__ == '__main__':
    app.run(debug=True)
pip install virtualenv  # 如果已安装则跳过
python -m venv flaskenv  # 新建flask虚拟环境
source flaskenv/bin/activate  # 激活flask虚拟环境(退出虚拟环境:deactivate)
pip install -r requirements.txt  # 安装依赖库
# pip install -r requirements.txt --no-dependencies 包版本冲突时可忽略版本要求,解决冲突
# 生成依赖库文件用 pip freeze > requirements.txt

3. Install gunicorn

pip install gunicorn

4. Start the gunicorn service

nohup gunicorn -w 4 -b 0.0.0.0:5000 app:app &     #不带日志
nohup gunicorn -w 4 -b 0.0.0.0:5000 app:app > gunicorn.log 2>&1 &     #带日志

Among them, -w sets the maximum number of processes, -b binds IP and port, the first app is the file name of app.py, and the second app is the instance name of the Flask application, which can be modified according to your own project.
If you need to customize the gunicorn configuration file, you can create a conf file in the corresponding path, and add -c gunicorn.conf to the startup command after configuration. (For the configuration tutorial of gunicorn, please refer to the detailed explanation of Gunicorn configuration . )
Now log in to http://xxxx:5000 to access your Flask project.

If you want to use port 80 for access, you can point port 80 to the port you specify through Ngix.

5. Install Nginx

sudo apt-get install nginx

6. Modify the Nginx configuration file (/etc/nginx/nginx.conf)

 Find the following two lines and comment out the second line. (saving files may require sudo or root privileges)

    include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*; 

 Add server{} configuration in http configuration, pay attention to add it in http{}.

http {
    ##############################
    server {
        listen 80;
        server_name localhost;  # 外界实际访问的地址

        location / {
            proxy_pass http://localhost:5000;  # 转发的本机端口地址
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
    ##############################
}

 If you have an ssl certificate and want http to automatically redirect to https, use the following configuration:

http {
    ##############################
    server {
        listen 80;
        server_name localhost;  # 外界实际访问的地址
        return 301 https://$host$request_uri;  # http重定向https
    }

    server {
        listen 443 ssl;
        server_name localhost;
        ssl_certificate xxx.com.pem;   # 改为自己的ssl证书的目录
        ssl_certificate_key xxx.com.key;  # 改为自己的ssl证书秘钥的目录
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
        ssl_prefer_server_ciphers on;

        location / {
            proxy_pass http://localhost:5000;  # 转发的本机端口地址
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
    ##############################
}

7. Test whether the Nginx configuration is correct

$ sudo nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

 8. Restart the Nginx service

sudo service nginx restart
# 或者 sudo nginx -s reload

At this point, you can directly access the page by logging in to http://xxxx, without specifying the port number.

If you are interested, you can also take a look at my Flask project, I wrote several interesting & useful gadgets.

Doodoo Male http://www.doodoo.site/

Guess you like

Origin blog.csdn.net/qq_40039731/article/details/123059571