PHP + Ruby Development Environment on Mac OS X

Note: This article is about PHP only, in a forthcoming one I will include Ruby.

There are many possible tools, this is the stack I use:
  • Mac OS X 10.6 Snow Leopard/10.7 Mountain Lion
  • MAMP 2.2
  • Ruby - rvm
  • Aptana Studio 3.4.2
  • Mercurial
  • [Dropbox for manual sync between two computers (e.g. desktop & laptop)]
And these are my particular reasons for it:
  • I'm already familiar with Eclipse, and Aptana comes with built-in support for Ruby, Python, PHP and JavaScript.
  • I develop/maintain a number of applications in Ruby and PHP, and I prefer to use the same tool for both languages.

Installations

Create a PHP project

  • Create a project on Aptana with your PHP application. 
    • I typically use File->Import-> Existing Folder as New Project
    • Let's assume you use the folder /Users/myuser/Workspace/PHPProject
    • If you haven't created a Mercurial repository, you can do it now from the terminal:
cd /Users/myuser/Workspace/PHPProject/
hg init
Open the project on Aptana (right click) and you should see:

  • For this example let's create a public/ folder and a index.php file in it with the following content:
    <?php 
    header("Content-Type: text/plain");
    echo "Hi, I'm your newborn PHP project";
    • Add a virtual host in apache. Edit /Applications/MAMP/conf/apache/httpd.conf & uncomment the line:   
    # Virtual host
    Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
    •  Edit /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
      • We will use the first sample entry, dummy-host.example.com
      • Change the DocumentRoot directive to point to the public folder of this project.
      • Also change port 80 to 8888
    NameVirtualHost *:8888
    
    <VirtualHost *:8888>
        ServerAdmin webmaster@dummy-host.example.com
        DocumentRoot "/Users/myuser/Workspace/PHPProject/"
        ServerName dummy-host.example.com
        ServerAlias www.dummy-host.example.com
        ErrorLog "logs/dummy-host.example.com-error_log"
        CustomLog "logs/dummy-host.example.com-access_log" common
    </VirtualHost>
    
    127.0.0.1       dummy-host.example.com localhost
    
      • Verify it works
    $ ping dummy-host.example.com
    PING dummy-host.example.com (127.0.0.1): 56 data bytes
    64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.063 ms
    64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.022 ms
    64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.112 ms
    ^C
    
    Hi, I'm your newborn PHP project
      If you're not familiar with Mercurial, this is my minimal cheatsheat.

      This is our first version of this application. We commit it to the Mercurial repository: Right click, Team, Commit...

      Xdebug

        • Uncomment at the end of the php.ini (see its location in the phpInfo page), plus add the line below to enable "remote" (Apache) debugging
        [xdebug]
        zend_extension="/Applications/MAMP/bin/php/php5.5.3/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so"
        xdebug.remote_enable = On
        
        • Restart MAMP, refresh phpInfo page and verify that now an xdebug section appears.
        • Aptana:

          • Aptana:
            • Window -> Open Perspective -> Debug
            • Bug Icon, Debug preferences, New_configuration
            • Set PHP server:

            •  Set the starting PHP script index.php, unset "Auto Generate" and empty the field at right

          We are ready to debug our "application". Press the "Debug" button and a new browser window should popup showing our magnificent message:

          Now we can set a breakpoint, and debug again; execution should stop at the breakpoint (browser window waiting) and should finish by pressing F5.