Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Tutorial: Installing Apache 2, PHP 5, MySQL 5, and phpMyAdmin on OS X

Tutorial: Installing Apache 2, PHP 5, MySQL 5, and phpMyAdmin on OS X
Thread Tools
Mac Elite
Join Date: Jan 2004
Location: Berkeley, CA
Status: Offline
Reply With Quote
Dec 30, 2006, 03:34 PM
 
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
(Last edited by tavilach; Dec 31, 2006 at 01:06 AM )
"Give me a lever long enough and a fulcrum on which to place it, and I shall move the world." -Archimedes
     
Fresh-Faced Recruit
Join Date: Dec 2006
Status: Offline
Reply With Quote
Jan 8, 2007, 07:54 AM
 
Thanks Tavlich. I've tried multiple different solutions for running an apache, SQL, PHP server on an iMac, including ApacheFriends, MAMP and Tenon's iTools. Although they are probably a little easier to install they don't afford the flexibiity that using ports provides, and the install went like a breeze anyway. I'll have to test drive it to make sure that it has all of the options that I currently need, but I believe so.

If I do need to build PHP with other options is that relatively easy to do?

Also, if I want a GUI front end to manage the server what would you recommend. I have used Webmin in the past without too many problems.

p.s. e-mail: sscotti@sscotti.com
     
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Jan 8, 2007, 08:05 AM
 
I don't see why you need Apache2 when the built-in version works fine for most things. As for PHP5 and MySQL, both are available in precompiled .pkg forms (here and here) that take all of 2 minutes to install. That's what I did and it worked great for me.
     
Fresh-Faced Recruit
Join Date: Dec 2006
Status: Offline
Reply With Quote
Jan 8, 2007, 01:31 PM
 
Right. I've tried alot of those, like MAMP, XAMPP, the native OS X apache and the PHP packages from Entropy. Many of them did not support all of the features that I wanted/needed, in particular for Egroupware and some other PHP packages that I have.

Compiling your own affords a little more flexibility in terms of where things are placed and with adding additional features.

After you do it a couple of times (Using Darwin Ports or something like FINK) it is actually pretty easy.

I know that the MySQL package that is Opensource is very easy to install. I haven't tried the Entropy packages recently, I could go back and try that but I'm liking what I have now.
     
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Jan 9, 2007, 07:34 AM
 
Out of curiosity, what can Apache2 do that 1.3 or whatever can't?
     
Mac Elite
Join Date: Mar 2002
Status: Offline
Reply With Quote
Jan 10, 2007, 02:13 AM
 
Panther user here.

I was wondering, what would be the easiest way to set up a local PHP/MYSQL environment? I've stumbled across this link, will that do it, or are there any particular pitfalls I have to look out for?

Just really taking baby steps, the idea is to set up a local SMF 1.1.1 forum that I can practice/experiment with for a month or two, before I get proper paid webhosting.

Thanks.
     
Fresh-Faced Recruit
Join Date: Jan 2007
Status: Offline
Reply With Quote
Jan 17, 2007, 05:07 PM
 
The entropy PHP install didn't work on my 10.4.8 Server. The process goes ok until the very end, and then it gives a 'Failed' error.
     
Fresh-Faced Recruit
Join Date: Jan 2007
Status: Offline
Reply With Quote
Jan 31, 2007, 04:46 PM
 
Thanks, good job. This article provided a good complement to this one I was following
James Duncan Davidson: Sandboxing Rails With MacPorts
especially cause i still needed php for my other apps
     
Fresh-Faced Recruit
Join Date: Feb 2007
Status: Offline
Reply With Quote
Feb 6, 2007, 04:32 PM
 
Hi,
first of all thanks for a great tutorial.
I already have mysql installed from the official distribution, and i just can't seem to get php5 to compile with mysql support, do I have to install the darwinports version or is there a way around?

thanks
     
Junior Member
Join Date: Mar 2005
Status: Offline
Reply With Quote
Feb 7, 2007, 06:05 AM
 
Great tutorial...I always found web development easier on the PC.
The Mac has come a long way on the web development side.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Feb 7, 2007, 04:18 PM
 
Hey Tavlich:

I used to be the port maintainer for phpMyAdmin, but I no longer have time to continue. Good to see MacPorts being used for this sort of thing though!

Have you ever experimented with having the Macports components co-exist with Apple's components so that you could use Apple's GUI to start/stop the server, and the OS X Server Server Admin GUI to configure Apache?
     
Fresh-Faced Recruit
Join Date: Feb 2007
Status: Offline
Reply With Quote
Feb 8, 2007, 04:21 PM
 
Feb 08, 2007
imac g5 tiger os4.8 2gb ram

Running mysql, php, zend ide for four years and never reinstalled them in less than a week.

