HOWTO: Make a Mac development environment with Eclipse, Subversion, Apache, MySQL+phpmyadmin, and PHP5

At my job they use a more elaborate setup for development than I’ve been used to (the Very Special engineer that I am). It has some advantages over the quick ‘n dirty approach I’ve used, but it has proved to be extremely tricky to set up. So I thought I would share the set of solutions I gathered that make a working setup, in case it helps anyone else. (In advance I should say that these aren’t the über geek ways of doing these things; for that, learn to love the Linux command-line and tip forums. They are stripped down and simplified.) I do mainly design, so I don’t want to spend that much time getting things perfect, just workable. I run Mac OS X Tiger and Leopard. I’ve been doing a lot of one-person development with Textmate, Fetch, and Terminal. If that sounds similar to your speed, this setup might be interesting.

The idea is that you run a local Web server, manage code revisions in a shared repository, and set up a virtual host for each project. This has the advantages of:

  • Giving you a fast, private, local environment to see and test your work
  • Having code that can will work on a public server with no changes
  • Letting you roll back to a previous version before you screwed everything up
  • Collaborating with others or working on more than one machine, effortlessly
  • Use a bloated-but-pretty-good development application with idiot-proof debugging, code hints, docs, variable name lists, and function tracking

It has a disadvantages of:

  • Being complex to maintain
  • Requiring more steps to do simple things
  • Being wrong for small projects

So, anyway…

Eclipse is a coding tool, a completely free Textmate or Coda replacement that has 18,423,521 features. It takes a lot of getting used-to, but in the end I do like it better than simpler tools. Projects can get complex really fast, and it’s much better for that than any tools I’ve used. Plus it has a lot users, plugins, and developers.

1. Eclipse

Eclipse is mainly for Java development, so it needs a plugin to function well for PHP coding. The easiest way to get that is to download a free, pre-configured version of Eclipse 3.3.2 that’s maintained by the PDT project.

  • Download the PDT “all in one” build (Mac DMG file, gzip)
  • Double click on the file to uncompress it, then copy the folder to Applications. Open the ‘Eclipse’ application inside the folder
  • Make a ‘workspace’ folder when the application starts up that is not in a system folder (like Library or Applications). I use Macintosh HD/dev/workspace in all the below steps

2. Subversion

Subversion is a version management tool that lets you go back to previous versions of files and collaborate with others really easily. Happily, Mac OS X ships with it pre-installed.

  • First, set up a repository (where your code will be tracked)
  • Open the Terminal (Applications » Utilities) and enter:
    sudo mkdir /dev/svn
    sudo mkdir /dev/svn/repository
    sudo svnadmin create /dev/svn/repository
    sudo chown -R www:www /dev/svn/repository

  • Then, if you want to get the most out of Subversion, continue following the instructions in the excellent documentation, setting up a trunk and branch for each project. Or, you can just create a new repository for each project like I do, and continue to the next part right now.

The simplest way to use Subversion is an Eclipse plugin called Subclipse. To get it, open Eclipse, then:

  • Go to the menu Help » Software Updates » Find and Install
  • Select “Search for New Features to Install”
  • Click “New Remote Site” and enter a name for the site (e.g. “Subclipse”)
  • Enter http://subclipse.tigris.org/update_1.2.x for the url and click ‘Ok’
  • On the ‘Update Sites to Visit’ screen, make sure the checkbox next to ‘Subclipse’ is checked, then click ‘Next’
  • On the Updates screen, expand the ‘Subclipse’ item and uncheck the ‘Integrations’ item
  • Click next and accept all the following dialog’s defaults
  • Create a new project (File » New » Other… » SVN Folder » Checkout Projects from SVN)
  • Choose ‘Enter a new Repository’ and enter http://localhost/dev/svn/repository/, select the item created, and click ‘Finish’
  • On the next screen, set up the project as PHP by finding the PHP folder, opening it, and choosing PHP Project. Then give it a name, and code away
  • Be sure to read about how to use Subversion, and Eclipse either at their sites or from within Eclipse Help.

3. Apache

