DSV Developer Blog

Random musings by DSV developers pertaining to the act of development... or whatever else strikes their fancy. This is our digital soapbox. Rants, raves, mini-tutorials to the passing of interesting tidbits. We won't guarantee content here every day, or even every week, but we will guarantee that the content here will be of interest... to us at least.

2009
Aug
11

Snow Leopard: Up and Running with Apache 2, PHP 5 and MySQL 5 using MacPorts

Yes, it is true that Snow Leopard comes with these critical web development tools pre-installed, however the installations are at the mercy of Apple's system updates. Customizing them with extra libraries, etc. has been known to be an effort in futility as they are automatically "updated".

By using MacPorts to install these tools, you can continue to maintain and update them at your leisure safe in the knowledge that they won't change unless you specifically change them. You could use Fink as an alternative to MacPorts, or even building the tools yourself. We prefer the least amount of inconvenience and the highest level of stability following accepted standards. That's MacPorts.

Install Helpful Pre-requisites

There are some tools you can install that will make your life a little easier in the command line environment, and for web development. The first, of course, is to install MacPorts itself. This should be pretty self-explanatory. Next we'll install a couple programs that make MacPorts just plain work better:

    sudo port install gawk; sudo port install nawk

Then we'll install a better text editor for the command line (Joe's Own Editor) and the ever useful png management tool, pngcrush:

    sudo port install joe; sudo port install pngcrush

Install Apache 2

IMPORTANT: To avoid conflicts with the stock installation of Apache 2 in Snow Leopard, open System Preferences, go to the Sharing control panel, and uncheck "Personal Web Sharing".

To install Apache 2, issue the following command:

    sudo port install apache2

You will see a note near the end of the output that explains how to create a system startup item for Apache 2. Copy, paste and execute the command it gives. It will look similar to the following:

    sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

Install MySQL 5

MySQL can be installed in one of two flavors: Standard and Server. For use in a web development environment, we want the server edition, so issue the following command to install it:

    sudo install mysql5-server

For some reason, Snow Leopard expects the location of the sock for MySQL to reside in one place, it installs it to another place itself, and MacPorts installs it to yet a third place. Let's clean that up once and for all. First, find out where the sock is installed by the revision you got from MacPorts:

    mysql_config --socket

Now, you will need to create a symbolic link (symlink) from the response to the above command to /tmp/mysql.sock where Snow Leopard expects to find it:

    ln -s path/of/mysql.sock/from/above /tmp/mysql.sock

We will also need to makes changes for php to find the sock as well. More on that in the next section.

Install PHP 5

There are a number of variants for PHP 5 you will want to consider installing. These seem to be changing lately to use additional installs rather than adding variants. The following are the install commands we used:

    sudo port install php5 +apache2 +pear +mcrypt
    sudo port install php5-tidy
    sudo port install php5-iconv
    sudo port install php5-mysql

Next, we need to tell Apache 2 that PHP 5 is available and we want to use it:

    cd /opt/local/apache2/modules
    sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

Get Those Config Files Working

Configure Apache 2

MacPorts installs a sample version of the Apache 2 configuration file. We need to copy this to the right name so it will be picked up when Apache 2 launches:

    sudo cp /opt/local/apache2/conf/httpd.conf.sample
    /opt/local/apache2/conf/httpd.conf

Next, we need to activate PHP 5 inside Apache 2:

    cd /opt/local/apache2/modules
    sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

Now we need to set Apache's Document Root, default Directory Index, and Root Directory Directive. To make these changes, you need to open the above mentioned httpd.conf file in your favorite command line text editor. We have selected joe, that we installed earlier, to make these edits. (Yes, you could use a text editor in the GUI but you HAVE to ensure the line breaks are of the correct type and that the file is saved a raw ASCII with no formatting code what-so-ever)

    sudo joe /opt/local/apache2/conf/httpd.conf

First, let's set the Document Root to Snow Leopard's default, for the sake of KISS. Press CTRL-K-F to search for DocumentRoot. Press Return until you find a line similar to this:

    DocumentRoot "/opt/local/apache2/htdocs"

Change that to read:

    DocumentRoot "/Library/WebServer/Documents"

Next, you'll need to change the Directory Directive to match this new path, so CTRL-K-F to find again, and this time search for . Change that to match the path above:

    <Directory "/Library/WebServer/Documents">

Now we need to tell Apache 2 to look for a php file for the index. Find again and this time look for "DirectoryIndex". Make the entry look like the following:

    <IfModule dir_module>
        DirectoryIndex index.html index.php
    </IfModule>

Finally, we need to add a couple mimetype handlers so Apache 2 properly presents php files. Add the following t the block:

    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps

Save and close the httpd.conf file. (CTRL-K-X)

Configure PHP 5

As was the case with Apache 2 above, MacPorts has installed a sample config file for PHP and we need to set the name correctly so it will be picked up when the we server starts:

    sudo cp /opt/local/etc/php5/php.ini-dev /opt/local/etc/php5/php.ini

Find (CTRL-K-F) "mysql.default_socket" and give it the path to the symbolic link we set earlier:

    mysql.default_socket = /tmp/mysql.sock

Recent versions of PHP 5 have become cranky about timezone settings. To prevent your server from getting all snarky on you, find the following line and uncomment it, adding your timezone as appropriate. In the example below I've set the timezone to Central time:

    date.timezone=America/Chicago

You will also need to uncomment (remove the ; at the start of the line) each of the modules you want to run, such as the mysql extension, the tidy extension, the mcrypt extension, etc.

Controlling Apache 2

First, we'll add an alias to your shell profile to make controlling Apache 2 much easier:

    joe ~/.profile

Now, add the following line in your .profile:

    alias apache2ctl='sudo /opt/local/apache2/bin/apachectl'

Reload your profile. To do this, you can either quit the terminal and restart it, or issue the following command:

    source ~/.profile

There are 3 commands you need to learn to control your new web server:

    sudo apache2ctl start // Start the web service
    sudo apache2ctl stop  // Stop the web service
    sudo apache2ctl graceful // Restart the web service in a safe and friendly way.

Test your installation

Once everything has been completed and the Apache 2 server is running, create a file with the contents below named "index.php" and place it in /Library/WebServer/Documents:

    <?php phpinfo(); ?>

Open your web browser to http://localhost/index.php and you should be greeted with a pretty purple and grey diagnostic information page from PHP 5. Enjoy!


0 comments

User Comments


Add Your Comment

You must be logged in to post a comment.