Python flask+gunicorn+nginx deploy Linux server

This article records the steps of deploying the flask project and some problems encountered.

1. Python installation

If the python version of the server itself is high enough, you can skip this step.

View Python version number

When Python is installed on Linux (default installation), you only need to enter a simple command to view the Python version number:

# python -V
Python 2.7.5

It can be seen that the Python version that comes with the system is 2.7.5.

Download the new version

To find the download address of the latest version, go directly to the official Python website to find the download address of the latest version. Copy the link address in the picture below.

Download compressed package

wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz

If wget is not installed, run yum -y install wget directly. -y means agreeing all the way.

Unzip

After the download is complete, unzip it:

# tar -zxvf Python-3.9.6.tgz
  • 1

Installation configuration

Enter the decompressed directory and install the configuration:

# cd Python-3.9.6/
# ./configure 

When executing ./configure, if an error is reported:

configure: error: no acceptable C compiler found in $PATH

Explanation: The appropriate compiler is not installed. At this time, you need to install/upgrade gcc and other dependent packages.

# yum install make gcc gcc-c++
  • 1

After completion, re-execute:

# ./configure 

Compile & amp; Install

After the configuration is complete, you can compile:

# make
  • 1

Long wait… Once completed, install:

# make install 

Verification

After the installation is successful, you can check the Python version:

# python -V
Python 2.7.5
# python3 -V
Python 3.9.6

At this point, the installation of python is complete. There is still an operation to change python3 to the default version. I have not done it. If you want to do it, you need to search it yourself.

Create a virtual environment

Upload our project to the server

Go to the folder you uploaded to

cd folder path

Create a python virtual environment under this path

python3 -m venv mvenv

After creation, enter the environment

source mvenv\bin\activate

Then pip adds the packages required by the project. You can export the dependent package files locally in advance and then import them in the virtual environment.

pip freeze >./requirements-all.txt #Create a dependency package file information in the root directory
pip install -r requirements-all.txt #Import dependency package file information

If the download is very slow and results in an error, you can change it to a domestic source and add

 -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host mirrors.aliyun.com

Now the entire virtual environment is configured, but if we start the service now, it still cannot be accessed by the public network. We need to install gunicorn first.

pip install gunicorn 

After installing gunicorn we can start the service

gunicorn -w 2 -b 0.0.0.0:5656 pushup:app

# -w is to start several processes
# -b is the server port number to be bound

Now it is actually accessible. Let’s install nginx for proxy jump. I installed nginx because other computers could not access the write interface during testing.

Close guicorn

1. Find the process tree

pstree -ap|grep gunicorn

Obviously, 18745 is the main process of Gunicorn.

Exit task

kill -9 process ID

Nginx

My system is CentOS7. If it is different, you can search how to install your own. There are many online

Add Nginx to YUM source

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

Install Nginx

sudo yum install -y nginx

Start Nginx

sudo systemctl start nginx.service

If everything is normal, you should be able to access the default Welcome page when you open the browser. If it cannot be opened, it is probably because the network firewall has not opened port 80. The following is the solution (if you don’t encounter the problem of not being able to access the default page, you can check the settings and auto-start section below)

Check whether Nginx has run successfully

[root@localhost home]# ps aux|grep nginx
root 36612 0.0 0.0 46464 972 ? Ss 19:49 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 36613 0.0 0.1 46856 1604 ? S 19:49 0:00 nginx: worker process
root 39654 0.0 0.0 112824 980 pts/0 S + 19:53 0:00 grep --color=auto nginx

Check whether port 80 is assigned to Nginx

[root@localhost \]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 36612/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1091/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1272/master
tcp6 0 0 :::22 :::* LISTEN 1091/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1272/master

Solution

The first step is to configure the firewall on port 80:

[root@localhost home]# firewall-cmd --zone=public --add-port=80/tcp --permanent

The second step is to restart the firewall service:

systemctl restart firewalld.service

Set Nginx to start automatically at boot

sudo systemctl enable nginx.service

If port 80 is still not accessible after doing this, it is recommended to change the listening port of nginx.

1. Modify the port listening address of nginx

1. Run the command find / -name nginx.conf to find the nginx.conf file. The path is generally /etc/nginx/nginx.conf.

2. Modify the nginx.conf file:

1) Run the command vim /etc/nginx/nginx.conf.

2) Press i key to enter editing mode.

3) Modify the following information in http:

4957bb517f8cbec9d72aa261afb2037d.png

Change to

42943d2be4dd9309f0708cfa6b12147f.png

Add redirect address

Add the following configuration to the server

 location / {
        proxy_pass http://127.0.0.1:8080; # This is the service address pointing to gunicorn host
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

4) Press the Esc key to exit editing mode, and enter :wq to save and exit.

3. Restart the service.

systemctl reload nginx

Some other methods:

  • Start service
systemctl start nginx
  • Out of service:
 systemctl restart nginx
  • Reload nginx
systemctl reload nginx