Sunday, July 12, 2009

Apples Developer Connection Documentation is buggy

I was working this morning on setting up a LAMP environment in OSX.  I found this tutorial  from Apple's Developer Connection. As I haven't set up PHP on a Mac in quite a while i felt I needed a refresher.

It was going good till I got to the section "Enabling PHP in Apache."  They outline a script you need to run but to a new Mac user this isn't readily apparent how to do.  Obviously this is geared towards the advanced Mac Developer even though the topic is for a beginner.  I finally deduce from past experience that this needs to be run like a batch script on Windows.   I do a search on Google for "shell scripting in osx" and found that I needed to run "sh filename.sh" in the terminal window.  So I created a file and copy/pasted the script into the file, saved and switched to the terminal.  Here is the script I pasted, DO NOT RUN THIS:

set admin_email to (do shell script "defaults read AddressBookMe ExistingEmailAddress")
user_www=$HOME/Sites
filename=php-test
user_index=${user_www}/${filename}.php
user_db=${user_www}/${filename}-db.sqlite3
# NOTE: Having a writeable database in your home directory can be a security risk!

conf=`apachectl -V | awk -F= '/SERVER_CONFIG/ {print \$2}'| sed 's/"//g'`
conf_old=$conf.$$
conf_new=/tmp/php_conf.new

touch $user_db
chmod a+r $user_index
chmod a+w $user_db
chmod a+w $user_www

echo "Enabling PHP in $conf ..."
sed '/#LoadModule php5_module/s/#LoadModule/LoadModule/' $conf | sed
"s^you@example.com^<b>\$admin_email</b>^" > $conf_new

echo "(Re)Starting Apache ..."
osascript <<EOF
do shell script "/bin/mv -f $conf $conf_old; /bin/mv $conf_new $conf;
/usr/sbin/apachectl restart" with administrator privileges

EOF
The first time I run it I am asked for the administrative password which I provide.  After I see that the script had a few errors.  But the instructions on the tutorial say I should be able to create phpinfo page and see the phpinfo data.  I create the file try to run it and the web server isn't running.

I do some troubleshooting and eventually figure out that httpd.conf has not no data in it anymore.  There is an older file with a version number attached but I can't copy or write to httpd.conf cause I don't have su priv on this computer.

I go back to the script file and try to figure out what happened. First I need to fix my apache.  I hack together a shell script to fix my httpd.conf and I come up with this:

osascript <<EOF

do shell script "/bin/mv -f /private/etc/apache2/httpd.conf.9002 /private/etc/apache2/httpd.conf" with administrator privileges

EOF

This restores the original httpd.conf that was made as a back up.  Retry to access a file in apache and it serves it.  Success!  At this point I want to make my own backup file of httpd.conf in case the script screws it up more. 

osascript <<EOF

do shell script "/bin/cp -f /private/etc/apache2/httpd.conf /private/etc/apache2/httpd.conf.bak" with administrator privileges

EOF

So why did httpd.conf have no data in it?  Looking over the script it seems that it is uncommenting the line for the PHP module and sending the output to conf_new.  But it seems the file specified in conf_new is never created in the script.  So when the final line is called to copy the new file over httpd.conf there is nothing to copy.  I solve this by adding another line: touch $conf_new

Now let's tackle the script, the first error I see is that a file doesn't exist: chmod: /Users/ralph/Sites/php-test.php: No such file or directory

The file doesn't exist apparently touch $user_index isn't included in the script.  Which is funny cause nothing else in the script requires the $user_index.  The script is basically just trying to create a php file.  I also add touch $user_index to the script.

Next error is: s^you@example.com^<b>$admin_email</b>^: No such file or directory

I wasn't quite sure what was causing this error and I couldn't solve fixing it but I determined that it was trying to replace the default admin e-mail with the one I specified earlier.  I took out that part of the command.  So the new line now looks like: sed '/#LoadModule php5_module/s/#LoadModule/LoadModule/' $conf > $conf_new

The final script looks like this:

user_www=$HOME/Sites
filename=php-test
user_index=${user_www}/${filename}.php
user_db=${user_www}/${filename}-db.sqlite3
# NOTE: Having a writeable database in your home directory can be a security risk!

conf=`apachectl -V | awk -F= '/SERVER_CONFIG/ {print \$2}'| sed 's/"//g'`
conf_old=$conf.$$
conf_new=/tmp/php_conf.new

touch $user_index
touch $user_db
touch $conf_new
chmod a+r $user_index
chmod a+w $user_db
chmod a+w $user_www
chmod a+w $conf_new

echo "Enabling PHP in $conf ..."
sed '/#LoadModule php5_module/s/#LoadModule/LoadModule/' $conf > $conf_new

echo "(Re)Starting Apache ..."
osascript <<EOF
do shell script "/bin/mv -f $conf $conf_old; /bin/mv $conf_new $conf;
/usr/sbin/apachectl restart" with administrator privileges

EOF

I feel like the script written in the Developers Connection article was just written and not tested.  But what is really concerning is there is no way to provide feedback on the article on the page.  MSDN provides a way on every page asking if the tutorial was helpful and provides an area to comment. 

Needless to say I did not finish the tutorial. 


Comments are closed.

Blog Posts by:

Currently Viewable:

The Official jQuery Podcast

with Ralph Whitbeck & Rey Bango

You can subscribe to the show in iTunes or via the raw RSS feed

My Twitter Updates

View Twitter Page