Tutorial: Installing Apache 2, PHP 5, MySQL 5, and phpMyAdmin on OS X
I struggled a little in figuring out how best to install Apache 2, PHP 5, and MySQL 5 (and phpMyAdmin) on OS X, given that Apache 1.3 and PHP 4 come preinstalled. This tutorial shows the steps that I took to get it all working, and while it may seem very basic to many of you, I figured that it might come in handy for some *NIX novices. Enjoy

.
To the large portion of you who know far more than I do about this stuff: Please let me know if any of these steps are unnecessary or inappropriate!
---
We will be using MacPorts (previously DarwinPorts) to make the Apache 2, PHP 5, and MySQL 5 installations painless and easily upgradable.
1. Install MacPorts:
First, make sure you have Xcode installed. This will not work otherwise.
Note: This tutorial assumes that you want to compile MacPorts from source (and at the time it was written, this was the only way to obtain the latest version). You could
just as easily download a binary of MacPorts (if available) and scroll down to the "port sync" step inside the quoted block below.
Go to
http://svn.macports.org/repository/macports/downloads and download the source for the latest version of MacPorts
Untar the source, and run the following command from within the newly created MacPorts (or DarwinPorts) directory, in order to place a MacPorts installation in /opt/local.
./configure && make && sudo make install
The following is taken from the MacPorts README:
This will place a standard DarwinPorts installation in /opt/local.
You should ensure that /opt/local/bin is in your PATH environment
variable, otherwise your shell will not be able to find the executables
that are installed by DarwinPorts.
If you are using a bourne shell (Panther and Tiger by default uses
bash) add the following line to your ~/.profile file. If the file
does not exist create it.
export PATH=/opt/local/bin:$PATH
If you are using a (t)csh shell (Jaguar) put the following line in
your ~/.cshrc file. If the file does not exist, create it. Your
changes will not take effect until you have opened a new shell.
set path=(/opt/local/bin $path)
Finally, you must download the port description files, the Portfiles:
port sync
If you want to update the Portfiles and the DarwinPorts infrastructure (base) run:
sudo port selfupdate
2.0. Install Apache 2:
To install the latest version of Apache 2, type the following:
sudo port install apache2
When that is done, you have to create a conf file (which you can edit to configure Apache 2):
sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf
2.1. Preserve Old Apache Directories
Please perform the following steps if you wish for your "Sites" directories (i.e.
/Users/user/Sites) to be accessed via
http://your-server/~user, as is the case with Apache 1.3).
Open
/opt/local/apache2/conf/httpd.conf, and change:
DocumentRoot "/opt/local/apache2/htdocs" to
DocumentRoot "/Library/WebServer/Documents"
<Directory "/opt/local/apache2/htdocs"> to
<Directory "/Library/WebServer/Documents">
Then, uncomment this line:
Include conf/extra/httpd-userdir.conf
2.2. Some Apache 2 extras
You may also want to uncomment the following lines in /opt/local/apache2/conf/httpd.conf:
Include conf/extra/httpd-autoindex.conf (Fancy directory listing)
Include conf/extra/httpd-default.conf (Some default settings)
2.3. Make Apache 2 access a little easier
Warning: Remember that OS X has a preinstalled version of Apache (1.3), which can turned on and off via "Personal Web Sharing" in Preferences, or via the "apachectl" command. The command points to
that version of Apache, and not the one we are installing!
To make starting and stopping Apache 2 a little easier, we will construct an alias to Apache 2's apachectl. Add the following line to ~/.profile (Panther/Tiger) and restart the terminal to gain access to it:
alias apache2ctl='sudo /opt/local/apache2/bin/apachectl'
2.4. Start Apache 2 on reboot
Warning: When I perform the command below, I eventually get the following error:
Workaround Bonjour: Unknown error: 0. I've read that this is harmless, but I really don't know for sure. I assume that everything ends up in proper order after a reboot.
To start Apache 2 now and whenever the system reboots, type the following:
sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
2.5. Notes
We added "sudo" to the alias apache2ctl, so if prompted for a password, enter your root password.
To run Apache 2:
apache2ctl start
To stop Apache 2:
apache2ctl stop
---
3.0. Install MySQL 5:
Note: MySQL has a very nice binary installation of MySQL, so feel free to go ahead and use that instead of a port. I, however, like having my entire system (Apache 2, PHP 5, MySQL 5) installed together in the same fashion.
To install MySQL 5, we will first get the port. If you want MySQL 5 to run automatically after system startup, perform the following commands:
Warning: When I perform the launchctl command below, I eventually get the following error:
Workaround Bonjour: Unknown error: 0. I've read that this is harmless, but I really don't know for sure. I assume that everything ends up in proper order after a reboot.
sudo port install mysql5 +server
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
Otherwise, run the following:
sudo port install mysql5
3.1. Make MySQL 5 access a little easier
When done, you may want to add the following lines to
~/.profile (Panther/Tiger) and restart the Terminal to gain access to them:
Warning: You will need to type
sudo -v before using mysqlstart, to avoid being prompted for a password in the background (thus preventing MySQL from starting).
alias mysqlstart='sudo mysqld_safe5 &'
alias mysqlstop='mysqladmin5 -u root -p shutdown' (The "-p" assumes that you will be using a password for root, which you of course should be)
3.2. Set up DB
Then, run the following to set up the database:
sudo -u mysql mysql_install_db5
You will now be able to use MySQL 5 (and you can use mysqlstart and mysqlstop for convenience).
I also recommend immediately adding a password to your root account:
mysqladmin -u root password [yourpw]
---
4.0. Install PHP 5:
(Make sure Apache 2 is stopped before proceeding.)
To install the latest version of PHP 5, type either:
sudo port install php5 +apache2 +mysql5 +pear (With PEAR) or
sudo port install php5 +apache2 +mysql5 (Without PEAR)
When that is done, register PHP 5 with Apache 2:
cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
And create a php.ini file (which you can edit to configure PHP 5):
cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini
Then open /opt/local/apache2/conf/httpd.conf again, and add this line:
Include conf/extras-conf/*.conf
You will probably want to register index.php with Apache 2 as a directory index page (automatically loaded as the directory index). If you would like to do this, also change:
DirectoryIndex index.html to
DirectoryIndex index.html index.php
---
5.0. Install phpMyAdmin:
The port is not updated regularly, and installation is ridiculously easy for phpMyAdmin, so:
Go to
phpMyAdmin > Downloads | MySQL Database Administration Tool | www.phpmyadmin.net and download it.
Next, untar the downloaded file. Rename the directory to "phpMyAdmin" and place it in your document root (
/Library/WebServer/Documents if you have preserved the old Apache directories).
Create a php file called
config.inc.php and place it in the root of the phpMyAdmin folder. This file will overwrite settings defined by
phpMyAdmin/libraries/config.default.php. The following is my
config.inc.php file. Some of the lines are not completely necessary, but I like readability and consistency:
Code:
<?php
$cfg['blowfish_secret'] = '[A passphrase for internal use]';
$i = 0;
$i++;
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['user'] = '';
$cfg['Servers'][$i]['password'] = '';
?>
That's it! You should now be able to access it at
http://your-server/phpMyAdmin. You can log in as root, add more users, create databases, etc.
---
I hope some fellow installation novices find this tutorial helpful

.
Tavi