ChipmunkNinja
Ninjas are deadly. Chipmunk Ninjas are just weird.
About this blog
Marc Travels
Marc on Twitter
JustLooking on Twitter

Marc Wandschneider is a professional software developer with well over fifteen years of industry experience (yes, he really is that old). He travels the globe working on interesting projects and gives talks at conferences and trade shows whenever possible.

My Publications:

My book, "Core Web Application Programming with PHP and MySQL" is now available everywhere, including Amazon.com

My "PHP and MySQL LiveLessons" DVD Series has just been published by Prentice-Hall, and can be purchased on Amazon, through Informit, or Safari


ABCHKMPRaRoSTVW
xxxxx-xxxxxxxxx
Jun 17, 2005 | 17:33:17
Installing PHP, MySQL, and Apache on Mac OS X
By marcwan

Many Mac OS X users seem to be having problems getting AMP (Apache, MySQL, and PHP) working properly on various versions of the operating system. In this article, I will describe the process by which I did this on my Powerbook which started out running Panther (10.3) and then Tiger (10.4) (the instructions are the same for both OSes).

Before we begin, we need to talk about the vehicle via which we will do most of our work: Terminal. Since OS X is effectively Unix (It is in fact a modern port and update of the NeXTStep operating system from the late 1980s and early 1990s), you can use command shells to work your way around the system and execute commands. You should definitely make a point of becoming somewhat familiar with this before beginning working with AMP on your computer.

Terminal is found under /Applications/Utilities/Terminal.app. You can launch it from the finder, and you will see something like the following:

Terminal.app

While a full treatment of how to use Terminal and bash are beyond the scope of this article, here are some basic commands with which you should be comfortable:

  Yukimura:~ marcwan$  <strong>mkdir src</strong>
  Yukimura:~ marcwan$  <strong>ls</strong>
Desktop         Library         Pictures        src
Documents       Movies          Sites           striptags1.txt
Download        Music           phpwa           work
  Yukimura:~ marcwan$  <strong>ls -alF</strong>
total 379816
drwxr-xr-x   28 marcwan  marcwan    952 Jun 16 15:46 ./
drwxrwxr-t    6 root   admin        204 May 29 12:22 ../
-rw-r-----    1 marcwan  marcwan  12292 Jun 16 19:34 .DS_Store
drwx------   20 marcwan  marcwan    680 Jun 15 14:56 .Trash/
-rw-------    1 marcwan  marcwan   6238 Jun 15 23:50 .bash_history
-rw-r-----    1 marcwan  marcwan     91 Jun  6 15:44 .bash_profile
-rw-------    1 marcwan  marcwan     51 Jun 16 14:34 .cvspass
-rw-r-----    1 marcwan  marcwan    687 May  8 18:10 .emacs
drwxr-x---    4 marcwan  marcwan    136 May  7 13:41 .emacs.d/
-rw-------    1 marcwan  marcwan    762 Jun 16 11:45 .mysql_history
drwx------    3 marcwan  marcwan    102 May 21 19:38 .ssh/
-rw-------    1 marcwan  marcwan   3896 Jun  6 15:44 .viminfo
drwx------    4 marcwan  marcwan    136 Jun 16 15:46 Desktop/
drwx------   11 marcwan  marcwan    374 Jun 15 23:12 Documents/
drwxr-x---   21 marcwan  marcwan    714 Jun 16 20:04 Download/
drwx------   35 marcwan  marcwan   1190 Jun  5 15:12 Library/
drwx------    5 marcwan  marcwan    170 Jun 13 17:22 Movies/
drwx------    4 marcwan  marcwan    136 May 21 20:05 Music/
drwx------   77 marcwan  marcwan   2618 Jun 16 15:46 Pictures/
drwxr-xr-x   11 marcwan  marcwan    374 Jun 13 17:31 Sites/
drwxr-xr-x   11 marcwan  marcwan    374 Jun 15 14:58 phpwa/
drwxr-xr-x   14 marcwan  marcwan    476 Jun 16 14:37 src/
-rw-r--r--    1 marcwan  marcwan  10256 Jun 16 14:19 striptags1.txt
drwxr-x---    4 marcwan  marcwan    136 Jun 16 20:05 work/
  Yukimura:~ marcwan$  <strong>cd src</strong>
  Yukimura:~ marcwan$  <strong>ls</strong>
  Yukimura:~ marcwan$  <strong>ls -alF</strong>
