关于php:PHPPHP开发必备配置-Windows

2次阅读

共计 4858 个字符,预计需要花费 13 分钟才能阅读完成。

下载 PHP

集体下载版本为 PHP 7.4 (7.4.33) 的版本。

把下载后的文件解包放到适合的地位,比方D:\Program Files\php-7.4.33-Win32-vc15-x86

配置环境变量

进入到零碎变量

而后把 php 根目录的门路退出进入,以及 ext 的内容一并退出到 Path 环境变量。

cmd 执行如下命令查看是否搭建环境胜利:

C:\Users\adong>php -v
PHP 7.4.33 (cli) (built: Nov  2 2022 17:33:44) (ZTS Visual C++ 2017 x86)
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Composer 装置

Composer是 PHP 开发必不可少的工具之一,能够无效的缩小依赖包下载和治理的懊恼。

下载 Composer

装置前请务必确保曾经正确装置了 PHP。关上命令行窗口并执行 php -v 查看是否正确输入版本号。

Composer 的官网网址如下:https://getcomposer.org/download/

Windows 平台上,咱们只须要下载 Composer-Setup.exe 后,一步步装置即可。

须要留神的是你须要开启 openssl 配置,咱们关上 php 目录下的 php.ini,将  extension=php_openssl.dll  后面的分号去掉就能够了。

装置胜利后,咱们能够通过命令窗口(cmd) 输出 composer --version 命令来查看是否装置胜利:

C:\Users\adong>composer --version
Composer version 2.4.4 2022-10-27 14:39:29

也能够依照中文网的官网文档,间接依照步骤下载即可:

php -r "copy('https://install.phpcomposer.com/installer','composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"

更改阿里云 Composer 镜像

因为国外网站拜访比较慢,这里介绍装置 Composer 镜像。

阿里云 Composer 镜像【举荐】

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

国内云 Composer 镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com

腾讯云 Composer 镜像

composer config -g repo.packagist composer https://mirrors.cloud.tencent.com/composer/

华为云 Composer 镜像

composer config -g repo.packagist composer https://repo.huaweicloud.com/repository/php/

安畅云 Composer 镜像

composer config -g repo.packagist composer https://php.cnpkg.org/

开发罕用配置

这里参考了 phpStudy,凋谢一些常见的 php.ini 配置组件,首先把上面这些扩大 extendsion 正文关上:

extension=bz2
extension=curl
extension=fileinfo
extension=gd2
extension=gettext
extension=mbstring
extension=mysqli
extension=openssl
extension=pdo_mysql

而后批改以后的扩大引入目录,这里批改为./ext

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
;extension_dir = "./"
; On windows:
extension_dir = "./ext"

这样解决之后,罕用的组件就根本齐全的。

伪动态

在各种 PHP 开发应用的全包软件中如果想要去除掉,能够在 nginx 外面配置伪动态,伪动态配置如下:

location / {if(!-e $request_filename){rewrite ^/index.php(.*)$ /index.php?s=/$1 last;
        rewrite  ^(.*)$  /index.php?s=/$1  last;
        break;
    }
}

如果下面的内容设置报错,能够设置为上面的内容:

location / {if (!-e $request_filename){rewrite  ^(.*)$  /index.php?s=$1  last;   break;
    }
}

VsCode 配置

VsCode 也算是用的比拟多的开发 PHP 的工具,进行配置之后要比 Jerbrian 家的 IDE 要不便很多。

VsCode 的下载网址如下:

Visual Studio Code – Code Editing. Redefined

下载之后,咱们间接下载相干的组件:

  • Composer
  • PHP 语言反对
  • PHP Debug(关注,后续须要设置)
  • PHP Extendsion Pack
  • Php IntelliSense

接下来是苦楚折磨的 Xdebug 配置。

Xdebug 配置

在 VS Code 中须要配置应用的 PHP 版本,对应的 php.ini 须要开启 debug 配置才能够失常应用。因为集体应用的是 PhpStudy,所以临时先引入到 PhpStudy 当中。

{
    "php.validate.executablePath": "D:/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe",
    "php.debug.executablePath": "D:/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe"
}

