PHP开发日志 ━━ IIS7安装PHP8.0及多个版本如何同时存在一台服务器

一、准备工作

1、安装运行库

php8和php5、php6、php7都需要安装对应版本的 Microsoft Visual C++运行库下载页面左侧说明里有运行库下载路径:

VC15 & VS16
More recent versions of PHP are built with VC15 or VS16 (Visual Studio 2017 or 2019 compiler respectively) and include improvements in performance and stability.
-The VC15 and VS16 builds require to have the Visual C++ Redistributable for Visual Studio 2015-2019 x64 or x86 installed

  • php8下载标题(本文发表时) VS16 x64 Non Thread Safe (2020-Nov-24 22:43:38),注意vs16字样
  • php7下载标题(本文发表时) VC15 x64 Non Thread Safe (2020-Nov-24 15:08:39),注意vc15字样
    在这里插入图片描述
    安装运行库并且重启服务器
    在这里插入图片描述

往期版本如果标题类似 php-5.3.4-Win32-VC6-x86.zip,那就意味着需要安装vc6运行库,去微软官网找吧。

2、IIS组件及安装

  • windows11等办公操作系统除了安装常用的IIS组件外,必须安装CGI
    在这里插入图片描述
  • windows2016等服务器系统之类同样,选择添加角色,CGI是最重要的核心

在这里插入图片描述

  • 功能用默认即可(我当时觉得没用把所有功能都给删了,导致iis、powershell、服务器管理器等都不能使用)
    在这里插入图片描述

如果用最简约的安装方法,那么成功后应当仅存在如下图功能:

在这里插入图片描述

2022.08.24配置了整整一天windows2016和php8.1.9,到处都是不兼容,甚至明明打开了gd组件,但程序提示对应函数无法使用。
强烈不建议安装8.1.x,甚至8.1.x这一版本让我都感觉到了php的没落!
参考吐槽文章《windows2016安装php8.1及mysql8出现的问题汇总(简直就是*#*o#)

3、解压缩php

将下载好的php8压缩包解压后放置于相关目录。个人经验建议创建类似这样的目录c:/php/php8.0,下次升级php的时候可以再建目录c:/php/php8.3,这样切换php相对而言比较清晰。

在这里插入图片描述

二、配置PHP

  • 定义时区
date.timezone = Asia/Shanghai
  • 定义扩展所在目录
extension_dir = "c:\php\php8.0\ext"
  • 激活扩展,根据业务需求去掉;注释标记即可
extension=curl
extension=gd
extension=mbstring
extension=mysqli
extension=openssl
extension=pdo_mysql
  • session存储目录
    可以不用设置。在老版本的php中,可能因为原生目录权限问题而出错。
session.save_path = "d:\temp\phpSession"
  • 系统临时目录
    有时候可能目录权限没开,php在执行时可能会出现500错误
sys_temp_dir = "d:\temp\phpSys"

PHP Warning: Unknown: Unable to create temporary file, Check permissions in temporary files directory. in Unknown on line 0

  • 上传临时目录
    上传出错时可考虑是否本目录权限不够。
upload_tmp_dir = "d:\temp\phpUpload"
  • 安全设置
    cgi.fix_pathinfo = 0fastcgi.impersonate = 1cgi.force_redirect = 0expose_php = Off
    可以看一下原文,更好理解这四个设置的作用。
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo = 0

; FastCGI under IIS supports the ability to impersonate
; security tokens of the calling client.  This allows IIS to define the
; security context that the request runs under.  mod_fastcgi under Apache
; does not currently support this feature (03/17/2002)
; Set to 1 if running under IIS.  Default is zero.
; http://php.net/fastcgi.impersonate
fastcgi.impersonate = 1

; cgi.force_redirect is necessary to provide security running PHP as a CGI under
; most web servers.  Left undefined, PHP turns this on by default.  You can
; turn it off here AT YOUR OWN RISK
; **You CAN safely turn this off for IIS, in fact, you MUST.**
; http://php.net/cgi.force-redirect
cgi.force_redirect = 0

; Decides whether PHP may expose the fact that it is installed on the server
; (e.g. by adding its signature to the Web server header).  It is no security
; threat in any way, but it makes it possible to determine whether you use PHP
; on your server or not.
; http://php.net/expose-php
expose_php = Off

以往都是设置cgi.fix_pathinfo = 1,但现在发现有重大漏洞,原因举例:

当访问www.xx.com/phpinfo.jpg/1.php这个URL时,$fastcgi_script_name会被设置“phpinfo.jpg/1.php”,然后构造成SCRIPT_FILENAME(绝对路径)传递给PHP CGI,如果开启了cgi.fix_pathinfo=1选项(这个默认值就是1,所以没有设置过就是开启),那么就会触发在PHP中的如下逻辑:
PHP会认为SCRIPT_FILENAME(绝对路径)是phpinfo.jpg,而1.php是PATH_INFO,所以就会phpinfo.jpg作为PHP文件来解析了.
也是一个逻辑问题,所以说我们只需要在正常的.jpg后面加/.php就可以成功的绕过解析

如果某些网站打开后提示No input file specified.,那只能使用cgi.fix_pathinfo = 1,值不能为0

Nginx + PHP CGI的一个可能的安全漏洞
php fpm 设置项 cgi.fix_pathinfo=1 漏洞不再出现

  • 目录权限
    如果提示无法运行程序,可以尝试修改一下目录权限

  • 其它设置
    以上配置仅为最基本的安全运行配置,如果在实际开发时,可能需要改变其它配置,例如改变上传文件的大小、响应等待时间等等。

在这里插入图片描述

三、配置IIS

1、添加映射

打开iis界面,选择 处理程序映射 - 添加模块映射,按照下图操作,注意可执行文件是php-cgi.exe
在这里插入图片描述
提示是否创建FastCGI应用程序,选择即可。
在这里插入图片描述

2、设置FastCGI

如果上一步选择了,那么这里就已经设置好了。
在这里插入图片描述
双击后根据实际情况编辑。
在这里插入图片描述

3、设置默认文档

添加默认的php文档,一般是index.phpdefault.php
如果你使用单入口的php框架,可以自定义一些奇怪的文件名称~~比如Xsdf2dm.php
在这里插入图片描述

四、运行测试

在根目录下新建一个index.php文件查看配置是否正确,代码如下:

<?php phpinfo();?>

实际运行时记得删除本文件,否则太暴露了~~

五、多个版本同时

  1. 先在父级设置好所有的php版本
    在这里插入图片描述

  2. 然后在不同的域名目录下设置web.config,内容参考如下:
    先移除所有版本,再设置你所需要的版本对应的内容;

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <remove name="php7.0" />
            <remove name="php8.0" />
            <add name="php7.0" path="*.php" verb="*" modules="FastCgiModule" scriptProcessor="c:\php\php7.0\php-cgi.exe" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
    </system.webServer>
</configuration>

经测试不写<remove name="php7.0" /><remove name="php8.0" />,只写add也是能成功运行的,但目前还没研究过是否会有问题,如果谁有错误出现可以留言。
在这里插入图片描述

参考:

VC++6.0、VC2008、VC2010之间的区别
PHP8.0尝鲜系列(一):Windows 10安装PHP8.0
PHP全线产品升级至5.3、5.4流程(PHP5.3在IIS上的配置)
IIS7中多个版本php共存的方法

猜你喜欢

转载自blog.csdn.net/snans/article/details/111823604