Apple support had to initialize my hard drive after the system crashed, and I have spent the last 11 days trying to get back up and running. I have four external drives with 250gigs each from which I have tried to reconstruct the procedures used to reinstall my system which is mysql, php and the zend ide.
Most of my experience is MS and LINUX, I've used this imac for about a year.

I use jedit, bbedit, speed download and superduper every day for production and development.
I can and do work with the /usr directories.

I want to start with the apple install disk 1, then 2 installing every thing available. I then want to install mysql, php and the zend ide, backup the system as bootable so that I can avoid the mess I get into every time I screw up.

Right now I get nothing from entering "port sync" in step 1.0 of the tutorial, and I also tried "sudo port selfupdate" , both in /root and ~/home.
It always replies "bash: port: command not found".

What directory or folder do I place
"DarwinPorts-1.3.2(2).tar.gz",
"mysql-max-4.1.13-apple-darwin8.1.0-powerpc.dmg",
"Entropy-PHP-5.0.4-apache2-1.dmg",
"CompleteApache2-2.0.52.dmg.gz" and eventually
"ZendStudio-4_0_2.sit".

When I enter "sudo port install apache2" I assume it should be
"sudo port install CompleteApache2-2.0.52.dmg.gz" , is this correct?

Here's hoping some one can hold my hand on the simple and/or obvious.
Thanks

Dander
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Feb 13, 2007, 07:21 PM
 
Originally Posted by wataru View Post
Out of curiosity, what can Apache2 do that 1.3 or whatever can't?

Apache2 includes the ability to change its processing model to change how it handles requests. These are called MPMs. Unfortunately, no MPMs other than the default are terribly stable or secure, and the default is identical to the model used in Apache 1.3.x.

Apache 2 includes mod_dav and mod_ssl, and a bunch of improvements when run on Windows. It's what I recommend new installs use, but there is no absolutely huge reason to upgrade to it if you are happily running 1.3 with mod_dav and mod_ssl.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Feb 13, 2007, 07:23 PM
 
Originally Posted by dander View Post
Feb 08, 2007
imac g5 tiger os4.8 2gb ram

Running mysql, php, zend ide for four years and never reinstalled them in less than a week.

Apple support had to initialize my hard drive after the system crashed, and I have spent the last 11 days trying to get back up and running. I have four external drives with 250gigs each from which I have tried to reconstruct the procedures used to reinstall my system which is mysql, php and the zend ide.
Most of my experience is MS and LINUX, I've used this imac for about a year.

I use jedit, bbedit, speed download and superduper every day for production and development.
I can and do work with the /usr directories.

I want to start with the apple install disk 1, then 2 installing every thing available. I then want to install mysql, php and the zend ide, backup the system as bootable so that I can avoid the mess I get into every time I screw up.

Right now I get nothing from entering "port sync" in step 1.0 of the tutorial, and I also tried "sudo port selfupdate" , both in /root and ~/home.
It always replies "bash: port: command not found".

What directory or folder do I place
"DarwinPorts-1.3.2(2).tar.gz",
"mysql-max-4.1.13-apple-darwin8.1.0-powerpc.dmg",
"Entropy-PHP-5.0.4-apache2-1.dmg",
"CompleteApache2-2.0.52.dmg.gz" and eventually
"ZendStudio-4_0_2.sit".

When I enter "sudo port install apache2" I assume it should be
"sudo port install CompleteApache2-2.0.52.dmg.gz" , is this correct?

Here's hoping some one can hold my hand on the simple and/or obvious.
Thanks

Dander

MacPorts is simply a disk image install, you don't have to manually place any files anywhere to install it. If you haven't installed it, this is why the port command is not found for you.

All of the other binary installers are also not necessary if you use MacPorts, as the whole purpose of MacPorts is to automatically download and install these pieces for you.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Feb 13, 2007, 07:25 PM
 
Originally Posted by webmonkie View Post
Great tutorial...I always found web development easier on the PC.
The Mac has come a long way on the web development side.
Do you mean editing HTML, or in running a web server? If the former, there are a number of really useful things you can do in the Unix environment that work well when speaking to a Unix web server.
     
Mac Elite
Join Date: Jan 2001
Status: Online
Reply With Quote
Mar 27, 2007, 10:37 PM
 
About that warning you get after using launchd to have apache2 start up at boot - I have tried 3 times now, and when I do that, the system runs fine for a while, then every app rainbows out and I have to force a reboot into single user mode and remove the plist file to make it all work again.

Anyone have an idea what is wrong with the launchd plist file for apache that is causing the system to crash?

Thanks!
     
Senior User
Join Date: Mar 2007
Location: New York
Status: Offline
Reply With Quote
Mar 28, 2007, 02:15 PM
 
Maybe apache2 is spawning too many copies? Open Activity Monitor and see what's running when your system starts to hang.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Mar 28, 2007, 02:41 PM
 
Any log entries?