How to launch and debug php using VS Code? - php

I'm new to VS Code and the php world. My experience is more with heavyweight IDE such as Visual Studio. However, I have a need to setup a php environment on my dev machine and am having some trouble getting it to work properly.
My environment is a Win 10 dev machine. I am using VS Code and php ver 5.5. I have properly installed the xdebug extension and verified it is properly installed. I have also installed the php-debug extension in VS Code.
The challenge I'm having and have been unable to find any useful information through google is launching the php website from within VS Code and then being able to debug it.
A few things I have tried, but haven't worked.
I installed the iis-express extension to VS Code which allows for running any folder through iis express. https://marketplace.visualstudio.com/items?itemName=warren-buckley.iis-express
This works, but the website doesn't display properly. IIS returns an error message saying the site is not properly configured. It's apparently missing a mapping or something along those lines.
I followed this blog. http://blog.denouter.net/2015/05/run-php-from-visual-studio-code.html and am able to run the website using the built in php web server.
Installed webmatrix and let the windows platform installer correctly install and wire up iis express to work with php. The same folder works fine when running from webmatrix.
Installed the php-debug extension to VS Code. https://github.com/felixfbecker/vscode-php-debug
Here is what I think I'm missing. I believe I need to launch the website from within VS Code for the debugging to work. I can't figure out how to "launch" the php website from within VS Code. The php-debug extension from VS Code only supports launch. It doesn't support "attach" mode. I suspect this is why when I run the website outside VS Code, the debugger doesn't work. Let me be clear, the debugger is working when I hit F5, it just doesn't ever stop on any breakpoints.
To summarize: How can I launch and debug my php website from within VS Code? I'm looking for a detailed step by step guide.
Thank you

I am the author of vscode-php-debug. You do not need to "launch the website" from inside VS Code. When you start the "Listen for XDebug" configuration from the debug section, VS Code (or rather, my debug adapter) will listen on port 9000 for XDebug. You need to run a web server like Apache, IIS or nginx locally on your PC and configure it to serve PHP files - this has nothing to do with VS Code. Then simply open a web browser and navigate to localhost and XDebug will connect to the debugger, stopping on breakpoints.
The two necessary settings in php.ini are:
xdebug.mode = debug
xdebug.start_with_request = yes
(Do not forget to add the subtitle [XDebug] before the settings.

Related

which server to install for phpEclipse and how to do it?

I know this is a newbie question, but i want to study php. i have phpeclipse and its running well, the problem is when i run a simple html file with the default server the page says cant display this page and thats it. i dont know if i need to install a new server or is there something wrong with the server configurations. ive tried installing tomcat on my computer but i cant see it under windows->preferences->server->runtime enviroments->add. iv also tried under the same page to fetch geronimo v2.2 and 3.0 and the dependencies were wernt satisfied. iv also tried the repository for zend debugger. what do i need to do in order to compile and see php and html pages with php eclipse?
You don't need to compile PHP.
Depending on your operating system and PHP version there are a few options.
As of PHP 5.4+ you can run your application using PHP's built in server, on the command line, if you cd into your website root directory (i.e. where the index file is) and then run the following
php -S localhost:8989
Then if you navigate to http://localhost:8989 in your browser you should see your application.
If you are unable to run the inbuilt web server, you need to look at Apache2. Most PHP websites run inside the Apache 2 web server. You will generally need a *AMP (WAMP/LAMP) environment.
L/W (Linux / Windows)
A pache (Web server)
M ySQL (Database)
P HP
Hope this is enough of a pointer ;)

How to debugging php on a VPS using phpStorm and Xdebug?

