目标:我需要使用 ASDF 插件加载运行 NGINX 的 PHP 网站 (7.4)。(已安装 Mac OS、M2 处理器、NGINX 和 ASDF)
问题:当我通过 nginx 加载网站时,我得到了这个:
致命错误:Composer 在您的平台中检测到问题:您的 Composer 依赖项需要 PHP 版本“> = 7.4.0”。您正在运行 5.6.40。位于 /usr/local/var/www/my_sulu_project/vendor/composer/platform_check.php 第 25 行
尝试调试错误
以下是我运行的一些命令:
$ php -v
PHP 7.4.33 (cli) (built: Apr 5 2023 18:33:55) ( NTS )
Copyright (c) The PHP Group
$ asdf list php
*7.4.33
8.2.6
$ which php
/Users/**redacted**/.asdf/shims/php
$ which php-fpm
/Users/**redacted**/.asdf/shims/php-fpm
$ composer -V
Composer version 2.2.21 2023-02-15 13:07:40
### !!! note that is showing `php-fpm` version 5.6 below
$ ps aux | grep php
**redacted** 2043 0.0 0.0 34851832 2016 ?? S 11:14am 0:00.01 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize
**redacted** 2042 0.0 0.0 34982908 2116 ?? S 11:14am 0:00.03 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize
**redacted** 1938 0.0 0.0 34851948 1332 ?? S 11:14am 0:00.66 /usr/local/opt/[email protected]/sbin/php-fpm --nodaemonize
$ nginx -t && nginx -s reload
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
更多信息:我通过 ASDF 安装了它,因为我需要使用多个版本的 PHP,具体取决于我正在从事的项目,包括正在迁移到 8.x。
我正在使用框架 Sulu CMS 1.6 (Symfony),并且根据这些文档是你如何配置的nginx
,但因为我使用asdf
插件,所以我必须编辑该fastcgi_pass
行,我将其更改为fastcgi_pass 127.0.0.1:9000;
运行 php 命令会给我正确的版本,即使运行 composer
我通过 ASDF 在本地和全局安装了 PHP 7.4。
我在想也许有一种方法可以指向?/usr/local/opt/[email protected]/sbin/php-fpm
/Users/**redacted**/.asdf/shims/php-fpm
或者也许 NGINX 需要以另一种方式配置?
我也尝试更改asdf php-fpm shim 位置,但没有成功。fastcgi_pass unix:/usr/local/opt/[email protected]/sbin/php-fpm;
重现此情况的安装示例
brew install libzip
asdf install php latest:7
asdf local php latest:7
git clone https://github.com/sulu/sulu-minimal.git
# composer 2.2 is required in my project
composer self-update --2.2
composer install
nginx 站点配置:
{
server {
listen 80;
server_name local.my_project;
root /usr/local/var/www/my_project/web;
error_log /usr/local/var/www/my_project/var/logs/nginx_error.log;
access_log /usr/local/var/www/my_project/var/logs/nginx_access.log;
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
# recommended security headers
add_header X-Frame-Options sameorigin;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
location /admin {
index admin.php;
try_files $uri @rewriteadmin;
}
location @rewriteadmin {
rewrite ^(.*)$ /admin.php/$1 last;
}
location / {
index website.php;
try_files $uri @rewritewebsite;
}
# expire
location ~* \.(?:ico|css|js|gif|webp|jpe?g|png|svg|woff|woff2|eot|ttf|mp4)$ {
try_files $uri /website.php/$1?$query_string;
access_log off;
expires 1y;
add_header Pragma public;
add_header Cache-Control "public";
}
location @rewritewebsite {
rewrite ^(.*)$ /website.php/$1 last;
}
# pass the PHP scripts to FastCGI server from upstream phpfcgi
location ~ ^/(website|admin|app|config)\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
# fastcgi_pass unix:/usr/local/opt/[email protected]/sbin/php-fpm;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SYMFONY_ENV dev;
internal;
}
}