Ubuntu20.04+Nginx环境升级PHP8后Composer仍报版本不兼容问题
问题:Ubuntu 20.04 Nginx环境下Composer依赖PHP版本报错
给Composer添加新依赖时本地运行一切正常,推送到Ubuntu 20.04服务器后出现如下报错:
Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0".
已将服务器PHP版本升级到8.0.12,执行php -v返回结果如下:
# php -v PHP 8.0.12 (cli) (built: Oct 22 2021 12:34:48) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.12, Copyright (c) Zend Technologies with Zend OPcache v8.0.12, Copyright (c), by Zend Technologies
排查Nginx错误日志发现如下记录:
Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0". You are running 7.4.3. in /var/www/html/vendor/composer/platform_check.php on line 24" while reading response header from upstream, client: ........, server: _, request: "GET /login HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.4-fpm.sock:"
可见请求实际调用的是php7.4-fpm.sock,已尝试过如下操作仍未解决问题:
- 修改
/etc/nginx/sites-available/default配置文件中PHP相关配置:
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; # fastcgi_read_timeout 300; # include fastcgi_params; }
配置已指定php8.0-fpm.sock,但仍调用PHP7.4版本
- 执行
sudo apt-get autoremove删除php7.4-fpm相关文件后,出现502 bad gateway报错,日志显示:
connect() to unix:/var/run/php/php7.4-fpm.sock failed
- 执行
composer install --ignore-platform-reqs,返回501内部错误
完整sites-available配置如下:
server { listen 80; server_name _ DOMAIN www.DOMAIN; root /var/www/html/public; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header X-Content-Type-Options "nosniff"; index index.html index.htm index.php; charset utf-8; location / { try_files $uri $uri/ /index.php$is_args$args; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } error_page 404 /index.php; fastcgi_read_timeout 300; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; # fastcgi_read_timeout 300; # include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } }
解决方案
问题核心是Nginx未加载更新后的站点配置,按以下步骤操作即可解决:
- 确认php8.0-fpm服务正常运行:执行
systemctl status php8.0-fpm检查状态,若未启动执行systemctl start php8.0-fpm && systemctl enable php8.0-fpm开启服务并设置开机自启 - 更新Nginx站点软连接:执行
ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default,确保sites-enabled目录下的配置指向你修改后的最新文件 - 校验Nginx配置合法性:执行
nginx -t,返回test is successful说明配置语法无错误 - 重载Nginx配置:执行
systemctl reload nginx让新配置生效 - 验证结果:访问项目新增phpinfo页面即可查看Web端实际加载的PHP版本,确认是8.0版本后问题解决
内容的提问来源于stack exchange,提问作者Luuk Bakx
相关产品推荐
相关产品推荐

