A blog named for the muse of Astronomy containing musings by an astronomer

Fixing my PHP woes with MacPorts

Posted on August 14, 2008 by Juan

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.

  1. I started by installing the MacPorts version of Apache 2 using the command sudo port install apache2
  2. 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.conf with 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:
    1. I changed DocumentRoot to "/Library/WebServer/Documents" as is the case with Apple’s built-in server.
    2. To allow index.php files to be used as directory indexes, I changed
      DirectoryIndex index.html
      DirectoryIndex index.html index.php
      (I don’t know why this isn’t necessary with Apple’s built-in server, but it was necessary here).
    3. I changed to and in that directory block of code, I added “MultiViews” to the Options line.
    4. I changed to in order to prevent the listing of .DS_Store files in directory listings by the web server.
    5. Before the ErrorLog block 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
    6. I changed ErrorLog to "/private/var/log/apache2/error_log"
    7. I changed CustomLog to "/private/var/log/apache2/access_log common"
    8. To match Apple’s Apache 2 server configuration, I changed ScriptAliasMatch to
      ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
    9. I changed back to
    10. 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
    11. I uncommended the following lines near the end of the file:
      Include conf/extra/httpd-autoindex.conf
      Include conf/extra/httpd-userdir.conf
    12. [OPTIONAL] Because I use the WebDAV server on my server, I also uncommented Include conf/extra/httpd-dav.conf
    13. Finally, I added the following lines to the end of the file in order to allow loading of the PHP5 configuration
      # Load PHP5 configuration
      Include conf/extras-conf/*.conf
  3. Next, I editted /opt/local/apache2/conf/extra/httpd-userdir.conf and 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
  4. [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
  5. 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 +pear which 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
  6. [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.ini and then editing /opt/local/etc/php.ini to 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
    1. mysql.default_port from “” to 3306
    2. mysql.default_socket from “” to /private/tmp/mysql.sock
    3. mysqli.default_socket from “” to /private/tmp/mysql.sock
  7. I deactivated Apple’s built-in web server by turning off Web Sharing in the Sharing.prefPane.
  8. 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 typing
    sudo launchctl unload -w /Library/LaunchDaemons/org.macports.apache2.plist
  9. [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 typing apache2ctl stop and restart it by typing apache2ctl start.

2 Trackbacks/Pingbacks

  1. 18 08 08 11:28

    Urania » Blog Archive » PHP on Leopard … damn irritating sometimes

  2. 05 09 08 12:34

    Urania » Blog Archive » Fork()ing Problems with FreeType solved

Leave a Reply

  • Translate

  • Astro Pic o' the Day

  • Archives

  • Admin

↑ Top