Web Developer / Blog

December
9th, 2008

Developing web applications locally on your Mac OSX using MacPorts

Digg this article · Save to del.icio.us · Stumble it!

I’ve long been a proponent of developing web applications locally on my laptop. It’s amazing how much time you can save when you don’t have to transfer files to a development server or wait for a file to save over the network. You can also do development when you’re not at the office, vpn’ed or even have an internet connection. However, the most compelling reason to do it is because it’s relatively trivial.

Currently, I use a Mac so I’ll go over how to set everything up using MacPorts. However, there are many packaged applications that let you get up and running with a web server, scripting language and database.

Installing MacPorts
MacPorts is great. It reminds me a lot of Gentoo’s portage which I love. To get started you’ll need to download the appropriate version of MacPorts here. Make sure you have XCode installed, else follow the instructions to install it. You can tell if you have XCode installed by doing the following in your terminal. You should se a similar output if XCode is installed.

jmathai@[~]: ls /Developer
About Xcode Tools.pdf Documentation         Extras                Icon?                 Makefiles             SDKs                  usr
Applications          Examples              Headers               Library               Private               Tools

Once you get MacPorts and XCode installed you can begin getting familiar with the port command. MacPorts has a subsystem you can use to quickly familiarize yourself with the options available to you. To enter the subsystem enter the following in your terminal.

Getting started with MacPorts
You’ll want to modify your executable path so you can easily access binaries installed by MacPorts. You’ll want to add export PATH=/opt/local/bin:/opt/local/sbin:$PATH somewhere in ~/.bashrc or ~/.bash_profile. Otherwise you’ll receive errors in the form -bash: port: command not found.

jmathai@[~]: which port
/opt/local/bin/port
jmathai@[~]: sudo port
MacPorts 1.600
Entering interactive mode... ("help" for help, "quit" to quit)
[Users/jmathai] >

Now you’re in the MacPort subsystem and can begin issuing commands.

[Users/jmathai] > help
...
Supported commands
------------------
activate, archive, build, cat, cd, checksum, clean, compact, configure,
contents, deactivate, dependents, deps, destroot, dir, distcheck, dmg,
dpkg, echo, ed, edit, exit, extract, fetch, file, gohome, help, info,
install, installed, lint, list, livecheck, location, mdmg, mirror, mpkg,
outdated, patch, pkg, provides, quit, rpm, search, selfupdate, srpm,
submit, sync, test, trace, unarchive, uncompact, uninstall, upgrade,
url, usage, variants, version, work
...

One of the first things you’ll want to do is to make sure MacPorts is up to date. Once you do that then go ahead and search for some common packages that you might want to install. For this example we’ll go with the traditional LAMP stack including Apache, PHP, MySql…excluding Linux of Course. We can search for apache, php and mysql.

[Users/jmathai] > selfupdate
MacPorts base version 1.600 installed
Downloaded MacPorts base version 1.600
The MacPorts installation is not outdated and so was not updated
selfupdate done!
[Users/jmathai] > search php5
...
php5                           www/php5       5.2.6        PHP: Hypertext Preprocessor
...
[Users/jmathai] > search apache2
...
apache2                        www/apache2    2.2.10       The extremely popular second version of the Apache http server
...
[Users/jmathai] > search mysql5
...
mysql5                         databases/mysql5 5.0.67       Multithreaded SQL database server
...
[Users/jmathai] >

Start installing packages with MacPorts
Ports have the notion of variants which is beyond the scope of this post but we’ll touch on the topic. To install php you can specify that you want it built with apache and myself. You can additionally specify that you want pear installed as well. We’ll install php5 with support for apache2 and mysql5. MacPorts will take care of the dependencies and install all the required packages.

[Users/jmathai] > install php5 +apache2 +mysql5

MacPorts downloads source tarballs and compiles them. This can take a long time, sometimes REALLY long. Be patient.

Understanding where MacPorts installs packages
MacPorts installs packages into /opt/local. For example, you’ll find your php.ini in /opt/local/etc/php.ini and your apache2 files in /opt/local/apache. Let’s verify that everything was installed successfully. Get back to your normal terminal prompt.

[Users/jmathai] > exit
Goodbye
jmathai@[~]: sudo /opt/local/apache2/bin/apachectl restart
jmathai@[~]: echo '<?php phpinfo(); ?>' | sudo tee /opt/local/apache2/htdocs/phpinfo.php
<?php phpinfo(); ?>
jmathai@[~]: open -a Safari "http://localhost/phpinfo.php"

Viola!
There’s plenty of information available on configuring apache and mysql. If you have any specific questions then post them in the comments and I’ll do my best to answer them.

Additional reading

One Response to “Developing web applications locally on your Mac OSX using MacPorts”

  1. John McFarlane Says:

    Very nice.

Leave a Reply


About this site:
This is my (Jaisen Mathai) personal site for potential employers who want to see my resume or portfolio. My ideal job would be to work as a Web developer on a large scale consumer website. My experience is in using PHP, MySQL, Ajax and JSON. I really enjoy creative brainstorming...taking a problem apart and narrowing 100 solutions down to the best one.

Thanks for stopping by. Be sure to drop me a line.