June 19, 2009

Setting up Mac for the Web Developer

Article about ▸

written by Marek Foss

Developer Space

Leopard comes with tons of stuff for developers, but out-of-the-box it ain’t quite configured to get them running in an instant. This article goes through the configuration process of Leopard and some of its bundled applications, to get the most out of it and create a nice web development environment. I will focus on setting up Apache with MySQL and Perl (and PHP, which I rarely use) plus some recommended apps. If you are a python developer, please share your configuration routine in the comments.

First, you should download Xcode — it includes some Unix tools necessary for compiling utilities from source code. We will need that while setting up Perl. Then, let’s start configuring stuff. We will use everything Leopard has: the built-in Apache 2 server, the PHP interpreter and the Perl interpreter. There is also the Python interpreter, but since I don’t code in this language, I didn’t feel the need to get it running — if you are Python coder and would like to share your configuration routine, please do so in the comments.

Apache

What we want to achieve is having a server run with the /Sites folder, located in your user directory, as root. Also, we will restrict access to just your machine, so no one will peak on your work.

Start with finding and opening for edit the Apache configuration file — open Terminal, type cd .. several times to get to the root of your filesystem, then type:

cd /etc/apache2
sudo vi httpd.conf

sudo command will open the configuration file to edit in Vi with admin privileges, that’s why you need to type your password (and yes, the characters doesn’t appear on screen, but they are invisibly typed). Next, hit A key, which triggers the edit mode. Move down using keyboard arrows until you find the lines that resemble the following:

#LoadModule php5_module		libexec/apache2/libphp5.so
#LoadModule fastcgi_module	libexec/apache2/mod_fastcgi.so

Remove the # sign to enable these two libraries. And move on to the next line:

DocumentRoot “/Users/name/Sites”

When you locate the DocumentRoot, it will point to a location somewhere in the Library. Change it as shown above, substituting the name with your user directory name. And move on to the second definition of Directory:

