XAMPP和VScode 实现对PHP的编写和调试

  在工作中遇到了需要开发PHP的情况,因为习惯用VS开发.net系列,所以决定用VScode继续开发PHP。对搭建VSCode的开发环境的过程做了一些总结。

Xmapp的安装

下载并且安装合适自己电脑的XMAPP的版本。下载地址:https://www.apachefriends.org/zh_cn/download.html
我用的是(7.0.27 / PHP 7.0.27)Window32位版本。

xDebug的安装

首先运行XMAPP以后,发现已经监听了80端口
这里写图片描述
打开浏览器访问 Http://localhost/,然后查看PHPInfo,找到正确的PHP版本,特别是是否线程安全。因为这个版本决定了Xdebug的版本,如果版本寻找不对,XDEBUG是无法安装成功的。
这里写图片描述
这里写图片描述
下载PHP对应的XDebug版本。下载地址:https://xdebug.org/download.php ,解压以后修改dll名称为php_xdebug.dll, 复制到php的ext文件夹下面。
修改php.ini,在下面增加

[XDebug]
zend_extension = "php_xdebug.dll"
xdebug.profiler_append = 0
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 0
xdebug.profiler_output_dir = "C:/xampp/tmp"
xdebug.profiler_output_name = "cachegrind.out.%t-%s"
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9010
xdebug.remote_connect_back=0
xdebug.remote_enable = 1
xdebug.remote_autostart=1
xdebug.idekey=netbeans 

特别需要注意的是remoting_port的设置,跟后面vscode需要一致。我再配置xdebug的时候,因为版本问题,走了很多弯路。

配置Apache的虚拟目录

首先在Apache的httpd.conf中把 #Include conf/extra/httpd-vhosts.conf修改为Include conf/extra/httpd-vhosts.conf。
然后找到httpd-vhosts.conf,在后面增加下面代码

<VirtualHost *:9000>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "D:/Learn/Spider/"
    ServerName localhost
    ErrorLog "logs/spider"
    CustomLog "logs/dspider" common
</VirtualHost>

重启apache服务,访问localhost:9000,会发现提示没有权限访问。然后再httpd-vhosts.conf文件中增加下面片段,并且重启Apache服务。

<Directory "D:/Learn/Spider/">  
    Options Indexes FollowSymLinks Includes ExecCGI  
    AllowOverride All  
    Require all granted  
</Directory>  

访问localhost:9000访问正常。

下载并且安装VScode

按照微软的惯例,一路NEXT就完成了安装,下载地址:https://code.visualstudio.com/

配置VSCode的PHP开发环境

这里写图片描述
1. 在VSCode的扩展中输入ext:php,安装PHP debug。
2. 在文件=》首选项=》设置

{
    "php.validate.executablePath":"C:/xampp/php/PHP.exe"
}

3.可以建立和浏览刚刚设置的项目。设置Lauch.json如下

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9010   //刚刚xdebug设置的端口
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

可以点击VS的调试,打开浏览器浏览http://localhost:9000/,进行断点调试。

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aofengdaxia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值