Wednesday, August 15, 2007

Apache2 + mysql5 + php5 + Mac OS 10.4 = RAWR!

More precisely: Apache 2.2.4 + MySQL 5.0.45 + PHP 5.2.3 on a Mac Pro running Mac OS 10.4.10
  • The most helpful resource I found was a 05/23/2007 carlmoonan.com blog post. So I updated it for PHP 5.2.3 and bypassed a MySQL bug. I also did some StartupItem research on macdevcenter.com (here, here, and here).

  • This should only be done if you're comfortable with unix and compiling source code.

  • In order to compile source code on your Mac you'll need the latest version of the Xcode Tools which you can find on Apple.com. It contains the Intel gcc (C++ compiler) which you will need to compile anything.

Download, Compile & Install Apache from Source

  1. Copy the following shell (bash) script and paste it into a file named "apacheBuildScript.sh" somewhere you can find it:

    #!/bin/bash

    curl http://www.mirrorservice.org/sites/ftp.apache.org/httpd/httpd-2.2.4.tar.gz > httpd-2.2.4.tar.gz
    tar -zxf httpd-2.2.4.tar.gz
    cd httpd-2.2.4
    ./configure --enable-layout=Apache --prefix=/usr/local/apache/2.2.4 --enable-mods-shared=all --with-ssl=/usr --with-mpm=prefork --enable-ssl --enable-dav --enable-cache --enable-proxy --enable-shared --disable-static --disable-unique-id --disable-ipv6 --enable-logio --enable-deflate --with-ldap --with-ldap-include=/usr/include --with-ldap-lib=/usr/lib --with-included-apr --enable-ldap --enable-auth-ldap --enable-cgi --enable-cgid --enable-suexec

    make
    sudo make install

  2. Make the file executable from the terminal:
    chmod +x apacheBuildScript.sh

    Then run it from the command line. Enter your root password when it asks (sudo).

Download & Install the MySQL Binary

  1. Grab the x86 mysql-5.0.45 binary from dev.mysql.com and run the installer.

  2. Fix the libmysqlclient.15.dylib location bug by entering the following code at the terminal:

    cd /usr/local/mysql/lib
    mkdir mysql
    cp libmysqlclient.15.dylib mysql/libmysqlclient.15.dylib

Download, Compile & Install PHP (and PEAR, a required lib) from Source

  1. Copy the following shell (bash) script and paste it into a file named "phpBuildScript.sh" somewhere you can find it:

    #!/bin/bash

    curl http://go-pear.org > go-pear.php
    sudo php go-pear.php

    curl http://uk3.php.net/distributions/php-5.2.3.tar.gz > php-5.2.3.tar.gz
    tar -zxf php-5.2.3.tar.gz
    cd php-5.2.3
    ./configure --prefix=/usr/local/PHP/5.2.3 --mandir=/usr/share/man --infodir=/usr/share/info --sysconfdir=/etc --with-zlib --with-xml --with-zlib-dir=/usr --with-openssl --enable-exif --enable-ftp --enable-mbstring --enable-mbregex --enable-sockets --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-apxs2=/usr/local/Apache/2.2.4/bin/apxs

    make
    make test
    sudo make install

  2. Make the file executable from the terminal:
    chmod +x phpBuildScript.sh

    Then run it from the command line. Enter your root password when it asks (sudo).


Congratulations, you've got it all installed. But wait.... there's more!



Disable the default Apache installation

  1. Go to System Preferences > Sharing > Services and uncheck Personal Web Sharing.

Configure Apache2 to run PHP files

  1. Open "/usr/local/apache/2.2.4/conf/httpd.conf" and add the following code to the bottom of the file.

    <IfModule php5_module>
    AddType application/x-httpd-php .php .phtml
    AddType application/x-httpd-php-source .phps
    </IfModule>

  2. Also Make sure the installer correctly added "LoadModule php5_module modules/libphp5.so" somewhere else in the file. If not, you're going to need to add it yourself. You can put it anywhere, preferably bellow the other LoadModule's.

  3. Find the "DirectoryIndex" line and add "index.php" to the end

(Optional) Additional Apache2 Configuring

  1. Open "/usr/local/apache/2.2.4/conf/httpd.conf" if it's not already.

  2. Find the line with "DocumentRoot" on it and change it to the following if you want your old Apache site to work:
    DocumentRoot "/Library/WebServer/Documents"
    The DocumentRoot. It is where your web site files will go.

  3. Update and uncomment the "ServerAdmin" line with your email and the "ServerName" line with your url for good measure. Read the comments for details.

  4. NOTE: You can also use Symbolic Links to reference folders elsewhere on your computer. "ln -s origPath newPath" The newPath will link to the origPath ("s" for symbolic) similar to a mac alias. This will require the FollowSymLinks or SymLinksifOwnerMatch Apache options to be enabled for the parent directiry or globally. See the Apache options docs for more information.

(Optional) Configuring Apache2 to use the existing StartupItem

  1. Replace the old apachectl by renaming it and replacing it with a Symbolic Link to the new one.
    sudo mv /usr/sbin/apachectl /usr/sbin/apachectl.old
    sudo ln -s /usr/local/apache/2.2.4/bin/apachectl /usr/sbin/apachectl
    This way the existing StartupItem [/System/Library/StartupItems/Apache] will run Apache2 instead of the old Apache. This will also allow for starting Apache2 via System Preferences > Sharing > Services > Personal Web Sharing.

  2. If for whatever reason you need to revert to the old Apache you can redirect the symlink:
    sudo ln -sf /usr/sbin/apachectl.old /usr/sbin/apachectl
    The added "f" forces the new link to replace the old one.

  3. Edit "/usr/local/apache/2.2.4/conf/httpd.conf" again by adding the following line to the file. You can put this line anywhere, but I chose to put it after the ServerRoot line, since that's where the comments about PidFile are.
    PidFile "/private/var/run/httpd.pid"
    This will allow System Preferences to be able to detect that the server is running and check the "Personal Web Sharing" box accordingly.

Start the new Apache2 server

  1. If you've done the previous step, all you need to do is check the "Personal Web Sharing" checkbox or select it and push the "Start" button. It should tell you it's starting and grey out then complete and go solid again.

  2. If you was to start Apache2 manually you can run the following at the terminal:
    sudo apachectl start

  3. View the default web page to make sure it works: http://127.0.0.1/