您的位置:首页 > 技术中心 > 运维 >

Nginx和Apache怎么配置多版本PHP

时间:2023-05-24 11:40

有时候我们的项目不可能都是同一个 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

指定对应目录即可。

以上就是Nginx和Apache怎么配置多版本PHP的详细内容,更多请关注Gxl网其它相关文章!

本类排行

今日推荐

热门手游