total 0
drwxr-xr-x   2 marcwan  marcwan  68 Jun 16 20:30 ./
drwxr-xr-x  29 marcwan  marcwan 986 Jun 16 20:30 ../

And so on … You should be proficient at moving around directories in Terminal, and also with running various commands.

To get AMP up and running, you will need the following software:
  • The latest version of XCode from the Apple developer website, developer.apple.com. This containers all of the compilers and header files and what not that you actually need to build the software. The base OS X operating system does not ship with the necessary compilers and headers.
  • MySQL 4.1.xx for Mac OS X. The latest version right now is 4.1.12a, and can be obtained from the MySQL Downloads Page. I just downloaded the installer package for Mac OS X 10.3, which worked perfectly fine on both Panther and Tiger (10.4). Please note that I did have troubles with some other builds, most notable the 5.0.x versions of MySQL. Your mileage may vary.
  • Libxml2. This will be needed to get some of the XML features in PHP working (very much worth having).
  • Optional Apache 1.3.33, which can can be obtained form the Apache HTTP Server Download pages.

The last of these software packages is optional because Mac OS X actually ships with a working copy of the 1.3 series of the Apache HTTP Server already! On Panther this is 1.3.29 while on Tiger it’s 1.3.33. If you are planning on using the system mostly for development and not for a live web server, then there is no reason not to use the included version of Apache. It’s so convenient.

We will assume you have downloaded these software packages to the Download folder in your home directory, and will begin by opening up a Terminal window.


Last login: Thu Jun 16 14:05:18 on ttyp2
Welcome to Darwin!
Yukimura:~ marcwan$

We will show the installation procedures for MySQL and PHP now, and show the optional instructions for Apache last, if you choose to use them.

Installing MySQL on Mac OS X

Because MySQL ships as an installer package, you actually can still use the Finder to install it. Go to the Download folder, and double click on the mysql-standard-4.1.12-apple-darwin6.8-powerpc.dmg icon (phew!). A New Finder window will open with the icons for the installer, a startup-item installer, and a README. Double Click on the mysql-standard-4.1.12-apple-darwin6.8-powerpc.pkg icon, and you will see something like the following:

Installing MySQL

Just follow the wizard through the rest of the steps for installation—it creates all of the appropriate accounts and installs the software in /usr/local/mysql (in fact it installs it in /usr/local/mysql-standard-4.1.12-apple-darwin6.8-powerpc and creates a symbolic link to /usr/local/mysql). Once it is done, you can execute your first command in the Terminal window to start the server:

Yukimura:~ marcwan$ <strong>sudo /usr/local/mysql/bin/safe_mysqld -user=mysql &</strong>

Because you are using the sudo command to execute things with super-user privileges, you will be asked for your password. The MySQL server will now be running and operational (w000t). To verify the operation, you should be able to execute:


Yukimura:~ marcwan$ <strong>/usr/local/mysql/bin/mysql -u root </strong>
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 734 to server version: 4.1.12-standard

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Congratulations, MySQL is now up and running. We’ll now move on to PHP 5.

Installing PHP5 on Mac OS X

To get PHP running, we will compile it (this will give us the most flexibility and let us choose the modules and options we want to use). Before we can do this, however, we will build and install the libxml2 package which enables all of the cool XML features in PHP 5.


Yukimura:~ marcwan$ <strong>cd ~/work</strong>
Yukimura:~ marcwan$ <strong>tar xfz ~/Download/libxml2-2.6.19.tar.gz</strong>
Yukimura:~ marcwan$ <strong>cd libxml2-2.6.19</strong>
Yukimura:~ marcwan$ <strong>./configure --prefix=/usr/local</strong>
Yukimura:~ marcwan$ <strong>make</strong>
Yukimura:~ marcwan$ <strong>sudo make install</strong>
Password: ******