接着下载 Xdebug,进入上面的官网:

Xdebug – Debugger and Profiler Tool for PHP

Windows 应用上面的形式,这里抉择第二个链接。

这里须要用户复制 Phpinfo 的信息,而后 Xdebug 页面会剖析 Phpinfo 给出适合的版本。具体的操作如下,间接新建一个 php 文件,而后增加上面的代码:

<?php
    phpinfo();

?>

比方集体拜访页面呈现上面的内容

把这个页面的内容ctrl + A 全选贴到页面即可:

之后点击剖析按钮即可,如果提醒找不到,则自行下载适合的版本放到 ext 目录。咱们把插件下载到对应的 Php 装置目录的 ext 目录上面,这里倡议重命名为php_xdebug.dll,为什么作为一个插件进行扩大这个集体也说不清楚,网络上有局部网友尝试说放到 ext 扩大能力失效也就照做了。

这里说一个小插曲,集体在本人的电脑尝试发现 xdebug 给集体的抉择是倡议降级到 7.4.40 版本并且提醒不兼容???有点摸不着头脑,最初下载了 xdebug-3.04 的版本。

留神这是个 dll 文件,应用局部浏览器下载可能会误认为是病毒,须要自行容许放行

之后咱们回到 VSCode 当中创立 debu 配置文件,创立配置如图:

集体创立之后 Json 内容如下(通常不须要自行配置):

{

    // Use IntelliSense to learn about possible attributes.

    // Hover to view descriptions of existing attributes.

    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387

    "version": "0.2.0",

    "configurations": [

        {

            "name": "Listen for Xdebug",

            "type": "php",

            "request": "launch",

            "port": [

                9003,

                9000

            ]

        },

        {

            "name": "Launch built-in server and debug",

            "type": "php",

            "request": "launch",

            "runtimeArgs": [

                "-S",

                "localhost:8000",

                "-t",

                "."

            ],

            "port": 9003,

            "serverReadyAction": {"action": "openExternally"}

        },

        {

            "name": "Debug current script in console",

            "type": "php",

            "request": "launch",

            "program": "${file}",

            "cwd": "${fileDirname}",

            "externalConsole": false,

            "port": 9003

        }

    ]

}

这里留神端口 9003,肯定要和 php.ini 的端口统一,否则是无奈 Debug 的。接着是最大的踩坑点,也就是 php.ini 中 debug 的配置,因为 Xdebug 降级过程的配置细节在一直变动,所以这里仅仅能保障 php .7.4.3 版本 + Xdebug 3.04 的版本能够用这套配置失常 Debug。

具体能够参考上面的配置,再次强调 9003 这个端口的重要性:

[Xdebug]
zend_extension=D:/phpstudy_pro/Extensions/php/php7.4.3nts/ext/php_xdebug.dll
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.auto_trace=On
xdebug.trace_output_dir=D:/phpstudy_pro/Extensions/php_log/php7.4.3nts.xdebug.trace
xdebug.profiler_enable=On
xdebug.profiler_output_dir="D:/phpstudy_pro/Extensions/php_log/php7.4.3nts.xdebug.profiler"
; xdebug.remote_enable=yes
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.remote_host=localhost
xdebug.remote_port=9003
xdebug.remote_handler=dbgp

最初咱们启动服务查看 phpinfo 的信息。

上面是在 vsCode 中编写代码,并且打上断点。

咱们进入到 VsCode 的 Run And Debug 查看相干 debug 选项,第一个选项是须要配合服务器申请相干的 PHP 之后进入到 DEBUG,第三个是在控制台进行 DEBUG 运行。

之后咱们开启 debug 模式,最终失常进入断点。

问题探讨

最有可能碰到问题就是依照下面的配置之后 debug 开启页面拜访没有进入 debug,集体的倡议是多尝试批改 Php.ini 配置,留神配置门路是否正确,Vscode 在 Debug 读取配置失败会在页面上面给出提醒,是个不错的排查伎俩。

总结

没什么好总结的,PHP 是我见过最恶心的配置之一。

正文完
 0