Tuesday, June 10, 2008

PHP on the Mac

If you have a Mac with Leopard and want to use PHP, most of what you need is already installed. It's just a matter of making it work.

First you need to turn on apache. Just go to your "System Preferences", "Sharing" and turn on "Web Sharing". That's it, apache is running. You can check if all is ok by going to http://localhost.

You'll probably want MySql too. Just go to their site, download and install. No big fuss, but make sure you install the StartupItem if you want MySql to start when the system boots.

Next you must enable PHP. Launch a terminal and use your favorite text editor to edit /etc/apache2/httpd.conf. Look for LoadModule php5_module and uncomment the line. Restart apache with sudo apachectl graceful.

Now let's say your username is john, and you have on your home folder a ~/dev/phpproj where your php code is. One way to make this available (and there's lots of others) is to edit /etc/apache2/users/john.conf and add the following to the bottom of the file (replace [ with "lesser than" and ] with "greater than". Blogger doesn't like those symbols):
Alias /phpproj/ "/Users/john/dev/phpproj/"
[Directory "/Users/john/dev/phpproj/"]
Options Indexes MultiViews
Order allow,deny
Allow from all
[/Directory]
Now you can access http://localhost/phpproj/ to view your app.

At this point you might get a MySql error stating that you /var/mysql/mysql.sock does not exist, just go back to your terminal window and type:

cd /var
sudo mkdir mysql
cd mysql
sudo ln -s /private/tmp/mysql.sock mysql.sock

That's it!
Post a Comment