Setting up a PHP Debugging Environment

Transcription

Setting up a PHP Debugging Environment
Page |1
Setting up a PHP Debugging Environment
© Software Engineering Solutions, Inc. 2010
Damodar Chetty
Please note that this material is presented without any warranty or support whatsoever. If you wish to
retain my services, I can be reached at damodarchetty@softwareengineeringsolutions.com.
Overview
Any programmer who has spent a weekend getting something to work, will readily admit that any
programming language is only as strong as its debugging support.
While it is a rather straightforward exercise to set up a complete development environment for Java EE,
getting everything spinning smoothly for PHP development was a bit of an uphill struggle.
This article is aimed at making it easier for others who want to tread the same path. I have executed these
steps repeatedly (over a dozen times) to ensure that I have not missed anything critical. The host
operating system used was Windows 7 64-bit.
Software Components
The software components that you will deal with during this exercise include:

VirtualBox
When playing with unfamiliar software, I find it easiest to work with an environment that is totally
under my control. And Oracle's VirtualBox (version 3.2.12) lets me create such environments.
When you've hosed your environment beyond your ability to fix, it is a simple matter to simply
blow it away and restart!

Ubuntu
This is probably the simplest Linux distribution available and is perfectly suited for newcomers to
Linux.

Apache
This free web server is extremely easy to install and run within a Linux environment.

PHP
The language under discussion in this article, PHP is a mature server side scripting language that
© Software Engineering Solutions, Inc. 2010
Page |2
is a must have in a web developer's toolkit. It is simple to learn, available for free on even the
stingiest hosting environments, and is amazingly functional.

MySQL and phpMyAdmin
This is the perfect relational database foil for use with PHP. phpMyAdmin provides an
administrative console for managing your database.

