Python project deployment process in CentOS-8.5 environment (Python, Nginx, MySQL, uwsgi)

Table of Contents

    • 1. Install Python3.6.7 in CentOS-8.5 environment
      • 1. Download the Python installation package
      • 2. Unzip
      • 3. Compile the installation package
      • 3. Recompile and install
      • 4. Add environment variables
      • 5. Check whether it is successful, execute the following code
      • 6. Replace the domestic pip source
      • 7. Uninstall Python (if necessary)
      • 8. Create a virtual environment
    • 2. Install Nginx
      • 1. PCRE needs to be installed before installing Nginx
      • 2. Install Nginx
      • 3. Open ports
      • 4. Test whether Nginx is normal
      • 5. Configure Nginx
    • 3. Install the MySQL database
      • 1. Download and install the official MySQL Yum Repository of MySQL
      • 2. Then install the repo
      • 3. Install the MySQL server
      • 4. Start the MySQL service
      • 5. Configure remote connection to MySQL database
      • 6. Open port
    • Fourth, install and configure uwsgi
      • 1.pip3 install uwsgi
      • 2. Configure uwsgi.ini

1. Install Python3.6.7 in CentOS-8.5 environment

1. Download the Python installation package

cd /opt/package/
yum install -y gcc*
yum install -y zlib
yum install -y zlib-devel

tips: Install the gcc* compiler and zlib zlib-devel in advance when compiling

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

2. Unzip

cd ../software
tar -zxvf /opt/package/Python-3.6.7.tgz

3. Compile the installation package

cd Python-3.6.7
./configure --prefix=/usr/local/python
make & amp; & amp; make install

There is a pit here, the ssl module may not be found, resulting in the inability to import ssl and pip to install the module

1. Download and install the latest openssl
cd /opt/package
wegt https://www.openssl.org/source/openssl-1.1.1n.tar.gz
cd ../software
tar -zxvf /opt/package/openssl-1.1.1n.tar.gz
cd openssl-1.1.1n
./config --prefix=/usr/local/openssl
make & amp; & amp; make install

2. Then modify the Setup file
cd /opt/software/Python-3.6.7/Modules
vim Setup

Make the following modification:
Untie the comment and change the “SSL=” to the location of the newly downloaded openssl installation
SSL modification example

3. Recompile and install

cd ..
./configure --prefix=/usr/local/python
make & amp; & amp; make install

4. Add environment variables

echo PATH='/usr/local/python/bin/:$PATH' >> /etc/profile
source /etc/profile

5. Check whether it is successful, execute the following code

python3.6

Python 3.6.0 (default, Jun 1 2017, 14:01:43)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>print('hello word')
hello word

6. Replace the domestic pip source

Temporary changes:
pip install pyinstaller -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
Permanent change:
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

After running the permanent change command you can go to edit the file

cd /root/.config/pip
vim pip.conf

Add the following

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com

Commonly used mirror sources in China

