一键搭建php本地测试环境_如何在PHP中设置本地调试环境

一键搭建php本地测试环境

Recently I started focusing more on PHP, and I needed to set up a local debugging environment. Since there aren’t many tutorials on how to do it, I’ve encountered some problems on how to get the setup up and running.

最近,我开始更加关注PHP,并且需要设置本地调试环境。 由于没有太多关于如何执行此操作的教程,因此我在如何启动和运行安装程序时遇到了一些问题。

By combining a few tutorials and spending a few hours on several forums, I came up with this text. The app I am working on runs inside of a Vagrant box which is shaky somehow. It collapses from time to time so I have to redo this setup, and therefore I know this setup works :).

通过结合一些教程并在几个论坛上花费几个小时,我得出了本文。 我正在使用的应用程序在Vagrant盒子中运行,这有点有些摇晃。 它不时崩溃,所以我必须重做此设置,因此我知道此设置有效:)。

The goal of this tutorial is to get the Xdebug working on your server and then point PhpStorm to Xdebug.

本教程的目的是使Xdebug在您的服务器上运行,然后将PhpStorm指向Xdebug。

Let’s get started.

让我们开始吧。

先决条件 (Prerequisites)

If you don’t have Xdebug installed on your server but are using PHP7, you can do it by using Tailored Installation Instructions from the official Xdebug site here.

如果你没有在服务器上安装的Xdebug但使用PHP7,您可以通过使用量身定制的安装说明从官方网站的Xdebug做这里

However, as my project is using PHP5, I’ve had to do it the old fashioned way: looking it up on forums.

但是,由于我的项目使用的是PHP5,所以我不得不采用老式的方式: 在论坛上进行查找

In this tutorial I will assume you have Xdebug installed.

在本教程中,我将假定您已安装Xdebug。

入门 (Getting started)

First you need to find the location of the xdebug.so file on your server. Copy the location somewhere as you will use it later on.

首先,您需要在服务器上找到xdebug.so文件的位置。 将位置复制到某个地方,以备后用。

locate xdebug.so

Now you need to navigate inside your server to a location: /etc/php5/apache2/conf.d/ and check if the file 20-xdebug.ini exists there. If it doesn’t exist, create one. You can create one using commands such as touch, vim, vi, nano and so on.

现在,您需要在服务器内部导航到以下位置:/etc/php5/apache2/conf.d/并检查文件20-xdebug.ini是否存在。 如果不存在,请创建一个。 您可以使用诸如touch,vim,vi,nano等命令创建一个。

You can see my project is made in PHP5, so change your folder name/location according to your version of PHP.

您可以看到我的项目是用PHP5制作的,因此请根据您PHP版本更改文件夹名称/位置。

Now open the 20-xdebug.ini file and paste this in:

现在打开20-xdebug.ini文件并将其粘贴到:

zend_extension=”location to your xdebug.so file”
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_host=”your localhost address”
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.remote_handler=”dbgp”
xdebug.remote_mode=req
xdebug.remote_cookie_expire_time=-9999
xdebug.remote_log=”/tmp/xdebug.log”
xdebug.var_display_max_depth=15
xdebug.profiler_enable=0
xdebug.idekey=”phpstorm”

From the explanation of the 20-xdebug.ini file that is written bellow, you will see that some of these settings are not needed, or that they are set to a default value. I keep these values in the 20-xdebug.ini file because they are good to know.

从下面写的20-xdebug.ini文件的说明中,您将看到不需要其中的某些设置,或者将它们设置为默认值。 我很高兴将这些值保存在20-xdebug.ini文件中。