Zend's Eclipse PDT All In One
Eclipse is the best IDE out there (yes, I'm biased), and this single download gives you all that you
need to get started with PHP.

Zend Debugger
This distribution contains the all important ZendDebugger.so and dummy.php files.
For the purposes of this article, the installation of VirtualBox being rather unremarkable, does not merit
further detailed treatment.
A number of people who tried this guide indicated that using an older version of VirtualBox ended up
making their lives miserable when using the guest additions.
Please make sure that you download the latest version.
Do note that screenshots grow stale almost as soon as they are published. However, I've chosen to
provide a detailed blow-by-blow account of the installation process. This is for the simple reason that this
is a fairly involved process, and it's easy for me to miss an important detail otherwise. Please extrapolate
the images to whatever version is current when you are performing this install.
© Software Engineering Solutions, Inc. 2010
Page |3
Setup the Virtual Machine and Virtual Disk
Download the latest version of Ubuntu's 32-bit distribution from http://www.ubuntu.com/desktop/getubuntu/download. The version that is current at the time of this writing is Ubuntu 10.10.
1.
Start up VirtualBox and click the New button
2.
Click Next.
3.
Pick a name for your VM instance. Pick the appropriate OS and version.
© Software Engineering Solutions, Inc. 2010
Page |4
4.
Specify the RAM allocated to this VM. If you pick a number that's too high, your VM might fail to start
up. In that case, try reverting this to the default value of 512 MB.
© Software Engineering Solutions, Inc. 2010
Page |5
5.
Create a new hard disk for this virtual machine.
© Software Engineering Solutions, Inc. 2010
Page |6
6.
Click Next.
7.
Choose the dynamic sizing option
8.
Choose the location of this virtual disk (by clicking the folder icon) and an appropriate max size. I've
had good luck with picking 20GB.
© Software Engineering Solutions, Inc. 2010
Page |7
9.
Click Finish to create your new disk, as a .vdi file, in the specified location.
© Software Engineering Solutions, Inc. 2010
Page |8
10. Click Finish to create your virtual machine.
With this step, you have successfully created a virtual machine that will use your newly created virtual disk
as its boot disk.
© Software Engineering Solutions, Inc. 2010
Page |9
Identify the Installation Image for the Ubuntu OS
1.
In VirtualBox, select your newly created virtual machine and click the Start button.
2.
Click Next.
© Software Engineering Solutions, Inc. 2010
P a g e | 10
3.
Mount the media that represents the Ubuntu distribution's image by clicking the Folder icon to
display the Virtual Media Manager. This is what will be installed to your VM.
4.
Click the Add button, and navigate to the downloaded image.
© Software Engineering Solutions, Inc. 2010
P a g e | 11
5.
The ISO image will be added to virtual media that will be accessible to your virtual machine.
6.
Click Finish.
© Software Engineering Solutions, Inc. 2010
P a g e | 12
7.
Pick the installation image from the drop down list.
8.
Click Finish and let the install proceed.
© Software Engineering Solutions, Inc. 2010
P a g e | 13
Install Ubuntu
1.
Choose Install Ubuntu
2.
Click Forward
© Software Engineering Solutions, Inc. 2010
P a g e | 14
3.
Click Forward
4.
Click Install Now.
© Software Engineering Solutions, Inc. 2010
P a g e | 15
5.
Pick your time zone.
6.
Select your keyboard layout
© Software Engineering Solutions, Inc. 2010
P a g e | 16
7.
Specify your login name and password
8.
Sit back and wait for the install to complete.
© Software Engineering Solutions, Inc. 2010
P a g e | 17
9.
Click Restart Now.
10. Wait for the restart to complete.
© Software Engineering Solutions, Inc. 2010
P a g e | 18
11. Hit Enter when you see this message.
Make sure you haven't closed the VirtualBox window – that can cause it to hang.
© Software Engineering Solutions, Inc. 2010
P a g e | 19
Update your installation of the Ubuntu OS
1.
Choose System > Administration > Update Manager to download and install updates.
2.
Wait until the changes have been downloaded.
3.
Once the download is complete, you will need to enter your password before install can begin.
© Software Engineering Solutions, Inc. 2010
P a g e | 20
4.
Click Restart Now.
© Software Engineering Solutions, Inc. 2010
P a g e | 21
Install Guest Additions
1.
Choose Devices > Install Guest Additions, and Restart the VM.
2.
The CD image should now appear on your desktop.
3.
Double click the CD image to open it.
© Software Engineering Solutions, Inc. 2010
P a g e | 22
4.
Either double click the autorun.sh file, and choose to "Run in Terminal".
Or click the "Open Autorun Prompt" button:
5.
Enter your password and wait.
6.
Restart the VM and your virtual box will now expand to fit the size of its containing window.
7.
Open Firefox and verify that you are able to access the Internet.
© Software Engineering Solutions, Inc. 2010
P a g e | 23
Setup Ubuntu packages
1.
Use the Synaptic Package Manager to install: apache2:
© Software Engineering Solutions, Inc. 2010
P a g e | 24
2.
php5:
3.
Verify that Apache and PHP are successfully installed, by browsing over to http://localhost.
4.
Complete the setup of the Apache web server, by setting your server's document root:
a. cd /etc/apache2/sites-available
b. sudo cp default webserver
c.
sudo gedit webserver
By default, the document root for Apache is /var/www.
Change this to a public_html folder within your home directory. For me, this is
/home/damodar/public_html.
© Software Engineering Solutions, Inc. 2010
P a g e | 25
d. sudo su
e. a2dissite default && a2ensite webserver
f. /etc/init.d/apache2 reload
5.
Verify PHP and Apache are running.
a.
Create the public_html folder and change to it:
mkdir /home/damodar/public_html
cd /home/damodar/public_html
b. Create a test PHP file, phpinfo.php with the following content:
<?php phpinfo(); ?>
c.
Restart Apache
sudo apache2ctl restart
© Software Engineering Solutions, Inc. 2010
P a g e | 26
d. Browse to http://localhost/phpinfo.php.
You should see this page:
© Software Engineering Solutions, Inc. 2010
P a g e | 27
Install MySQL and PHPMyAdmin
1.
Use the Synaptics Package Manager to install: phpmyadmin, php5-mysql, mysql-server, mysql-client
2.
This will invoke the configuration of MySQL – enter a password, and choose apache2 as the web
server to configure.
© Software Engineering Solutions, Inc. 2010
P a g e | 28
© Software Engineering Solutions, Inc. 2010
P a g e | 29
Verify that MySQL and phpMyAdmin are installed.
At a Terminal prompt, type:
$ mysql -u root -p
<enter your root user's password as specified in the above screens>
On pressing Enter you will find yourself at the MySQL command prompt as shown below.
Exercise MySQL by using the following commands:
mysql>
mysql>
mysql>
mysql>
show databases;
use mysql;
show tables;
describe user;
Verify the installation of phpMyAdmin, by browsing on to http://localhost/phpmyadmin.
© Software Engineering Solutions, Inc. 2010
P a g e | 30
Enter your phpMyAdmin password, and click Go. You will be displayed the administration console as shown
below:
Install a JDK for use by Eclipse
Use the Synaptics Package Manager to install default-jdk.
© Software Engineering Solutions, Inc. 2010
P a g e | 31
© Software Engineering Solutions, Inc. 2010
P a g e | 32
Download the Zend packages
1.
Download the Zend all-in-one tool.
http://www.zend.com/en/community/pdt#debugger
2.
Download the Zend Debugger
http://www.zend.com/en/products/studio/downloads
© Software Engineering Solutions, Inc. 2010
P a g e | 33
3.
Extract the downloaded tarballs.
I downloaded these files to my Desktop and extracting them resulted in the layout below:
© Software Engineering Solutions, Inc. 2010
P a g e | 34
Install the Zend Debugger
1.
The debugger's extracted folder looks as follows.
2.
Copy dummy.php to your docroot - /home/damodar/public_html.
3. Copy ZendDebugger.so from 5_3_x_comp over to /usr/local
Use the sudo command to elevate your privileges.
From the directory where thi s extracted file lives, execute:
sudo cp ZendDebugger.so /usr/local
© Software Engineering Solutions, Inc. 2010
P a g e | 35
4.
Verify that the copy worked:
5.
Edit php.ini to set up the debugger.
Go to /etc/php5/apache2, and execute sudo php.ini.
/etc/php5/apache2/php.ini
; This setting is on by default.
;report_zend_debug = 0
zend_debugger.allow_hosts=127.0.0.1/32,127.0.0.1
zend_debugger.expose_remotely=always
zend_extension=/usr/local/ZendDebugger.so
6.
Restart Apache to pick up these changes.
sudo apache2ctl restart
7.
Access phpinfo.php to verify that the debugger has been installed. In the displayed tables, the
following box should change from:
to:
© Software Engineering Solutions, Inc. 2010
P a g e | 36
In addition, you also have an entire section for the debugger.
© Software Engineering Solutions, Inc. 2010
P a g e | 37
Debugging in Eclipse
1.
Execute the eclipse executable from where you extracted the All-in-One package.
2.
Pick an appropriate location for your workspace. This is where your source files will be stored, along
with any metadata required by Eclipse (such as your project files).
© Software Engineering Solutions, Inc. 2010
P a g e | 38
3.
Once Eclipse has started, click the image of the Arrow in the circle (to the far right) to start up the IDE.
4.
A blank perspective will be displayed.
5.
Create a new PHP project: File > New > PHP Project.
© Software Engineering Solutions, Inc. 2010
P a g e | 39
6.
Click Finish:
7.
Create the following PHP Files - File > New > PHP File
a.
getName.php:
<html>
<head>
<title>A form processing site</title>
</head>
<body>
<form name="getName" method="post" action="showName.php">
Enter your Name:
<input type="text" name="name" value="Enter Name Here"><br />
<input type="submit" name="submit" value="Submit"><br />
</form>
</body>
</html>
b. showName.php
<?php
if (isset($_POST["submit"])) {
echo "<h1>Hello ".$_POST['name']."</h1>";
} else {
echo "No name provided";
}
?>
© Software Engineering Solutions, Inc. 2010
P a g e | 40
8. Copy both these files over to your docroot at /home/damodar/public_html
9.
Set up a debug configuration.
This is a bit tricky, so follow along carefully.
a.
Run > Debug Configurations
b. Select PHP Web page, and click the new file icon at the top left.
c.
Enter the Name (getName).
Click the Browse button and pick /test/getName.php from your project's workspace.
Uncheck Auto Generate, and write the URL to the script in your Apache docroot.
© Software Engineering Solutions, Inc. 2010
P a g e | 41
In the image above, I have already setup my PHP Server – which links Eclipse to my Apache
Server. In order to set one up, click the New button.
© Software Engineering Solutions, Inc. 2010
P a g e | 42
i. Click the New button. Enter a server name and the docroot URL. Click Next.
In the Server Path Mapping dialog, map the path on the server (the docroot's
absolute path) to the local path in your Eclipse project. Click the Browse button to
pick your project.
© Software Engineering Solutions, Inc. 2010
P a g e | 43
ii. Click Finish
d. Click the Test Debugger button to verify that Eclipse can communicate with your Apache web
server.
© Software Engineering Solutions, Inc. 2010
P a g e | 44
e.
You should get the success message below:
f.
Click the Apply button to save all the changes you've made so far.
g. Click the Debug button to begin your new debug session.
10. At the prompt, to display the Debugger perspective, choose Yes, and you'll be presented with the
Debug perspective.
The debugger will be poised at the last line of the getName.php file.
Press F8 to let it complete processing the file. The resulting screen should look like this.
© Software Engineering Solutions, Inc. 2010
P a g e | 45
Note the Browser view on the far right – don't use that. Else, you'll get the following message:
Instead, make sure you are interacting with the Internal Web Browser as defined in the next step.
© Software Engineering Solutions, Inc. 2010
P a g e | 46
11. Choose Window > Show View > Other and pick the Internal Web Browser:
12. This opens up a fully functional web browser.
13. Type in the URL, http://localhost/getName.php and hit Enter. Press F8 again when the debugger
pauses at the last line of this PHP file.
14. Enter a name and press Submit.
The debugger will breakpoint on the first line in the showName.php file.
© Software Engineering Solutions, Inc. 2010
P a g e | 47
15. Observe the various displays in Eclipse. You can see the $_POST superglobal with the submitted name.
You can see the execution paused at line 2 in showName.php. And you can now take control of the
execution of this script using the Step Into (F5), Step Over (F6), Step Return (F7), and Resume (F8)
keys.
16. Press F8 to resume the script, and to complete its execution. An appropriate welcome message is
displayed on the web browser view.
© Software Engineering Solutions, Inc. 2010
P a g e | 48
Note:
If you are seeing this error message:
Then you didn't follow my advice and are actually using the Browser Output view. Make haste to the
Internal Web Browser view instead.
That's it folks! Good luck and happy bug hunting!
© Software Engineering Solutions, Inc. 2010
P a g e | 49
An Alternative to avoid having to physically copy files to the docroot
A shortcut option is to use a Linked folder for your source - where the target of the link is your Apache
public_html folder.
1.
Right click the ics325 project, and choose Build Path > Link Source.
As shown, we are linking our Apache document root as the public_html folder in our project.
Click Finish.
2.
Apache's docroot is now visible as part of our ics325 Eclipse project.
3.
Setting up the Debug Configuration is a bit different now.
© Software Engineering Solutions, Inc. 2010
P a g e | 50
a.
First, set up a PHP server configuration as shown.
The "path on server" is your docroot as before, but the "Local path" is your linked alias folder.
© Software Engineering Solutions, Inc. 2010
P a g e | 51
b. The final change is the URL that should be used to invoke the script.
© Software Engineering Solutions, Inc. 2010
P a g e | 52
c.
The scripts added to the docroot now show up without your having to copy the files over
physically.
© Software Engineering Solutions, Inc. 2010