How to Set up a PHP Development Environment for Drupal under Windows

So I bought a new notebook computer, an HP dv5-1250us, which came with the unloved Windows Vista, 64-bit version. I haven't wasted any time on Vista so far, and don't intend to, either. So the plan was to install Windows XP until Windows 7 would be available, but it turns out that the XP installation disks don't have the required SATA drivers. Rather than mucking with custom XP installation disks, I'm going for the Windows 7 64-bit RC now.

Get the Servers Up and Running

Our first ingredient is XAMPP (Donationware), which (at the time of this writing) includes
* Apache 2.2.12 (IPv6 enabled) + OpenSSL 0.9.8k
* MySQL 5.1.37 + PBXT engine
* PHP 5.3.0
* phpMyAdmin 3.2.0.1
* Webalizer 2.21-02 + GeoIP lite
* FileZilla FTP Server 0.9.32
* msmtp 1.4.17

These are all the servers we'll need, and the best thing: they're easy to maintain and upgrade, because essentially, you just copy them to your hard disk and start them, with hardly any installation required. However, be sure to check the compatibility list!

It turns out that XAMPP 1.7.1 can run on Vista, but without Xdebug support, and maybe this will give us trouble on 64-bit Win7. Luckily, Microsoft has just released Windows 7 XP Mode, which provides a way to seamlessly run older applications inside an XP virtual machine, but on the Win7 desktop! This will be our backup strategy if we run into problems...

Ok, so download XAMPP for Windows, in .zip format, extract it to D:\PDev\xampp, and install it using the "'Installation' without the Installer" method, of course. We don't need SSI, IPv6, mod_perl, and Apache::ASP.

We can give it a try and start xampp-control.exe to see what happens, but I find it's unable to start Apache. So, we create a desktop icon for xampp-control.exe, open its Properties, and set Compatibility Mode to WinXP SP3. This allows starting Apache and MySQL, and if we open http://localhost/, we can actually see XAMPP's welcome screen. Under http://localhost/phpmyadmin/ we can confirm that MySQL is running, too.

Enable Xdebug

Locate php\php.ini and make the following adjustments:

[PECL]
zend_extension = "D:\PDev\xampp\php\ext\php_xdebug.dll"

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "localhost"
xdebug.remote_mode = "req"
xdebug.remote_port = 9000

Stop and restart Apache and look at the phpinfo() — we should now see something like

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
    with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans

where the third line confirms that Xdebug is running.

Install the IDE

An easy way to get a fully-featured development environment is to install the (Eclipse-based) Aptana Studio and the Aptana PHP Development Environment.

Rather than messing with Eclipse, let's just get the stand-alone version of Aptana Studio (.zip distribution), currently at version 1.5.1. Aptana Studio is free; there's a payable Pro version. When you install it, you get to try the extended feature set for 30 days.

Aptana also requires a 32-bit Java run-time environment that you can get for free from http://java.com/.

Start AptanaStudio.exe and add Aptana PHP and jQuery support. Add CVS, too.

For compatibility with Drupal coding standards, go to Window|Preferences and make the following changes:

  • Aptana|Editors|General: use spaces rather than tabs
  • General|Workspace: set Text File Encoding to UTF-8 and New Text File Line Delimiter to Unix
  • Aptana|Editors|PHP: add the following file associations to the PHP Source File data type: *.engine, *.theme, *.install, *.inc, *.module and *.test
  • Team|CVS: turn Convert text files to use platform line ending off
  • (see also http://drupal.org/node/75242)

Check Out Drupal HEAD as a Project

Follow the directions at http://drupal.org/node/37615. Note: you have to create the directory where you want to put the source tree. I've checked mine out to D:\PDev\Drupal\drupal-head.

Configure Apache so that You Can Run Multiple Sites in Parallel

Edit D:\PDev\xampp\apache\conf\httpd.conf to append the following lines:

NameVirtualHost 127.0.0.1:80
Include "D:/PDev/vhosts.d/*.conf"

Then you can create as many .conf files as you want to add sites.

I prefer to create sites at the root level. To that end I used to simply add hostnames to the windows32/drivers/etc/HOSTS file as aliases to localhost. For some reasons I couldn't get this to work with Windows 7 (it doesn't have the localhosts entry in the HOSTS file anymore!). See here for some info on this.