I have trying to get this done for days...and reading through about 20 tutorials, documents, etc...but still no luck.
Here is the thing. I installed wordpress(just for example, can be any php program) on my VPS, and hoping to debug it from my macbook and my desktop. This is clearly a remote debugging thing. So I here is what I done:
1.Installed right version of xdebug and located its .so file on VPS.
2.Changing php.ini and 20-xdebug.ini file on VPS, my settings currently are:
zend_extension=/usr/lib/php5/20131226/xdebug.so
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.remote_port=9200
xdebug.show_local_vars=0
xdebug.var_display_max_data=10000
xdebug.var_display_max_depth=20
xdebug.show_exception_trace=0
xdebug.remote_log=/var/log/xdebug.log
;xdebug.remote_host=202.84.93.66
The last line, which is comment out is my desktop IP address, where I run phpStorm. Based on xdebug's documents, if you set remote_connect_back=1, you won't need this host ip to be specifici.
On phpStorm Side, I "start new project from existing code, and specificy the source root on VPS, map it with one of my local dest. PhpStorm just downloaded all the files and after setting up "Automatic upload", the sync is perfect.
On phpStorm side, I specificy the PHP intercepter as the remote one on my VPS.
On phpStorm side, I changed my debugging port to 9200, as same as the one I using on VPS.
Using the booklet method phpStorm provide, I put them on my firefox bootkmarklet.
Click listen button on phpStorm, set breakpoint, go to firefox open the page, click 'start debuging', refresh.....BUT NOTHING HAPPENS!
I also tried to using the tranditional methods, which set a run configration way as both php-webapplication or php-remote-debug...but still not working.
At php storm, the web debug validation, I could pass all the testing there though....
Can anyone help me with this? I think I must be doing wrong on some very fundation part since I saw all the tutorial are so simple set, but this already took me about 3 days to figure out....
Thanks!

Netbeans and CodeIgniter on Windows and Linux?

I installed a plugin for NB 7.0.1 which enabled CodeIgniter but when I went to create a project using it, I was told I need to specify a PHP interpreter for Netbeans. I tried to do this using the php.exe you get with Xampp but I get a Java null pointer error. I can't seem to fix this but I'm thinking, why do I need an interpreter on Windows when the Linux machine can do that? I have already set this up on the VM.
Any advice from here? Thanks a lot!
I am using NB 7.1.1 and had similar issue with CodeIgniter plugin when creating a new project. NetBeans would complain No PHP Interpreter was defined in Tools->Options->PHP General. It would not enable finish button so a New Project could not be setup.
I just created a dummy batch file called nb_cli.bat and pointed NB to that for the PHP interpreter. I don't plan on executing (or debugging) CodeIgniter CLI code on local machine so that file will likely never get executed.
If you want to debug or run php locally, make sure xammp works OK outside of NetBeans. But if not, this fix should get you by the NetBeans issues.
I have a separate linux server on my local network for development as I don't need apache/php locally when I use multiple machines for dev/testing/etc.
NetBeans should bugfix that by downing that PHP Interpreter message from an Alert preventing project setup to a warning that No PHP interpreter is defined (or found) so CLI testing may not function.
As an alternative, if you have a linux box with NB installed, you can create the New project there and copy the whole folder (including the nbproject folder) to your PC and you should be able to get by having No PHP Interpreter defined.
Hopefully that gets you going with NB and CodeIgniter. If you need additional details, just ask.

xdebug, php on remote server, netbeans

