有时候我们的项目不可能都是同一个 php 版本,需要每个项目都配置不同版本的 php,宝塔和 phpstudy 就是通过以下配置实现的:
nginx
切割 conf(非选)
在 nginx.conf 添加
include vhosts/*.conf;
这样 nginx 会自动引入当前目录->vhosts 目录下的所有 *.conf 文件,方便每个项目单独管理 nginx 配置文件
配置多版本 php
在 conf 文件中增加
server { listen 80; server_name localhost; root "d:/www"; location / { index index.php index.html; include d:/www/nginx.htaccess; autoindex on; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9010; fastcgi_index index.php; fastcgi_split_path_info ^((?u).+\.php)(/?.+)$; fastcgi_param script_filename $document_root$fastcgi_script_name; fastcgi_param path_info $fastcgi_path_info; fastcgi_param path_translated $document_root$fastcgi_path_info; include fastcgi_params; }}
fastcgi_pass 是 php 执行 ip + 端口
fastcgi_index 默认 php 文件
fastcgi_split_path_info 是正则
fastcgi_param 是 php 所在目录(nginx 会自动获取赋值给 $fastcgi_script_name)
假设我们有两个 php 版本,一个 php5,一个 php7,那么可以将他们分别运行在不同的端口上,然后通过设置 fastcgi_pass 参数来实现每个项目不同 php 版本
apache
切割 conf(非选)
在 httpd.conf 添加
include conf/vhosts/*.conf
这样 apache 会自动引入 apache安装目录->conf->vhosts 目录下的所有 *.conf 文件,方便每个项目单独管理 apache 配置文件
配置多版本 php
在 conf 文件里添加
fcgidinitialenv phprc "d:/extensions/php/php8.2.2-nts" addhandler fcgid-script .php fcgidwrapper "d:/extensions/php/php8.2.2-nts/php-cgi.exe" .php
指定对应目录即可。
推荐学习:《php视频教程》
以上就是聊聊nginx和apache配置多版本php的详细内容。