So, given that we have to make do with the localhost domain, we'll use different ports for different sites. Create a file named dh.conf with the following content

Listen 0.0.0.0:8700
<VirtualHost localhost:8700>  
  ServerName localhost:8700
  
  DocumentRoot "D:/PDev/Drupal/drupal-head"
  <Directory "D:/PDev/Drupal/drupal-head">
    AllowOverride All
    Order Deny,Allow
    Deny from All
    Allow from 127.0.0.1
  </Directory>

  CustomLog "D:/PDev/Drupal/drupal-head/drupal.head-access.log" common
  ErrorLog "D:/PDev/Drupal/drupal-head/drupal.head-error.log"
</VirtualHost>

Create your database (for MySQL use your local phpMyAdmin to create a user and a database to go with it). Create the D:\PDev\Drupal\drupal-head\sites\8700.localhost\settings.php file according to the Drupal instructions. Go to http://localhost:8700/ to install your site.

Set up the IDE to use the Xdebug Debugger

Open Run|Debug Configurations. We want to use our real Apache, not the built-in PHP interpreters: Right-click PHP Web Page and select New; name it localhost:8700. Click the Plus button to the right of the PHP Server drop-down and select Apache.
Enter the coordinates for our Apache installation. Specify index.php as the Initial Script.

Go to the Common tab and put this configuration into the Debug menu.

Enjoy!

Now start debugging by clicking on the side of the "Bug" button and selecting your configuration from the drop-down menu.

Caveats

Well, that's the theory, so far. Xdebug seems to work, even though it's not supposed to, but all variables appear as <Uninitialized>! I've opened an issue on xdebug.org, and Derick Rethans pointed me to this archive.

This is bleeding edge here: 547846: Drupal installation fails on MAMP & XAMPP due to pass-by-reference error.

More Caveats

What about Drupal 6? Arghh! It turns out that D6 currently doesn't run on PHP 5.3: #360605: PHP 5.3 Compatibility. We have to get XAMPP 1.7.1 with PHP 5.2.9, and the XDebug version to use is 5.2 VC6 (32 bit) / php_xdebug-2.0.5-5.2.dll.

The relevant php.ini sections should be:

[Zend]
zend_extension_ts = "D:\PDev\xampp-1.7.1\php\zendOptimizer\lib\ZendExtensionManager.dll"
;zend_extension_manager.optimizer_ts = "D:\PDev\xampp-1.7.1\php\zendOptimizer\lib\Optimizer"
zend_optimizer.enable_loader = 0
zend_optimizer.optimization_level=15
;zend_optimizer.license_path =
; Local Variables:
; tab-width: 4
; End:

[XDebug]
;; Only Zend OR (!) XDebug
zend_extension_ts="D:\PDev\xampp-1.7.1\php\ext\php_xdebug.dll"
xdebug.remote_enable=true
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
;xdebug.profiler_enable=1
;xdebug.profiler_output_dir="D:\PDev\xampp-1.7.1\tmp"

and we should end up with something like

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with Zend Extension Manager v1.2.0, Copyright (c) 2003-2007, by Zend Technologies
    with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans

Sending Mail

Finally, if your test installation needs to be able to send mail, then change your php.ini similar to the following:

[mail function]
; For Win32 only.
;SMTP = localhost
SMTP = mail.bluewin.ch
smtp_port = 25

; For Win32 only.
sendmail_from = phpdev@localhost

Specify your ISP's mail server, of course, and whatever sender account you want. This will only work with an SMTP server that does not require logging in (such as your ISP's server, because you've already logged in for Internet access).