I have my 1and1 hosted web site. I've got server-side php I need to debug.
All I've found mentions running/installing xdebug when you have a local web server
with php and mysql on a local web server.
However, I'm using the remote web server hosted at 1and1 hosting.
I'm using and loving netbeans, wow great IDE.
Anyways, how (if it's even possible) can I install then use the xdebug
debugger to debug my php code running at 1and1 from within Netbeans?
I'm wondering if it is my hosting company (1and1) that has to have xdebug installed.
I'm running the latest version of Netbeans but when I press the debug button on
the toolbar I see an error message popup that says
"There is no connection from xdebug detected within 40 seconds. The reasons could be that xdebug is neither installed nor properly configured. Be sure that your php.ini file contains these entries:
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost (or hostname)
xdebug.remote_port=9000
So I searched my harddisk, and despite having installed the full Netbeans
IDE, I have no php.ini file.
So can I make xdebug work remotely, ie. can I debug my php code as it runs on the
remote 1and1 web server, using Netbeans and xdebug?
I've only been using Netbeans for 2 days and for all I know, xdebug is not even a part
of Netbeans. Reason I say that is, I can 'run' my php remotely just fine -- it executes on the remote 1and1 web server when I push 'run' inside Netbeans -- and then the client side output on my local machine is correct. But I cannot step into the code with xdebug by pressing the 'Debug' button on the Netbeans toolbar -- is it possible xdebug did not get installed correctly with Netbeans?
xdebug should be installed inside the php folder and of course the php folder is located on the server where php is installed... at your hosting provider.
I would not expect an hosting provider to install xDebug as it seems to me it would be a huge security hole.
xDebug is not part of Netbeans and it is not part of PHP by default.
Now, looking at things on a more positive viewpoint, if you are going to develop PHP, you NEED a local server. It's very unprofessional to develop code directly on the live server.
Grab a copy of xampp and install it on your computer. Grab a copy of xdebug and install it following the instructions. Restart Apache. There, now you have a testing server.
Once you have a complete set-up on your computer, copy your PHP application's folder inside the "htdoc" folder.
NOW, and only now, should you consider developing in PHP. Develop locally, on your own testing server, xdebug will work whenever you start a debugging session from inside Netbeans.
When everything works, upload your files on your hosted space.

How to get Visual Studio Development Server to work with PHP?

I'm a .NET specialist working with a PHP/Flash programmer to deliver an app via IIS. After jumping through 17 hoops of fire and defeating a gargoyle in a gruesome battle to the death, I managed to get my IIS 7.5 server to cooperate with PHP, and now if you browse to my public web site, you can see the Flash objects happily doing their behind-the-scenes stuff with PHP, whatever that may be.
But... when I'm debugging my app using the VS Development Server, that still apparently doesn't know how to cope with PHP: I'm getting the same HTTP 405 (Method Not Allowed) errors that I was getting on IIS before aforementioned gargoyle breathed its last ("The HTTP verb POST used to access path '/php/blah.php' is not allowed.").
So, what do you have to do to get the VS Dev Server to play nice with PHP?
My PHP Debugging Setup
I'm a .NET developer who has been swimming in the PHP pool for the past few months.
Spoiled by the VS.NET IDE, debugger, and strongly typed platforms, I was determined to create a PHP development environment that closely resembles my .NET debugging experience.
NOTE: It may be easy to setup PHP debugging in IIS for new PHP applications. However, the steps listed below break down some very involved steps as I found required to debug in WordPress and Joomla. Only follow these steps if you aren't having any luck with getting debugging to work.
Disclaimer:
The full set of steps to complete this setup are quite involved. I'm throwing this together ad hoc in hopes it will help others in need of setting up a professional development environment with little background in Linux based systems.
These steps are not guaranteed to work and may be very sensitive to environment settings. I spent a lot of time going through the effort of trial and error until I got this working.
Along the way, I have to give credit to a few good online resources you should review to get started. These do not address debugging or development setup. I'll provide details below.
Step by Step Guide: Installing XAMPP and WordPress on Windows
Since I'm new to posting here, I can't add more than one link. Just Google the following references:
- Google: sixrevisions tutorials web-development-tutorials using-xampp-for-local-wordpress-theme-development
Securing Your XAMPP Installation
Google: robsnotebook xampp-builtin-security
DEVELOPMENT SETUP
WAMP Stack vs IIS for PHP:
First, I did not use IIS to host my PHP application. I wanted to keep these web servers separate and use one of the available WAMP Stacks to develop against. This allowed me to manage and study configuration settings that would be used on Apache and Linux. If I was deploying the PHP App to a Windows host, I would have chosen an IIS setup. Again, this was a choice based on creating similar configured environments with a Linux host.
What is WAMP Stack?
For those unfamiliar, a WAMP Stack stands for a distribution package of Apache, MySQL, and PHP running on Windows. Other flavors include LAMP (for Linux), MAMP (for MAC), and others. There are several flavors within the community that provides WAMP Stacks to work with. I originally found BitNami interesting to work with. However, I was not happy with the Control Panel used to manage the different services within the Stack.
Selecting XAMPP for Windows
At the end of the day, I went with a package called XAMPP (Cross Platform Apache, MySQL, PHP, and PERL). The second P in XAMPP provides PERL support that is lacking in the other WAMP Stacks (WAMPServer, bitnami, and a few others). I also like XAMPP because it has what appears to be a more active community and the stack has been very stable for me. Additional standout features of XAMPP to consider is support for hosting an FTP Server, apache based mail server. The option to run MySQL and Apachi as services or local running app is easily toggled with a click of a button.
Setting Up XAMPP for Development
Setup of XAMPP is straight forward. My experience is with the previous release 1.7.3. They just released 1.7.4. Go to:
Google: apachefriends xampp-windows
and scroll down the page till you see the install links. If you want to work with a release that has matured with some time, you can still find 1.7.3 at the following link:
Build Link: www (dot) apachefriends (dot) org (/) download (php) ?xampp-win32-1.7.3.exe
Installing XDebug for PHP Debugging
This applies to installing PHP on WAMP with Apache or on IIS. Selecting the correct version of this dll isn't straight forward.
First, go to:
Google: xdebug org download (php)
and review the various versions available.
The versions are not very intuitive to parse. Follow this guide to understand it:
Breakdown of XDebug Name: [php_xdebug-2.1.0-5.3-vc6.dll]
- XDebug version 2.1.0
- Compatible with PHP 5.3
- VC6: Use for Apache ver 1 or 2
- VC6 indicates compiled with legacy Visual Studio 6 Compiler
- VC9: Use for IIS
- VC9 indicates compiled with Visual Studio 2008.
- NTS (not listed in the name above) indicates Non Thread Safe.
- The version listed is thread safe.
PHP.ini Config Setting
NOTE: Since I've not set this up on IIS, I'm not sure what the specific settings are to apply. However, this is document throughout the web.
For WAMP/XAMPP:
Locate \php\php.ini file.
Comment out line by adding semi colon to start of line.
;zend_extension = "php\ext\php_xdebug.dll"
Locate [XDebug] Section
Use settings similarly listed below and set to your path:
[XDebug]
;Common Settings
zend_extension = "P:[Fully Qualified Path]\xampp\php\ext\php_xdebug-2.1.0-5.3-vc6.dll"
xdebug.profiler_enable = 1;
xdebug.profiler_output_dir = "P:[Fully Qualified Path]\xampp\tmp"
xdebug.profiler_output_name = "xdebug_profile.%p";
xdebug.remote_enable = 1;
xdebug.remote_host = "127.0.0.1";
xdebug.remote_port = 9000;
;Make sure your IDE setup on port 9000. Some will default to 7870.
xdebug.trace_output_dir = "P:[Fully Qualified Path]\xampp\tmp";
;************
;Needed for IDE Support
;************
xdebug.idekey = "vsphp";
;This value can be arbitrary or may require something specific for your IDE.
xdebug.remote_autostart = 1;
xdebug.var_display_max_depth = 5;
Debugging with IDE
I use 2 IDEs to develop in PHP:
phpDesigner7
Google: mpsoftware phpDesigner
VS.PHP (a PHP plugin for VS.NET 2010).
Google: jcxsoftware vsphp
I want to love VS.PHP in VS.NET 2010, however, it's not the greatest experience in step through debugging. phpDesigner7 has been much better for debugging and access to local variables, intellisense, and using running eval commands during debugging. VS.PHP is so close to being great, but you will be frustrated if you have little patience. I still prefer it as my development editor of choice, even for PHP.
Regardless of IDE, most IDEs provide internal debugging support without any of the extra steps I listed above. However, these applications will launch the php app in a private webserver using IDE specific php.ini settings.
I'm doing a lot of custom integration with WordPress, Joomla, and .NET applications. Therefore, I need the debugger to use the php.ini settings for my various platforms. For me to debug these platforms, I configure my IDEs to essentially run in remote debug mode. The IDE and web server coordinate run time execution with the help of XDebug acting as a broker and providing the necessary debugging symbols to the IDE debugger.
Configuring IDE for Debugging
Final step is to configure your IDE so the debugger can connect to the web server.
The 3 settings to look for are as follows:
PHP-CGI: P:[Fully Qualified Path]\xampp\php\php-cgi.exe
PHP.INI: P:[Fully Qualified Path]\xampp\php\php.ini
Listen Port: 9000
Different IDEs may label these settings differently and require additional settings. These should be the most important ones to look for.
NOTE: Use [php\php-cgi.exe], not [php\php.exe] for debugging. The php-cgi.exe is required to run php.exe on windows.
I hope this gets you started with attaching your IDE to the web server, setting breakpoints, and doing a lot with line by line debugging.
You will not be able to make Visual Studio Development Server work for PHP. However you can easily use IIS Express (http://weblogs.asp.net/scottgu/archive/2010/06/28/introducing-iis-express.aspx) which will not only give you PHP, but also install WinCache, URL Rewrite and other features that are super important for PHP development.
Also, note that other platforms like Classic ASP are supported as well, where in Cassini they are not.
One option is to not run cassini and instead just get IIS installed on your dev box and run everything through there.

Categories