Published:

11 Jul 2009

Categories:

Code
Software
Tutorials

Comments:

9 total

Installing PHP 5.3 with mysqlnd on Mac OS X with MacPorts

Historically, I’ve always preferred to use Apple’s built-in Apache 2.2 and PHP 5.x that comes with Leopard. However, after trying to compile PHP 5.3 from scratch and connect it with Apache, I decided to just use the MacPorts installer instead. That did mean giving up control of a finely-tuned Apache installation, but in the end, I think I’ve ended up with a better localhost system.


Prerequisites

Also, make sure that your MacPorts install is completely up-to-date with:

sudo port -d selfupdate

Installation

Now, I’ve never used MacPorts to install PHP or Apache before, so I’m starting with a clean slate. If you’ve already installed PHP or Apache with MacPorts, your steps may be different. As always, your mileage may vary. For me, I develop several open-source projects, so I need things that others may not. Adjust these steps as necessary.

  1. Using “Web Sharing” in your Sharing Preferences should be turned off. Currently this points to the (old) Apple Apache installation, although we’ll change that later.
  2. From Terminal, install PHP 5.3 + Apache, and some other stuff. This will likely take quite a while. I’m installing SQLite, MySQL, and PostgreSQL because of my work on CacheCore, so you may or may not need those. mysqlnd is the new PHP Native Driver for MySQL and is supposed to be better, so we’ll use that. We also need to enable non-default settings for cURL.
    sudo port install curl +ssl+ipv6+ares+idn+gss+openldap+sftp_scp \
    php5 +apache2+fastcgi+pear

    You can see all available options by running port variants php5

  3. The new Apache configuration file is stored at /opt/local/apache2/conf/httpd.conf while the old one was at /etc/apache2/conf/httpd.conf. Take a moment to copy over any settings you’ll want to maintain into the new Apache installation.
  4. You’ll also want to include your extra settings. Toward the bottom of your httpd.conf file, add the following line:
    # All settings
    Include conf/extra/*.conf
  5. If you don’t have an SSL certificate, rename your SSL configuration:
    cd /opt/local/apache2;
    sudo mv conf/extra/httpd-ssl.conf conf/extra/httpd-ssl.conf-disabled
  6. You’ll also want to enable PHP in Apache:
    sudo mv conf/extras-conf/mod_php.conf conf/extra/mod_php.conf
  7. The new PHP configuration file is stored at /opt/local/etc/php5/php.ini while the old one was at /etc/php.ini. Take a moment to copy over any settings you’ll want to maintain into the new PHP installation.
  8. Restart Apache. If you were using apachectl before, it still points to the old Apache, so we’ll want to point specifically to the new one.
    sudo /opt/local/apache2/bin/apachectl restart

At this point, PHP 5.3 with Apache 2.2 and the new mysqlnd extension are all installed.


Extra stuff

  1. I generally prefer to have lots more stuff installed locally so that I can worry more about developing and less about installing. Because of this, I also install a few other things.
    sudo port install memcached \
        php5-apc \
        php5-curl \
        php5-http \
        php5-iconv \
        php5-imagick \
        php5-mbstring \
        php5-memcache \
        php5-mysql \
        php5-postgresql \
        php5-sockets \
        php5-sqlite \
        php5-tidy \
        php5-xdebug
  2. Restart Apache with:
    sudo /opt/local/apache2/bin/apachectl restart

Replacing older versions

Now, we want to continue using our command-line PHP scripts and the “Web Sharing” checkbox in the Sharing Preference Pane, so let’s make sure that those are all pointing to the new locations instead. We’ll be backing up and redirecting php, apachectl, and httpd.

  1. Open up your Sharing Preference Pane, and disable Web Sharing
  2. Run the following command on the Terminal:
    sudo mv /usr/bin/php /usr/bin/php.bak && sudo ln -s /opt/local/bin/php /usr/bin/php; \
    sudo mv /usr/sbin/apachectl /usr/sbin/apachectl.bak && sudo ln -s /opt/local/apache2/bin/apachectl /usr/sbin/apachectl; \
    sudo mv /usr/sbin/httpd /usr/sbin/httpd.bak && sudo ln -s /opt/local/apache2/bin/httpd /usr/sbin/httpd; \
  3. Re-enable Web Sharing in the preference pane.

Shortcuts

Lastly, I like to set up some shortcuts so that I can access all of my important localhost stuff from one place. I’ll create a new directory called www-config and then I’ll place symlinks into it for quick access to Apache and PHP configuration files.

sudo mkdir /www-config && \
cd /www-config && \
sudo ln -s /opt/local/apache2/bin/apachectl /www-config/apachectl && \
sudo ln -s /opt/local/apache2/conf/ /www-config/httpd-conf && \
sudo ln -s /opt/local/var/db/php5/ /www-config/php-ini && \
sudo ln -s /opt/local/etc/php5/php.ini /www-config/php.ini

Published:

27 Feb 2009

Categories:

Code
Projects

Comments:

3 total

Introducing DOMBuilder

I hate the DOM. Actually, I take that back. I love the DOM, but I hate the fact that generating DOM nodes in JavaScript is so verbose and unintuitive. You need to construct a new element, then add properties, then construct a child element, then add properties, then append the child to the parent, and the parent to whatever DOM object you want that’s already in the page.

A few years ago I discovered Builder.node(), a component of Scriptaculous. The problem is that Scriptaculous relies on Prototype, and both are HUGE JavaScript libraries. Later I moved to Moo.fx/MooTools, then I didn’t do much JavaScript for a while, then I started doing a lot with YUI, while sprinkling a little jQuery around here and there. None of these other frameworks had an equivalent to Builder.node(), and again, that sucks.

So last night, I wrote a small JavaScript class to handle this very thing. Introducing DOMBuilder. DOMBuilder is small, fast, and doesn’t depend on any other JavaScript frameworks meaning that it’s easy to use in any project where you need to construct nested DOM elements. The fully commented debug version clocks in around 3k. The minified version is 739 bytes. With gzip compression, it squeezes down to a mere 393 bytes.

It’s not quite as terse or elegant as I’d like (yet), but it’s a good result for about 2 hours of hacking.

Examples

Here’s the HTML we want to generate:

<div class="location_select_control">
	<a href="" class="location_select_label">
		<label>This is my label</label>
	</a>
</div>

Here is how we’d do it with the standard DOM:

control_div = document.createElement('div');
control_div.className = "location_select_control";
control_link = document.createElement('a');
control_link.href = "";
control_link.className = "location_select_label";
control_label = document.createElement('label');
control_label.innerHTML = "This is my label";
control_link.appendChild(control_label);
control_div.appendChild(control_link);
document.body.appendChild(control_div);

Lastly, here’s how we’d do it with DOMBuilder:

document.body.appendChild($dom('div', { class:'location_select_control' }).child(
	$dom('a', { href:'', class:'location_select_label' }).child(
		$dom('label').innerHTML('This is my label')
	)
).asDOM());

Download

This code is BSD licensed, so feel free to use it in personal or commercial projects. You can download it from Github.


Published:

10 Dec 2008

Categories:

AWS
Code
Projects

Comments:

None

Tarzan 2.0 is finally here!

After 18 months of ongoing development, I am proud to announce the immediate availability of Tarzan 2.0! The Tarzan platform has complete support for six different AWS services (S3, CloudFront, EC2, SimpleDB, SQS, and Amazon Associates) and has been built from the ground-up to be fast, memory-efficient, easy to use, and easy to build on top of by providing a solid set of core tools for your (and our) web application.

You can download the 2.0 release from http://bit.ly/tarzan2, and please Digg us at http://bit.ly/digg-tarzan.