Both Mac OS X 10.4 Tiger and 10.5 Leopard come with Apache already installed. Turn it on in the System Preferences » Sharing preference pane. To configure it for this setup:

  • Open the Terminal application (in the Applications->Utilities folder) and:
  • for Tiger enter sudo nano /etc/httpd/httpd.conf, then your password
  • for Leopard enter sudo nano /etc/apache2/httpd.conf then your password
  • Scroll down (cntrl-v) until you find the line:
    # Virtual hosts
    # Include /private/etc/[apache2 or httpd]/extra/httpd-vhosts.conf

    and change it to:
    # Virtual hosts
    Include /private/etc/[apache2 or httpd]/extra/httpd-vhosts.conf
  • Then add a ‘Directory’ block, entering these lines below the lines above (use what ever folder path you made for your workspace in the first line):
    <Directory "/dev/workspace/">
       Order allow,deny
       Allow from all
    </Directory>
  • Next, set up Subversion for Apache. Enter in the same document, after the above lines:
    LoadModule dav_svn_module /usr/libexec/apache2/mod_dav_svn.so
    <Location /dev/svn>
       DAV svn
       SVNParentPath /dev/svn
    </Location>
  • Save the file (cntrl-o) and exit (cntrl-x).

Next, set up a virtual host so that you can type in the url for your domain and see your project almost exactly as on the final server. Open the Terminal application (in the Applications->Utilities folder) and:

  • for Tiger enter sudo nano /etc/httpd/extra/httpd-vhosts.conf, then your password
  • for Leopard enter sudo nano /etc/apache2/extra/httpd-vhosts.conf, then your password
  • Delete all the lines in the file that don’t have a ‘#’ at the beginning
  • Enter, at the end (replace www.domain.com with your server domain):

    NameVirtualHost 127.0.0.1
    <VirtualHost 127.0.0.1>
       DocumentRoot "/dev/workspace/www.domain.com"
       ServerName www.dev.domain.com
    </VirtualHost>

4. php5

On Mac OS X 10.5, you can use the existing PHP5 installation:

  • Open Terminal and enter sudo nano /etc/apache2/httpd.conf
  • Find the line #LoadModule php5_module libexec/apache2/libphp5.so
  • Remove the # and save the file
  • Restart Apache (System Preferences » Sharing).

On Mac OS X 10.4, the existing PHP install doesn’t seem to work reliably. Easiest is just to use Marc Liyanage’s install packages with Apache 1.3 that comes installed:

5. MYSQL

On Mac OS X 10.4:

On Mac OS X 10.5:

  • Download MySQL 5 (dmg)
  • Run Installers for MySQL, the startup item and preference pane
  • Make sure the preference pane (in System Preferences) indicates it’s running
  • Open the Terminal application and enter /usr/local/mysql/bin/mysql -u root
  • Remove a big security hole by adding a MySQL root account password. Enter:
    update mysql.user SET password=PASSWORD('yourpasswordhere');
  • Enter EXIT to leave MySQL

6. phpmyadmin

Phpmyadmin lets you use a Web-based interface to MySQL instead of the command-line. For most tasks, this is drastically slower but far easier than the command-line.

  • Download and uncompress phpmyadmin
  • Copy to /Library/WebServer/Documents and rename the folder “phpmyadmin”
  • Open Terminal and enter sudo cp /private/etc/php.ini.default php.ini
  • Open the php.ini file and change the lines
    mysql.default_socket = and mysqli.default_socket =
    to:
    mysql.default_socket = /private/tmp/mysql.sock
    mysqli.default_socket = /private/tmp/mysql.sock
  • In your Web browser, go to localhost/phpmyadmin/scripts/setup.php and click ‘add’ in server section, then:
    • Leave all defaults as they are, except for…
    • set config user as root
    • set config password as the one you set above in MySQL section
    • Click the ‘Save’ button
  • Go to the folder Macintosh HD/Library/WebServer/phpmyadmin/config/config.inc.php
  • move config.inc.php up one directory level and delete the ‘config’ directory
  • Go to http://localhost/phpmyadmin to use your database

You’re done! Whew. Now, as long as you don’t code any absolute paths, you can develop in Eclipse, save as you go, and see your site in its final form by going to www.dev.domainname.com. When it’s time to deploy, you can move your files as they are to the production server and avoid all the last-minute path-fixing (at the very least).

(Thanks hugely to Jeff Reiecke and Yixu Lin for various pieces of help with this.)

Post a Comment

Your email is never shared. Required fields are marked *

*
*