Tsinghua: [https://pypi.tuna.tsinghua.edu.cn/simple]
Alibaba Cloud: [http://mirrors.aliyun.com/pypi/simple/]
University of Science and Technology of China [https://pypi.mirrors.ustc.edu.cn/simple/]
Huazhong University of Science and Technology: [http://pypi.hustunique.com/]
Shandong University of Technology: [http://pypi.sdutlinux.org/]
Douban: [http://pypi.douban.com/simple/]

7. Uninstall Python (if necessary)

The first method:

Install multiple versions of Python, delete the specified version
whereis python # View all Python paths

# Only delete Python3.7 related files
rm -rf /usr/local/python3.7m
rm -rf /usr/local/python3.7
rm -rf /usr/lib/python3.7
rm -rf /usr/lib64/python3.7
rm -rf /usr/include/python3.7m

whereis python # View all Python paths again

# Modify the pip configuration file
vim /usr/bin/pip
# Change the first line #!/usr/bin/python3.x to #!/usr/bin/python3.x (that is, change to the python version execution file you use in the /usr/bin/ directory)

The second method (use with caution):

!!!rpm will uninstall all python and its dependencies: yum depends on python2.x to run, which will cause yum to be unavailable, etc.!!!
Completely delete Python (!!!Note: All versions of Python and dependent packages will be deleted!!!)
rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps #Forcibly delete installed programs and their dependencies
 
whereis python |xargs rm -frv # removes all leftover files, xargs allows you to execute some other commands on the output
 
whereis python # verify whether the deletion is successful, return no result

8. Create a virtual environment

pip3 install virtualenv # Install virtualenv module

virtualenv env-dir --python=python3.6.7 # Create env-dir virtual environment in the current directory, based on python3.6.7
After configuring the python environment variable, the virtualenv command can be used directly, otherwise you need to specify the directory

cd env-dir/bin
source activate # activate into the virtual environment
deactivate # Exit the virtual environment

Go to the host to export the module file
pip freeze > requirements.txt
Batch install modules from module files
pip3 install -r requirements.txt

There is a module that needs attention: mysqlclient==1.4.2.post1

This module needs to install mysql-devel first

yum install -y mysql-devel --nogpgcheck

2. Install Nginx

1. PCRE needs to be installed before installing Nginx

wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
cd ../software
tar -zxvf ../package/pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/usr/local/pcre-8.35
make & amp; & amp; make install

Configure environment variables

echo PATH='/usr/local/pcre-8.35/bin/:$PATH' >> /etc/profile # Configure environment variables
source /etc/profile
pcre-config --version # View version

2. Install Nginx

wget https://nginx.org/download/nginx-1.14.2.tar.gz
cd ../software
tar -zxvf ../package/nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx-1.14.2 --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/software/pcre-8.35/
make & amp; & amp; make install

Note: Here “–with-pcre=” specifies the PCRE source code directory, not the installation directory

Configure environment variables

echo PATH='/usr/local/nginx-1.14.2/sbin/:$PATH' >> /etc/profile # Configure environment variables
source /etc/profile
nginx -v # View version

3. Open ports

firewall-cmd --zone=public --add-port=80/tcp --permanent # Open port 80
firewall-cmd --reload # Refresh the firewall
firewall-cmd --zone=public --list-ports # View the list of open ports

4. Test whether Nginx is normal

nginx # Start Nginx service

nginx -s quit # Shut down Nginx service normally
nginx -s stop # Quickly shut down the Nginx service
service nginx restart # Restart Nginx service
nginx -s reload #Reload after configuration file modification

5. Configure Nginx

cd /usr/local/nginx-1.14.2/conf
vim nginx.conf

edit the following

#user nobody;
worker_processes 1;

# Error log file location
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {<!-- -->
    worker_connections 1024;
}


http {<!-- -->
    include mime.types;
    default_type application/octet-stream;
\t
    # log format
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # nginx access log location
    access_log logs/access.log main;

    sendfile on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout 65;

    # If the project is large, you can turn on compression
    gzip on;

    # upstream djangos{<!-- -->
    # server 192.168.88.8:8000;
    # server 192.168.88.8:8001;
    # server 192.168.88.8:8002;
    # server 192.168.88.8:8003;
    # }

    server {<!-- -->
        # Listening port and IP
        listen 80;
        server_name localhost;
        #charset koi8-r;

        # server access log location
        access_log logs/host.access.log main;

        # Specify the project path uwsgi
        location / {<!-- -->
            include uwsgi_params;
            uwsgi_connect_timeout 30;
            # proxy_pass http://192.168.88.8:8000;
            uwsgi_pass unix:/home/pyproject/script/uwsgi.sock;
            
            # load balancing
            # proxy_pass_header Server;
            # proxy_set_header Host $http_host;
            # proxy_redirect off;
            # proxy_set_header X-Real_IP $remote_addr;
            # proxy_set_header X-Scheme $scheme;
            # proxy_pass http://djangos;
        }

        # Specify static file path
        location /static/ {<!-- -->
            alias /home/pyproject/mymall/static_all/;
            index index.html index.htm;
        }

        # Specify the media file path
        location /media/ {<!-- -->
            alias /home/pyproject/mymall/media/;
            index index.html index.htm;
        }
\t\t
        error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {<!-- -->
            root html;
        }

3. Install MySQL database

1. Download and install MySQL official MySQL Yum Repository

wget https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm

2. Then install the repo

rpm -ivh mysql57-community-release-el7-10.noarch.rpm

3. Install MySQL server

yum -y install mysql-community-server

If you directly execute the yum installation command, an error will be reported: Error: Unable to find a match: mysql-community-server

At this time, execute the following command, and then execute the yum installation command here

yum module disable mysql

When installing MySQL on the Alibaba Cloud mirror, using yum may cause an error: Error: GPG check FAILED

Then use the following command instead

yum -y install mysql-community-server --nogpgcheck

4. Start MySQL service

systemctl start mysqld # Start MySQL service
systemctl status mysqld # View MySQL service status
systemctl enable mysqld # Set MySQL service to start automatically
systemctl disable mysqld # Disable MySQL service from starting

grep "password" /var/log/mysqld.log # Find the initial password after installation, all passwords after "root@localhost:"
mysql -uroot -p # log in to the database with the initial password
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password'; # modify password

When we study, we may set relatively simple passwords such as “root” or “123456” and may report the following errors
Password security level error report example

Simply put, your password is too simple and does not conform to the current security policy, then we can lower the policy

set global validate_password_policy=LOW; # Set as low security policy security policy
set global validate_password_length=4; # set password length

There are four security policies: OFF (closed), LOW (weak), MEDIUM (medium), STRONG (strong)

5. Configure remote connection to MySQL database

mysql -uroot -proot # login database
use mysql; # enter the mysql library
select user,host from user; # View users and access rights

Access authority query result

You can see that the root user only allows local connections, let’s modify the permissions

update user set host='%' where user = 'root';
flush privileges; # refresh privileges

6. Open ports

firewall-cmd --zone=public --add-port=3306/tcp --permanent # Open port 3306
firewall-cmd --reload # Refresh the firewall
firewall-cmd --zone=public --list-ports # View the list of open ports

4. Install and configure uwsgi

1.pip3 install uwsgi

cd /root/pyproject/mymall-env/bin
source activate # enter the virtual environment
pip3 install uwsgi==2.0.18

2. Configure uwsgi.ini

cd /root/pyproject
mkdir script
cd script
vim uwsgi.ini

write the following

# uwsgi starts using the configuration file
[uwsgi]
# project directory
chdir=/root/pyproject/mymall
# Specify the application of the project [note: this application is the application> function in the wsgi.py file]
module=netshop.wsgi:application
# Specify the file path of the sock
socket=/root/pyproject/script/uwsgi.sock
# number of processes
workers=5
pidfile=/root/pyproject/script/uwsgi.pid
# Specify the IP port
http=192.168.88.8:8000
# Specify static files
static-map=/static=/root/pyproject/mymall/static_all
# Start uwsgi user name and user group
uid=root
gid=root
# enable the main process
master=true
# Automatically remove unix socket and pid files when the service stops
vacuum=true
# Serialize accepted content, if possible
thunder-lock=true
# enable threads
enable-threads=true
# Set the self-interruption time
harakiri=30
# set buffer
post-buffering=4096
# Set the log directory
daemonize=/root/pyproject/script/uwsgi.log

Start uwsgi with configuration file

uwsgi --ini uwsgi.ini # Remember to be in the virtual environment and specify the uwsgi.ini file path
uwsgi --reload uwsgi.pid # Restart uwsgi
uwsgi --stop uwsgi.pid # close uwsgi