WP系列2: upgrade PHP from 7.3 to 7.4 (most stable version)

first check which php version is most suitable for worpress, via this link:

I will go for stable version 7.4, since 8.x is not fully compatible.

check the current version of your server by cmd: php –version

what I got here is:

root@vir1-idehe:~# php -v
PHP 7.4.33 (cli) (built: Sep 4 2023 08:15:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

upgrade by common tutorials, but faced an error:

    sudo apt install apt-transport-https lsb-release ca-certificates curl -y
    sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg 
    sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
    sudo apt update

    root@vir1-idehe:/var/www# lsb_release -sc
    Traceback (most recent call last):
    File "/usr/bin/lsb_release", line 25, in <module>
        import lsb_release
    ModuleNotFoundError: No module named 'lsb_release'

To solve this problem, try this:

# 将系统完整的lsb_release.py文件拷贝到报错的目录文件下即可
sudo cp /usr/lib/python3/dist-packages/lsb_release.py /usr/bin/

Continue with upgration

    sudo apt install php7.4
    sudo apt-get install php7.4-{bcmath,bz2,intl,gd,mbstring,mysql,zip}
    apt-get install php7.4-xml

Also disable apache2, uninstall php7.3 and install php7.4-pfm by following commands:

sudo systemctl disable --now apache2
apt-get install php7.4-fpm
sudo apt purge php7.3 libapache2-mod-php7.3

XML is necessary to install, otherwise you will have critial issues on site health.

change nginx php settings in /etc/wordpress/sites-available/worpress.conf (this is the file I use)

change the php7.3-pfm to 7.4 as below:

        location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_read_timeout 3600s;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 128k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
    }

restart nginx engine, now you should all done, check it out.