Setting up php and mysql (and phpbb) on Mac OS X 10.2

by Marion Bates <mbates at whoopis.com>

PHP:

1. Enable PHP. Current versions of Mac OS X v. 10.2 ("Jaguar") come with php installed, but it is not enabled by default. Edit (backup first) /etc/httpd/httpd.conf. Find and uncomment (remove # mark) these lines:

LoadModule php4_module libexec/httpd/libphp4.so
AddModule mod_php4.c
and add these three lines...
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.php index.shtml index.html
...to the section marked "<Directory />", so that it looks like this:
<Directory />
    Options FollowSymLinks
    AllowOverride None
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    DirectoryIndex index.php index.shtml index.html
</Directory>
Now restart the webserver, by going to Preferences->Sharing and unchecking, then rechecking the Personal Web Sharing box.

To test, create a file containing:

<?php

	phpinfo();

?>
and save that file as something.php. Then load it in a web browser. You should see a big page of configuration information about php, with tables and colors and the php logo. If you don't, then something's wrong -- check the syntax in httpd.conf, restart Apache again, run Software Update and reboot if necessary, and try again.

2. Add the key php libraries. The basic functionality of php is now enabled on your system. However, there are a multitude of common libraries with functions you will need, which are present but are not yet accessible because they're not in php's "includes" path. Furthermore, I can't find where that path is specified on the stock OS X installation; it appears that maybe /usr/lib/php/build/acinclude.m4 is the file to edit, but I don't know enough about how everything works to feel comfortable messing with that. So, options are: reinstall php from scratch using some distro that works, figure out how to modify the includes path in all your scripts, or move/copy the php libraries from their weird location (/System/Library/PHP) to the expected location (/usr/lib/php). I chose the latter for simplicity's sake. You will need to be an Administrator (or root) to do this.

sudo cp -R /System/Library/PHP/* /usr/lib/php
(enter password)
When you're finished, go to the php directory and do an ls (list). It should look like this:
mbates@dhcp127 PHP$ cd /usr/lib/php/ 
mbates@dhcp127 php$ ls
Archive    Crypt      HTML       Log.php    Numbers    System.php
Benchmark  DB         HTTP       Mail       PEAR       XML
Cache      DB.php     HTTP.php   Mail.php   PEAR.php   build
Cache.php  Date       Image      Math       Payment    extensions
Console    File       Log        Net        Schedule
mbates@dhcp127 php$ 
Now your php scripts will have access to the DB (DataBase) functions, among other things. That's the stuff we really need in order to do anything with php and mysql.

Note: Potential pitfall here -- if Apple releases an OS X update which affects the stock PHP stuff, then it will only patch the files under the default path (/System/Library/PHP) whereas you'll actually be using old copies of those files in the /usr/lib/php directory. So, either make sure you re-copy those files after you install relevant system updates, or figure out how to do something clever with hard links (OS X 10.0 didn't support them, maybe Jaguar does?), or do something else that works...and please email me about it so I can update this howto. :)

MYSQL:

1. Download and install mysql. Go to the mysql downloads page, then go to the link for whatever's listed as the "Production Release" from the "MySQL database server & standard clients" list. (At the time of this writing, that's version 4.0.) Then scroll down to the "Mac OS X Package Installer downloads" section and pick "Standard."

When it's finished downloading, unpack and mount the disk image. Before you run the installer, open up the readme and skim it -- make sure you get the idea of what it all means. Here's a condensed, un-explained, assumes-this-is-the-first-time-you've-installed-mysql version of what it says to do after the installer is finished:

Open Terminal, edit your PATH. For bash users:

vi ~/.profile
OR
sudo vi /etc/profile
and change the PATH line to something like:
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin
Make sure "/usr/local/mysql/bin" is in there somewhere. Save and exit, then logout and log back in.

2. Start the mysql server (mysqld). To run the server:

cd /usr/local/mysql
(return)
sudo ./bin/mysqld_safe &
(Note: If you want MySQL to start up automatically on boot, you can download Marc Liyanage's MySQL StartupItem from here.)

3. Set the mysql root password. Say the password you want to use is "goober". You would type these two commands, verbatim:

mysqladmin -u root password goober
(return)
mysqladmin -u root -h `hostname` password goober
(return)
4. Test. To test from the command line, type:
mysql -u root -p
(hit return)
(enter mysql root password)
You should then see a mysql prompt. You can SHOW DATABASES; CREATE DATABASE whatever; etc. -- all the fun sql stuff. Or just ctrl-d to exit.

At this point, you can start cranking out php scripts to communicate with mysql databases via web forms or whatever. For a quickstart on that, take a look at this tutorial.

phpBB2:

1. Download and unpack phpBB2. Go to the phpBB downloads page and select the gzipped version of the "Full Package" from the download list. When it's finished, do NOT let Stuffit Expander unpack it! (Don't double-click it either. If Stuffit automatically unpacks it, delete the folder it creates and leave the archive untouched.) Now, open a Terminal, cd to the directory containing the downloaded phpbb tarball, and type:

tar -xzf phpBB-2.0.5.tar.gz
This should create a directory called "phpBB2". This contains all the code for the bulletin board. Move this directory to your webroot:
sudo mv phpBB2 /Library/WebServer/Documents/
Optionally, you can rename the directory to something friendlier like "forums" -- keep in mind that whatever you name the directory will be its URL. In other words, if you rename the directory "forums", then you will access it by typing "http://www.yourdomain.com/forums/". 2. Create initial database. In the Terminal, type:
mysql -u root -p
(return)
(enter password)
At the mysql prompt, type:
create database forumdb;
(return)
Don't forget the semicolon. It should say "Query OK, 0 rows affected (0.01 seconds)" or something very similar. If you get an error, try again -- check syntax, make sure there's no spaces where they shouldn't be, etc.)

To check that the database was created, type:

show databases;
You should see three databases: "mysql", "test", and "forumdb". Type ctrl-d to exit mysql.

3. Do phpBB installation. Open a web browser and go to http://www.yourdomain.com/forums/ (or whatever you renamed the phpBB2 directory). You should get the phpBB "Install" page. If you don't, something is broken -- either your php setup is uncool (you'll need those key libraries, make sure they're all under /usr/lib/php), or the phpBB2 files themselves are somehow fouled up (did you unpack them with Stuffit Expander instead of command-line tar? Bad idea.) Try again, and make sure to force-reload the page and/or clear your browser's cache.

For the actual phpBB installation details, refer to their excellent documentation at http://www.phpbb.com. Remember to select the right version of mysql ("4.x") and specify the right login info so that phpBB can talk to your "forumdb" database.

Note: It would be a Good Idea, security-wise, to create a non-root mysql user for phpBB to use, rather than using the root account. For some info on that, consult the mysql documentation, or for a quick-and-dirty solution, see this post.


References: