Server Environment Setup
Host: 4-core 8GB CentOS 7.9 virtual machine (verify with cat /etc/redhat-release)
Dependencies: Anaconda, Python 3.8, uWSGI, Nginx
Python 3.8 Compilation and Installation
Update Yum Repositories
First back up default CentOS base repo:
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
Replace with Alibaba Cloud mirror repo:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
Generate package metadata cache:
yum makecache fast
Clean old cache optionally:
yum clean packages headers metadata dbcache
Install Build Dependencies
yum groupinstall -y "Development Tools"
yum install -y zlib-devel bzip2-devel openssl11-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
Download and Extract Python 3.8 Source
cd /tmp
wget https://www.python.org/ftp/python/3.8.18/Python-3.8.18.tgz
tar -xzf Python-3.8.18.tgz
Compile and Install Python 3.8
mkdir -p /opt/python38
cd /tmp/Python-3.8.18
./configure --prefix=/opt/python38 --enable-optimizations
make -j4
make install
Configure System Symlinks
List existing Python-related symlinks:
ls -la /usr/bin/ | grep python
Replace default Python symlink:
rm -f /usr/bin/python
ln -s /opt/python38/bin/python3 /usr/bin/python
Verify Python version:
python --version
Update Pip symlink:
rm -f /usr/bin/pip
ln -s /opt/python38/bin/pip3 /usr/bin/pip
Verify Pip version:
pip --version
Adjust Yum Scripts
Yum relies on Python 2.7, modify shebangs:
vi /usr/bin/yum
# Change first line from #!/usr/bin/python to #!/usr/bin/python2
vi /usr/libexec/urlgrabber-ext-down
# Change first line from #!/usr/bin/python to #!/usr/bin/python2
vi /usr/bin/yum-config-manager
# Change first line from #!/usr/bin/python to #!/usr/bin/python2
Optional Python 3.8 Uninstallation
rpm -qa | grep python3 | xargs rpm -ev --allmatches --nodeps
whereis python3 | xargs rm -frv
whereis python
uWSGI Configuration and Management
Create a config file uwsgi_config.ini in your Flask project root:
[uwsgi]
socket = 127.0.0.1:9090
chdir = /var/www/my_flask_app
wsgi-file = app.py
callable = application
workers = 4
threads = 4
stats = 127.0.0.1:9292
pidfile = /var/www/my_flask_app/uwsgi.pid
daemonize = /var/www/my_flask_app/logs/uwsgi_app.log
lazy-apps = true
vacuum = true
touch-chain-reload = /var/www/my_flask_app/.reload_trigger
uWSGI Control Commands
Start in foreground:
cd /var/www/my_flask_app
uwsgi --ini uwsgi_config.ini
Start as daemon:
uwsgi --ini uwsgi_config.ini --daemonize /var/www/my_flask_app/logs/uwsgi_app.log
Restart service:
uwsgi --reload uwsgi.pid
Gracefully stop service:
uwsgi --stop uwsgi.pid
Hard kill unresponsive processes:
kill -9 $(cat uwsgi.pid)
Nginx Installation and Reverse Proxy Setup
Check and Uninstall Existing Nginx
find / -name nginx -type f 2>/dev/null
yum erase -y nginx nginx-all-modules
Install Nginx Build Dependencies
yum install -y gcc-c++ pcre2-devel zlib-devel openssl-devel
Compile and Install Nginx
Navigate to source directory:
cd /usr/local/src
Download Nginx 1.24.0 source:
wget -c https://nginx.org/download/nginx-1.24.0.tar.gz
Extract and enter directory:
tar -xzf nginx-1.24.0.tar.gz
cd nginx-1.24.0
Configure, compile, and install:
mkdir -p /usr/local/nginx-1.24.0
./configure --prefix=/usr/local/nginx-1.24.0 --with-http_stub_status_module --with-http_ssl_module --with-pcre
make -j4
make install
Create a symlink for easier access:
ln -s /usr/local/nginx-1.24.0/sbin/nginx /usr/sbin/nginx
Configure Nginx Reverse Proxy
Edit main Nginx config:
vi /usr/local/nginx-1.24.0/conf/nginx.conf
Replace default server block with:
server {
listen 80;
server_name YOUR_SERVER_PUBLIC_IP_OR_DOMAIN;
access_log /usr/local/nginx-1.24.0/logs/my_flask_access.log;
error_log /usr/local/nginx-1.24.0/logs/my_flask_error.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
}
Nginx Control Commands
Start Nginx:
nginx
Test config file syntax:
nginx -t
Reload config without downtime:
nginx -s reload
Gracefullly stop Nginx:
nginx -s quit
Force stop Nginx:
nginx -s stop
Check running Nginx processes:
ps aux | grep nginx