Now we’re ready to build PHP5. We will unpack, configure, build, and install it. The only trick is that we will have it install everything into /usr/local/php5 so as to not interfere with any other software on the computer.

First step, unpacking:


Yukimura:~ marcwan$ <strong>cd ~/work</strong>
Yukimura:~ marcwan$ <strong>tar xfj ~/Download/php-5.04.tar.bz2</strong>
Yukimura:~ marcwan$ <strong>cd php-5.0.4</strong>

Second step, configuring. We need to give it a few paths, most notably the location of the Apache dynamic module loader (apxs) as well as tell it which modules we want. I usually build with multi-byte support (mbstring), SOAP (soap), and mysqli (a newer, improved version of the mysql module).

Yukimura:~ marcwan$ <strong>./configure --prefix=/usr/local/php5 \
--with-apxs=/usr/sbin/apxs --with-libxml=/usr/local \
--with-mbstring --with-mbregex --enable-soap \
--with-mysqli=/usr/local/mysql/bin/mysql_config</strong>

If you want to use the older mysql extension instead of MySQLi, this is how it’s done:

Yukimura:~ marcwan$ <strong>./configure --prefix=/usr/local/php5 \
--with-apxs=/usr/sbin/apxs --with-libxml=/usr/local \
--with-mbstring --with-mbregex --enable-soap \
--with-mysql=/usr/local/mysql</strong>

Next, we build the PHP software:

Yukimura:~ marcwan$ <strong>make</strong>
And finally, we install it (making sure to copy over a php.ini as well):

Yukimura:~ marcwan$ <strong>sudo make install</strong>
Password: **********
Yukimura:~ marcwan$ <strong>sudo cp php.ini-dist /usr/local/php5/lib/php.ini</strong>

PHP5 is now compiled and installed. Unfortunately, we’re not quite done yet. The Apache that ships on our system comes with PHP 4 installed, and needs to be told where to find our magical new PHP5. This is done by editing /private/etc/httpd/httpd.conf. This requires the use of a text editor. Mac OS X ships with both vi and emacs, so you can do:

Yukimura:~ marcwan$ <strong>sudo vi /private/etc/httpd/httpd.conf</strong>
Yukimura:~ marcwan$ <strong>sudo emacs /private/etc/httpd/httpd.conf</strong>
Password: **********

If you are not comfortable or familiar with either of these editors, we can still use the TextEdit application that ships with OS X, but must do things in a slightly roundabout fashion. We will copy the file to the current directory, edit it, and then copy it back:

Yukimura:~ marcwan$ <strong>cd ~/home</strong>
Yukimura:~ marcwan$ <strong>cp /private/etc/httpd/httpd.conf .</strong>
[edit the file using TextEdit.app]
Yukimura:~ marcwan$ <strong>sudo cp httpd.conf /private/etc/httpd</strong>
Password: ***

And now the actual editing work: The default httpd.conf on your system is configured for PHP4. We will make three small changes to the file to tell it to use PHP5 instead.

First, we will look for a section filled with:


#LoadModule unique_id_module   libexec/httpd/mod_unique_id.so
LoadModule setenvif_module    libexec/httpd/mod_setenvif.so
#LoadModule dav_module         libexec/httpd/libdav.so
#LoadModule ssl_module         libexec/httpd/libssl.so
#LoadModule perl_module        libexec/httpd/libperl.so
LoadModule php4_module        libexec/httpd/libphp4.so
LoadModule hfs_apple_module   libexec/httpd/mod_hfs_apple.so
LoadModule bonjour_module     libexec/httpd/mod_bonjour.so

What we will do is comment out the line with php4_module and add in a new one for PHP5:

#LoadModule php4_module        libexec/httpd/libphp4.so
LoadModule php5_module        /usr/local/php5/libexec/httpd/libphp5.so

Next, we will look for a section similar to the following:

AddModule mod_setenvif.c
#AddModule mod_dav.c
#AddModule mod_ssl.c
#AddModule mod_perl.c
AddModule mod_php4.c
AddModule mod_hfs_apple.c
AddModule mod_bonjour.c

