如何在 Nginx 中配置多版本 PHP?安装所需的 PHP 模块,例如 php7.2-fpm、php7.3-fpm 和 php7.4-fpm。在 Nginx 配置文件中添加 location 块,并在其中指定每个 PHP 版本的 fastcgi_pass。为每个 PHP 版本创建指定虚拟主机,例如,使用 fastcgi_pass unix:/run/php/php7.3-fpm.sock 指定 PHP 7.3 版本。重启 Nginx 以使配置生效。使用 php -v 命令验证当前正在使用的 P
如何在 Nginx 中配置多版本 PHP
Nginx 作为一款流行的 Web 服务器,允许您配置多个 PHP 版本,以满足不同的应用程序需求。下面是配置多版本 PHP 的步骤:
1. 安装所需模块
首先,需要安装以下 Nginx 模块:
立即学习“PHP免费学习笔记(深入)”;
php7.2-fpm php7.3-fpm php7.4-fpm
2. 配置 Nginx
在 Nginx 配置文件中(通常位于 /etc/nginx/nginx.conf),找到 http 块,并在其中添加以下内容:
location ~ \.php$ { fastcgi_pass unix:/run/php/php7.2-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ \.php$ { fastcgi_pass unix:/run/php/php7.3-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ \.php$ { fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
3. 添加指定 PHP 版本的虚拟主机
在 Nginx 虚拟主机配置文件中(通常位于 /etc/nginx/sites-available/),添加以下内容:
server { listen 80; server_name domain.com; root /var/www/domain.com; location ~ \.php$ { fastcgi_pass unix:/run/php/php7.3-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
在此示例中,虚拟主机将使用 PHP 7.3 版本。要使用其他版本,只需将 php7.3-fpm.sock 替换为您所需的版本。
4. 重启 Nginx
配置完成后,重启 Nginx 以使其生效:
systemctl restart nginx
5. 验证配置
要验证配置是否正确,请使用以下命令查看当前正在使用的 PHP 版本:
php -v
以上就是nginx 如何配置多版本php的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号