跳到正文
Ubuntu / Linux

After Upgrading Ubuntu 20.04 to 22.04: Fix Nginx and PHP-FPM (PHP 7.4 → 8.1)

Install php8.1-fpm and the extensions your sites need, change every fastcgi_pass from php7.4-fpm.sock to php8.1-fpm.sock, run nginx -t and reload.

测试环境 Ubuntu 22.04 LTS (upgraded from 20.04)

An in-place upgrade from Ubuntu 20.04 to 22.04 replaces PHP 7.4 with PHP 8.1. Your Nginx server blocks still pass PHP requests to /run/php/php7.4-fpm.sock, which no longer exists – so sites show 502 Bad Gateway, a blank page or “File not found”.

Step 1 – Install PHP 8.1-FPM and extensions

Terminal
sudo apt updatensudo apt install php8.1-fpm php8.1-cli php8.1-mysql php8.1-mbstring php8.1-xml php8.1-curl php8.1-gd php8.1-zip php8.1-intl php8.1-bcmath php8.1-imagick

Compare with what you used before (dpkg -l | grep php7.4) and install the 8.1 equivalents.

Step 2 – Point Nginx at the new socket

Find every reference to PHP 7.4:

Terminal
grep -rn u0022php7.4u0022 /etc/nginx/

Replace them in all site files at once (a backup of each file is kept with a .bak extension):

Terminal
sudo sed -i.bak 's/php7.4-fpm.sock/php8.1-fpm.sock/g' /etc/nginx/sites-available/*.conf

Step 3 – Carry over PHP settings

Custom values such as upload_max_filesize or memory_limit were in /etc/php/7.4/fpm/. Copy them to /etc/php/8.1/fpm/php.ini (and any custom pool files in pool.d/).

Step 4 – Test and reload

Terminal
sudo nginx -tnsudo systemctl restart php8.1-fpmnsudo systemctl reload nginx

Check WordPress compatibility

PHP 8.1 is stricter than 7.4. Update WordPress, themes and plugins before switching; check /var/log/nginx/error.log and the site’s debug log for deprecations. When everything works, remove PHP 7.4: sudo apt purge "php7.4*".

Setting up a new site instead? The Nginx Server Block Generator writes a complete config for any PHP version.

Inder Kochar

作者

Inder Kochar

系统与网络工程师 – 我把为客户使用的解决方案整理成文,并在真实系统上测试过。遇到类似问题了吗?

继续阅读

提出问题或分享您的解决方法

您的邮箱不会公开。评论需经审核——请围绕主题发言。