Again, we will comment out the PHP4 line and add in a PHP5 line:

#AddModule mod_php4.c
AddModule mod_php5.c

And finally, we will look for the following section in httpd.conf:

<IfModule mod_php4.c>
    # If php is turned on, we repsect .php and .phps files.
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps

    # Since most users will want index.php to work we
    # also automatically enable index.php
    <IfModule mod_dir.c>
        DirectoryIndex index.html index.php
    </IfModule>
</IfModule>

Again, we will just change it from php4 to php5:

<IfModule mod_php5.c>
    # If php is turned on, we repsect .php and .phps files.
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps

    # Since most users will want index.php to work we
    # also automatically enable index.php
    <IfModule mod_dir.c>
        DirectoryIndex index.html index.php
    </IfModule>
</IfModule>

And you’re done!

To get things working and test it out, edit /Library/WebServer/Documents/test.php:


<?php phpinfo(); ?>

And now we can start the server:

Yukimura:~ marcwan$ <strong>sudo /usr/sbin/apachectl start</strong>
Password: **********
Processing config directory: /private/etc/httpd/users/*.conf
 Processing config file: /private/etc/httpd/users/marcwan.conf
 Processing config file: /private/etc/httpd/users/visitor.conf
/usr/sbin/apachectl start: httpd started

You can view your file by typing in http://localhost/test.php into the address bar of your favourite browser on your Mac.

Congratulations! You now have a working AMP Stack on your machine.

Rolling your Own Apache

If you decide that you’d rather not use the built-in Apache that ships with your Macintosh, then you can easily build and run your own. Again, the process involves unpacking the software, configuring the build, compiling the software, and installing it.

We should do this before we build PHP 5 so that we can tell the latter where our new version of apxs is.

First, unpacking the software:


Yukimura:~ marcwan$ <strong>cd ~/work</strong>
Yukimura:~ marcwan$ <strong>tar xfz ~/Download/apache_1.3.33.tar.gz</strong>
Yukimura:~ marcwan$ <strong>cd apache_1.3.33</strong>

Next we will configure the software. I always enabled the shared object (so) module and typically also include mod_rewrite:

Yukimura:~ marcwan$ <strong>./configure --prefix=/usr/local/apache \
--enable-module=so --enable-module-rewrite</strong>

Now we just compile the software, and then install it with super-user privileges:

Yukimura:~ marcwan$ <strong>make</strong>
Yukimura:~ marcwan$ <strong>sudo make install</strong>
Password: ***********

Before we run it, we should configure the software, using either emacs, vi, or the copy-and-TextEdit solution we showed above for PHP.

We need to add the following three things to the /usr/local/apache/conf/httpd.conf file:

First, in the section where all the other LoadModule calls are, add the following:


LoadModule php5_module libexec/libphp5.so

Next, in the section with all the AddModule calls, we need to add:


AddModule mod_php5.c

Finally, where all the other AddType directives are included, we will add:


AddType application/x-httpd-php .php

We will change our PHP configuration to point at the apxs for this new Apache installation instead

  --with-apxs=/usr/local/mysql/bin/apxs

To test and run this new Apache build:

Yukimura:~ marcwan$ <strong>sudo /usr/local/apache/bin/apachectl start</strong>
Password: *****

Everything should be working!
Comments (17) Add Comment | Tags: Mac OSX AMP PHP MySQL Apache LAMP
XCode is required
Posted By: marcwan Jul 17, 2005 16:08:24
Well, I just got a new OS X system here, and it does not, indeed, come with the headers or compilers. You definitely need to download and install the stuff yourself.

http://developer.apple.com/tools/xcode/index.html
Has Anybody tried it?
Posted By: marcwan Feb 04, 2006 11:08:11

Has anybody actually just tried installing 2.2 on Panther to see what's up? I'm curious as to whether it works.

I seem to recall that 2.1 worked just great on my old Panther system, but that was a long time ago (okay, 6 months!!)

Marc.
I'm trying to follow this but..
Posted By: Muskie Jul 12, 2006 20:50:41
There are a lot of things going wrong. First your <code><strong></code> tags mean you can't cut and past easily the lines to the terminal.

Your libxml2 link is busted but I found and downloaded that ultimately though apparently it comes with Tiger now anyway...

You have no link to the current version of PHP but I got and downloaded version 5.1.4

Your uncompression line:
<code>
tar xfj ~/Download/php-5.04.tar.bz2
</code>

won't work either, but I'm sure you'll spot whats wrong. Why not just double click on the file?

When I tried this command:
<code>
Evangeline:~/Downloads/php-5.1.4 muskie$ ./configure --prefix=/usr/local/php5 \ --with-apxs=/usr/sbin/apxs --with-libxml=/usr/local \ --with-mbstring --with-mbregex --enable-soap \ --with-mysqli=/usr/local/mysql/bin/mysql_config</code>

I got this error:
<code>
configure: warning: --with-apxs=/usr/sbin/apxs: invalid host type
configure: error: can only configure for one host and one target at a time</code>

I checked and the command is in the sbin directory.
After the ./configure command
Posted By: Muskie Jul 24, 2006 15:40:34
I got the following error:
Try adding --with-zlib-dir=<DIR>.

It also told me to check config.log which was 500+ lines of stuff...
Wrong MySQL version...
Posted By: Muskie Jul 24, 2006 16:02:02
I ran ./configure with only
--prefix=/usr/local/php5
--with-mysqli=/usr/local/mysql/bin/mysql_config
and got the following error.

configure: error: wrong mysql library version or lib not found.

Accordint to phpinfo I have API version: 4.1.13a. I checked the directory and it says:
mysql-debug-4.1.10a-apple-darwin6.8-powerpc
mysql-standard-4.1.20-apple-darwin8.6.0-powerpc
mysql-standard-5.0.22-osx10.4-powerpc

I beielve I tried installing version 5 at some point. I've tried a lot of things...
Latest version of XCode
Posted By: Muskie Jul 24, 2006 16:14:52
I believe I don't have the most recent version of XCode... The reason for this is I don't have the fastest internet connection and it is 850 Megs or something. I'm downloading it now. Maybe some of my problems will go away after this...
Weird
Posted By: marcwan Jul 24, 2006 17:02:20

Well, there appear to be some issues with the new XCode on OS X Tiger ... I've found versions of PHP less than 5.1 no longer compile on my machine. I have not yet tried installing MySQL 5 on my powerbook, which I will do in the near future.

I'll update these instructions as I get things working.

If anybody has any tips or other things to try, pass them on !!

I got it to work
Posted By: Muskie Jul 26, 2006 13:03:12
You definitely need the latest version of XCode/Developer's Tools. I'm not so sure about libxml2 since it comes with the OS, but I foreced the latest versions into my system. There is a Mac binary distribution available.

Beware of typos, the <strong> tags at least in Opera prevent cutting and pasting. Although my fumbling around may not give me much credibility but I used to be a software developer and if things go wrong or just weird, try a make clean.

I also had to reinstall MySql though perhaps the make clean might have prevented that as well. I should also mention you can use pico to edit your httpd.conf or better still BBEdit, one of its many tricks is being called from the command line.

According to test.php this is what I got working just now:
PHP Version 5.1.4
System:
Darwin Evangeline.local 8.7.0 Darwin Kernel Version 8.7.0: Fri May 26 15:20:53 PDT 2006; root:xnu-792.6.76.obj~1/RELEASE_PPC Power Macintosh

Configure Command:
'./configure' '--prefix=/usr/local/php5' '--with-apxs=/usr/sbin/apxs' '--with-libxml=/usr/local' '--with-mbstring' '--with-mbregex' '--enable-soap' '--with-mysqli=/usr/local/mysql/bin/mysql_config'

Apache Version:
Apache/1.3.33 (Darwin) PHP/5.1.4

mysqli Client API library version: 4.1.20

I haven't done anything else but run test.php and post this. I will be working through your book and will also get a local version of WordPress running on my PowerBook.
Leopard and PHP5.2.5
Posted By: Sreeram Apr 05, 2008 11:34:53
Hi Mark,

I needed a PHP build with soap enabled. The configure and build seem to go fine but apache2 is not starting up. httpd complains that libphp5.so has the wrong architecture. But it is i386 already. May be you can help.

Macintosh-100:php-5.2.5 Sreeram$ /usr/sbin/httpd -t -D DUMP_MODULES
httpd: Syntax error on line 114 of /private/etc/apache2/httpd.conf: Cannot load /usr/libexec/apache2/libphp5.so into server: dlopen(/usr/libexec/apache2/libphp5.so, 10): no suitable image found. Did find:\n\t/usr/libexec/apache2/libphp5.so: mach-o, but wrong architecture
Macintosh-100:php-5.2.5 Sreeram$ vi /private/etc/apache2/httpd.conf
Macintosh-100:php-5.2.5 Sreeram$ ls -l /usr/libexec/apache2/libphp5.so
-rwxr-xr-x 1 root wheel 4852372 Apr 5 11:25 /usr/libexec/apache2/libphp5.so
Macintosh-100:php-5.2.5 Sreeram$ file /usr/libexec/apache2/libphp5.so
/usr/libexec/apache2/libphp5.so: Mach-O bundle i386
Macintosh-100:php-5.2.5 Sreeram$ file /usr/sbin/httpd
/usr/sbin/httpd: Mach-O universal binary with 4 architectures
/usr/sbin/httpd (for architecture ppc7400): Mach-O executable ppc
/usr/sbin/httpd (for architecture ppc64): Mach-O 64-bit executable ppc64
/usr/sbin/httpd (for architecture i386): Mach-O executable i386
/usr/sbin/httpd (for architecture x86_64): Mach-O 64-bit executable x86_64
Macintosh-100:php-5.2.5 Sreeram$ uname -a
Darwin Macintosh-100.local 9.2.2 Darwin Kernel Version 9.2.2: Tue Mar 4 21:17:34 PST 2008; root:xnu-1228.4.31~1/RELEASE_I386 i386 i386
php architecture issue
Posted By: marcwan Apr 06, 2008 19:45:40
are you using a pre-built version of PHP or something? I think I saw some problem like that when I used a program built for Tiger on Leopard -- you might have to just recompile and then you'll be okay.



marc.

RE: Leopard and PHP5.2.5
Posted By: charles Apr 13, 2008 15:33:43
The issue your having is with your apache config, soon as you recompile it it decides its an i386 version and your on a mac... you need to use something like:

./configure -enable-layout=Darwin -enable-mods-shared=all

when configuring, then it runs fine.
Same issue with Architecture
Posted By: Lantrix Dec 30, 2008 21:28:47
I have the same "Arch" issue with php5 and Apache2.

lantrix@lexx:/etc/apache2 $ file /usr/usr/sbin/httpd -t -D DUMP_MODULES
httpd: Syntax error on line 115 of /private/etc/apache2/httpd.conf: Cannot load /usr/libexec/apache2/libphp5.so into server: dlopen(/usr/libexec/apache2/libphp5.so, 10): no suitable image found. Did find:\n\t/usr/libexec/apache2/libphp5.so: mach-o, but wrong architecture

lantrix@lexx:/etc/apache2 $ file /usr/sbin/httpd
/usr/sbin/httpd: Mach-O fat file with 4 architectures

lantrix@lexx:/etc/apache2 $ file /usr/libexec/apache2/libphp5.so
/usr/libexec/apache2/libphp5.so: Mach-O bundle i386

lantrix@lexx:/etc/apache2 $ uname -a
Darwin lexx.local 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386

This is PHP 5.2.6 compiled from source, and Leopard 10.5.6 using the built in Apache2.
I note that Charles says above that re-compiling apache for Darwin layout will do the trick - but I don't really want to compile apache.
It seems that the supplied Apache2 from apple is at fault.

If there is no other workaround then I'll have to compile apache. Thoughts?
Add a Comment

Title:

Name:

URL:

Comment:

Copyright © 2005-2008 Marc Wandschneider All Rights Reserved.