# This should be set to whatever DocumentRoot is set to
<Directory “/Users/name/Sites/*”>

Locate the Options and make sure you add ExecCGI to them (each option is separated with a space). At the end of the Directory tag you should see some permission settings (Allow, Deny etc.) — change them to the following settings to set access from localhost only:

Order allow,deny
Allow from localhost

One note about this configuration: all directories inside /Sites folder are invisible with this setting, but regular files located in /Sites folder are visible — they are still listed when browsing to your machine’s IP address — thus I recommend having just folders there. Finally, lets prepare Apache for handling script files of various extensions, like .php, .cgi, .pl — locate the following tags and change accordingly:

#AddHandler cgi-script .cgi .pl

After adding the necessary extensions, unquote the above statement by removing the # sign. Next, configure how Apache deals with index pages — locate the dir_module tag and add index types to your liking:

<IfModule dir_module>
	DirectoryIndex index.html index.php index.cgi index.pl
</IfModule>

This will tell Apache to handle HTML, PHP and Perl index pages like we are used to. Moreover, the order in which you define these file-types tells the server what to do when there are multiple index files with different extension — which to treat as the current index.

Finally, save the changes and quit Vi — hit Escape key to finish edit mode, then type :wq and hit Enter to save the file and quit. That’s all about Apache, now lets move to MySQL and PHP.

MySQL

Go to the MySQL website and download the Community Server — be sure you pick the 64-bit version if on Snow Leopard 10.6, and the 32-bit version if on Leopard 10.5, otherwise you will run into compatibility problems between database and programming language sockets. There’s a nice binary installer for the Mac OS X, so you shouldn’t have much problems installing it.

Next, let’s tell our system where the MySQL is installed, so we can use all its goodies from any place. Lanuch Terminal again and from your user directory (usually it’s the one that Terminal launches with) type:

sudo vi .bash_profile

We are again using Vi, so hit A for edit mode, then type the following command:

export PATH=/usr/local/mysql/bin:$PATH

Vi may inform you that this file is read-only — to override it and save the file, just hit Escape to exit edit mode, then type :wq! and hit Enter — this will force Vi to do what you want :) You probably should restart Finder now, by selecting Force Quit from the Apple menu.

Lets secure your MySQL installation now by opening Terminal and typing the following command:

mysqladmin -u root password 'your-password'

Substitute your-password with a string you want to use as the password to the database. Finally, in the System Preferences, you should see the MySQL Preference Pane and its state. By now, it’s probably stopped — you can start it now, and if you wish to have it starting up every time you launch the system, you need to install the Startup Item Package from the installation disk — the checkbox in the preferences doesn’t work without it.

The connection details to your newly installed database that you could use while programming web services, are as follows:

HOST: localhost
DBUSER: root
DBPASS: your-password

PHP

To point the PHP interpreter to the correct MySQL location, we need to make on small change in the PHP configuration file. Like before, launch Terminal, do some cd .. to go to absolute root, then type:

cd /private/etc
sudo vi php.ini

It may happen that with clear installation you don’t have the php.ini file. Instead, there is the php.ini.default that you can copy using the following command:

sudo cp php.ini.default php.ini

When you finally open php.ini using Vi, locate these two lines and set the paths as shown:

mysql.default_socket = /private/tmp/mysql.sock
mysqli.default_socket = /private/tmp/mysql.sock

Restarting Apache will reload PHP configuration. To do that, go to System Preferences - Sharing where you need to uncheck and then check again the Web Sharing service.

Perl

Perl interpreter comes with Leopard, but it’s missing the DBD bundle responsible for connecting with databases, including MySQL. Luckily, there is CPAN, the huge and very automated library of Perl modules. Installing the bundle should be no harder than opening Terminal and typing in:

cpan
install DBD::mysql

You might need to configure CPAN first by answering some questions it asks you, but you should be fine by just answering the default answers (always given in the parenthesis, next to the question). And if the MySQL bundle installation fails the test, you could try starting it with force install command. One way or another, you should have by now the ability to code in Perl and use MySQL.

Useful apps

phpMyAdmin — the interface for MySQL management, very useful
Komodo Edit — the best and free IDE for Mac, in my opinion
Transmit — the best file transfer client for Mac, unfortunately not free
Paparazzi — small and free app for Mac for making full-page screenshots of websites
VirtualBox — the best and free virtualization environment to run Windows and Linux inside your Mac OS

With the configurations made and these apps on board, you are ready to go on and code, test and deploy web sites and services straight on your Mac.

Photo by Major Nelson


If you liked the article, please spread the word and share it!

  • Share

Comments


Pozycjonowanie writes:

Macs are way to limiting for my tastes. I’d rather have less trendy windows machine that works better. But I guess if you already have a mac this might be pretty useful.


Paznokcie writes:

So tell me, honestly: is Mac better than a PC ?  I know it is “cooler”, but from a clearly practical point of wiev, is it better ? Or is it comparable, only slightly different ?


Noclegi Gory Stolowe writes:

Surprisingly I had the most problems with setting up MySQL. It worked great on my windows machine yet I spent endless hours trying to figure it out on mac. Still don’t know where I went wrong.


Mr. McMacUser writes:

I’ve done quite a bit of web development with MySQL, Apache and PHP. Setting up a server was easiest on Mac. The only issue I ever had was with using the /~username/ naming scheme. MOD_rewrite fails, but is easily fixed by a simple httpd.conf edit. Apache and PHP are preinstalled, and MySQL is a simple .pkg install away.



Leave a comment: (comments may not appear immediately due to page caching)

Name: Email: (not disclosed)

WWW: Remember my details

Notify me of follow-up comments

Feed me:

to feed
  • Subscribe and get the new articles every now and then directly in your reader — I recommend using Google Reader

Facebook:

Connect:

 by Google
Google FriendConnect appears to be down at the moment. Sorry for inconvenience.
Related Posts with Thumbnails