20-xdebug.ini文件的说明: (Explanation of the 20-xdebug.ini file:)
  • xdebug.remote_enable — controls whether Xdebug should try to contact a debug client which is listening on the host and port as set with the settings

    xdebug.remote_enable —控制Xdebug是否应该尝试联系调试客户端,该客户端正在使用设置来侦听主机和端口
  • xdebug.remote_port — The port to which Xdebug tries to connect on the remote host. Default is 9000.

    xdebug.remote_port — Xdebug尝试在远程主机上连接到的端口。 默认值为9000。
  • xdebug.remote_host — Selects the host where the debug client is running. Default is localhost.

    xdebug.remote_host —选择运行调试客户端的主机。 默认值为localhost。
  • xdebug.remote_autostart — when this setting is set to 1, Xdebug will attempt to start a remote debugging session and try to connect to a client.

    xdebug.remote_autostart —当此设置设置为1时,Xdebug将尝试启动远程调试会话并尝试连接到客户端。
  • xdebug.remote_connect_back — If enabled, the xdebug.remote_host setting is ignored and Xdebug will try to connect to the client that made the HTTP request. Default is 0.

    xdebug.remote_connect_back —如果启用,则xdebug.remote_host设置将被忽略,Xdebug将尝试连接到发出HTTP请求的客户端。 默认值为0。

  • xdebug.remote_handler — Can be either ‘php3’ which selects the old PHP 3 style debugger output, ‘gdb’ which enables the GDB like debugger interface or ‘dbgp’ — the debugger protocol. The DBGp protocol is the only supported protocol. Default is dbgp.

    xdebug.remote_handler —可以是“ php3”(用于选择旧的PHP 3样式的调试器输出),可以是“ gdb”(用于启用调试器接口等GDB),也可以是“ dbgp”( 调试器协议) 。 DBGp协议是唯一受支持的协议。 缺省值为dbgp。

  • xdebug.remote_mode — Selects when a debug connection is initiated. This setting can have two different values: req — Xdebug will try to connect to the debug client as soon as the script starts. jit — Xdebug will only try to connect to the debug client as soon as an error condition occurs.

    xdebug.remote_mode —选择何时启动调试连接。 此设置可以具有两个不同的值:req-脚本启动后,Xdebug将尝试连接到调试客户端。 jit — Xdebug仅在出现错误情况时才尝试连接到调试客户端。
  • xdebug.remote_cookie_expire_time — This setting can be used to increase (or decrease) the time that the remote debugging session stays alive via the session cookie. Default is 3600.

    xdebug.remote_cookie_expire_time-此设置可用于增加(或减少)远程调试会话通过会话cookie保持活动的时间。 默认值为3600。
  • xdebug.remote_log — If set to a value, it is used as filename to a file to which all remote debugger communications are logged.

    xdebug.remote_log —如果设置为一个值,它将用作所有远程调试器通信都记录到的文件的文件名。
  • xdebug.var_display_max_depth — Controls how many nested levels of array elements and object properties are when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or through Function Traces. Default is 3.

    xdebug.var_display_max_depth —控制使用xdebug_var_dump()xdebug.show_local_vars或通过函数跟踪显示变量时,数组元素和对象属性的嵌套级别为多少。 默认值为3。

  • xdebug.profiler_enable — Enables Xdebug’s profiler which creates files in the profile output directory. Default is 0.

    xdebug.profiler_enable-启用Xdebug的探查器,该探查器在探查输出目录中创建文件。 默认值为0。

  • xdebug.idekey — Controls which IDE Key Xdebug should pass on to the DBGp debugger handler. The default is based on environment settings.

    xdebug.idekey —控制应将哪个IDE密钥Xdebug传递给DBGp调试器处理程序。 默认值基于环境设置。

Save the file and restart the Apache server:

保存文件并重新启动Apache服务器:

sudo service apache2 restart

Xdebug和PhpStorm (Xdebug and PhpStorm)

First you need to open PhpStorm and select Run > Edit Configuration. There you should select + (Add new configuration) and choose “PHP Remote Debug”.

首先,您需要打开PhpStorm并选择“运行”>“编辑配置”。 在那里,您应该选择+(添加新配置),然后选择“ PHP远程调试”。

Change the name of the configuration from “Unnamed” to something else. I’ve chosen the name “Tutorial” for obvious reasons ;) Then check the “Filter debug connection by IDE key”.

将配置名称从“未命名”更改为其他名称。 由于明显的原因,我选择了“ Tutorial”这个名称;)然后检查“通过IDE密钥过滤调试连接”。

Select the Servers button (…) and then select + (Add new Server).

选择服务器按钮(…),然后选择+(添加新服务器)。

  • Rename your Server to something more soothing. I’ve chosen “localbackend1” in this example.

    将您的服务器重命名为更舒缓的名称。 在此示例中,我选择了“ localbackend1”。
  • In Host field type in your localhost. Set Debugger to Xdebug.

    在主机字段中,输入您的本地主机。 将调试器设置为Xdebug。
  • Select “Use path mapping” and point to the folder of your app.

    选择“使用路径映射”,然后指向您的应用程序的文件夹。

Click on Apply. You will be returned to the previous window where you will see that the IDE key is editable, and now you need to enter a key. I always put “phpstorm” or something similar.

单击“应用”。 您将返回到上一个窗口,在该窗口中您将看到IDE密钥是可编辑的,现在您需要输入一个密钥。 我总是放“ phpstorm”或类似的东西。

You will notice that the IDE key “phpstorm” is already in your 20-xdebug.ini file:

您会注意到,IDE密钥“ phpstorm”已经在您的20-xdebug.ini文件中:

xdebug.idekey=”phpstorm”

You should also select Single instance only as it will stop you from launching more than one instance of the same project.

您还应该仅选择“单个实例”,因为它将阻止您启动同一项目的多个实例。

Click on to Apply and Voilà!

单击“申请”,然后发贴!

You are done with the setup!

您已完成设置!

测试和包装 (Testing and wrapping up)

Now you are complete. You should see the Tutorial configuration we created in the upper right corner of PhpStorm.

现在您完成了。 您应该在PhpStorm的右上角看到我们创建的Tutorial配置。

You can test it by setting a breakpoint in your project. Click on the “bug” button in the configuration window of Tutorial and then run your app.

您可以通过在项目中设置一个断点来对其进行测试。 单击教程的配置窗口中的“错误”按钮,然后运行您的应用程序。

That should be all, if everything is OK you should hit the breakpoint.

仅此而已,如果一切正常,则应达到断点。

And remember, nothing beats the feeling of seeing run time values while debugging.

请记住,调试时看到运行时值的感觉无可比拟。

Thank you for reading! Check out more articles like this, and other fun stuff I make on my Github profile: https://github.com/GoranAviani

感谢您的阅读! 查看更多类似这样的文章,以及我在Github个人资料上所做的其他有趣的东西: https : //github.com/GoranAviani

翻译自: https://www.freecodecamp.org/news/set-up-xdebug-phpstorm-in-php5-7-6a8386304fc6/

一键搭建php本地测试环境

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值