Tutorial: Installing Apache 2, PHP 5, MySQL 5, and phpMyAdmin on OS X
Did this on my Intel Mac as well as my dual G5 at work running OS X Tiger
I recommend using MacPorts. If have done this from source distributions several times in the past, this is way nicer.
1. Install MacPorts:
First, make sure you have Xcode installed. This will not work otherwise.
Lets compile MacPorts from source.
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 directory. If you downloaded to your desktop then.
$ cd ~/Desktop/MacPorts-1.6.0
$ ./configure
$ make
$ sudo make install
This will place a standard MacPorts 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 MacPorts.
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.
Finally, you must download the port description files, the Portfiles:
port sync
sudo port selfupdate
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
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)
Include conf/extra/httpd-vhosts.conf (Setup for vhosts)
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 ~/.bash_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: 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.