Fixing my PHP woes with MacPorts 2
As I mentioned in my blog post earlier today, I have been having issues using the JpGraph graphing package for PHP with Apple’s built-in PHP under MacOS 10.5. It appears Apple’s security efforts have “secured” PHP to the point where JpGraph (and PDFLib) will not function properly under the built-in PHP. [Note added after initial post: The rest of this post describes installing Apache 2 and PHP under MacPorts and configuring it to be similar to Apple's built-in servers. This appeared to solve my problems, but then my Mac locked-up [possibly related] and on reboot, the new MacPorts-based PHP installation started throwing the same errors as Apple’s built-in PHP. More information on this problem is located in my newer post on the issue.]
I had tried to alleviate the solution previously by compiling a version of PHP myself that would be compatible with Apple’s built-in Apache 2 web server. This turned out to be difficult because Apple’s Apache 2 web server is a “universal” binary, meaning it contains four seperate binaries (for 32-bit PowerPC, 64-bit PowerPC, 32-bit Intel, and 64-bit Intel processors). As such I needed to compile a “universal” PHP binary and since I wanted MySQL support, I needed a “universal” MySQL binary. This turned out to be too much for this astronomer, so I gave up on making a new PHP that was compatible with Apple’s built-in web server.
So I bit the bullet and after reviewing the options, I decided to install Apache 2 and PHP under MacPorts. If you have read the other posts on this site, you know I really like MacPorts as the quick and dirty way to get many things running on the Mac. However, despite this, I don’t like installing MacPorts for items Apple provides, instead generally preferring to use Apple’s “pre-installed” versions. Furthermore, there have been a lot of complaints on the MacPorts mailing lists about various issues compiling PHP5. So I wasn’t as quick to jump onto the MacPorts bandwagon for PHP as I am for other problems. However, since I am familiar with MacPorts, I decided this was the best approach for getting my online Finder Charts to work again.
The process proved reasonably painless, especially since I was able to review this blog post, where he lays out pretty much what to do. However, since I wanted to achieve maximum compatibility with Apple’s built-in web server and PHP setup, in case I wanted to switch back, I ended up doing things a little bit differently, so I am outlining my steps here.
- I started by installing the MacPorts version of Apache 2 using the command
sudo port install apache2 - Next, I had to create the Apache 2 configuration files and edit them. I started by copying the sample configuration file
sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf
and then editing/opt/local/apache2/conf/httpd.confwith my favorite text editor to change the configuration to match the that of the built-in Apache 2 server a closely as possible. My matching the configuration of Apple’s built-in server, I can switch back to it with relative ease if I choose to do so later. So I made the following changes to/opt/local/apache2/conf/httpd.conf: -
- I changed
DocumentRootto"/Library/WebServer/Documents"as is the case with Apple’s built-in server. - To allow
index.phpfiles to be used as directory indexes, I changedDirectoryIndex index.html
toDirectoryIndex index.html index.php
(I don’t know why this isn’t necessary with Apple’s built-in server, but it was necessary here). - I changed to
and in that directory block of code, I added “
MultiViews” to theOptionsline. - I changed to in order to prevent the listing of
.DS_Storefiles in directory listings by the web server. - Before the
ErrorLogblock of code in this file, I added the following lines copied from Apple’s default Apache 2 configuration:# # Apple specific filesystem protection. # Order allow,deny Deny from all Satisfy All Order allow,deny Deny from all Satisfy All
- I changed
ErrorLogto"/private/var/log/apache2/error_log" - I changed
CustomLogto"/private/var/log/apache2/access_log common" - To match Apple’s Apache 2 server configuration, I changed
ScriptAliasMatchto^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
- I changed back to
- I added the following Handles to the “To use CGI Scripts” block of code:
AddHandler imap-file .map AddHandler cgi-script .cgi AddHandler cgi-script .pl - I uncommended the following lines near the end of the file:
- [OPTIONAL] Because I use the WebDAV server on my server, I also uncommented
- Finally, I added the following lines to the end of the file in order to allow loading of the PHP5 configuration
- I changed
- Next, I editted
/opt/local/apache2/conf/extra/httpd-userdir.confand added the following to the end of the file# # Users might not be in /Users/*/Sites, so use user-specific config files. # Include /private/etc/apache2/users/*.conf
- [OPTIONAL] Since I use the built-in WebDAV server, I made a backup of the WebDAV configuration, then copied the
Default MacOS X one, because I have spent a lot of time tweaking it previously and I didn’t want to have to reinvent the wheel.
sudo cp /opt/local/apache2/conf/extra/httpd-dav.conf /opt/local/apache2/conf/extra/httpd-dav.conf.orig sudo cp /etc/apache2/extra/httpd-dav.conf /opt/local/apache2/conf/extra/httpd-dav.conf
- I had to install PHP5 with MacPorts. Since I wanted to add support for Apache 2 and MySQL, I entered the command:
sudo port install php5 +apache2 +mysql5 +pearwhich has the side effect of installing MacPorts version of MySQL as well. Assuming everything goes well, after a few minutes (this takes longer than the apache2 install earlier), the installation will end. At this point we can configure the Apache 2 mod_php module by typing:cd /opt/local/apache2/modules/opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
- [OPTIONAL] Next, I had to create the PHP5 configuration file and edit it. I started by copying the sample configuration file
sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.iniand then editing/opt/local/etc/php.inito make it match/etc/php.ini(which Apple’s built-in PHP uses). All the changes I made were optional and related to the specifics of my setup. The only interesting one was that I wanted to continue to use the MySQL.com binary distribution of MySQL server, so I set the following variables in/opt/local/etc/php.ini:mysql.default_portfrom “” to3306mysql.default_socketfrom “” to/private/tmp/mysql.sockmysqli.default_socketfrom “” to/private/tmp/mysql.sock
- I deactivated Apple’s built-in web server by turning off Web Sharing in the Sharing.prefPane.
- Finally, I launched the new webserver (and set it up for launching on boot-up in the future) by typing
sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
If this breaks anything, I can reverse the process by typingsudo launchctl unload -w /Library/LaunchDaemons/org.macports.apache2.plist
- [OPTIONAL] I like the ability to turn on and off the Apache webserver from the command line using apachectl. I can “emulate” this in /bin/tcsh (my prefered shell) by adding the following command to the ~/.tcshrc file:
alias apache2ctl 'sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper'
After which I can bring down the server by typingapache2ctl stopand restart it by typingapache2ctl start.

