Saturday, July 18, 2009

Installing Apache, MySQL and PHP on Leopard

Working in Web Development means you have to have a development environment installed on your local machine, in order to test and develop dynamic pages, using a web server (Apache,), a database (MySQL) and a scripting language (PHP). There are ways of getting those components installed in a bundle, like WAMP, LAMP or MAMP. But as a developer, you are more the manual type, right? So as I had to go through that installation process recently, this article documents the steps I went through.
Apache

OSX already comes with Apache installed, it is just a matter of starting the server. You can do this if you go to System Preferences > Sharing and check “Web Sharing”. The Apache default page should now be displayed at
http://localhost

Later on, you can use the following commands to start, stop or restart Apache:

$ sudo apachectl start
$ sudo apachectl stop
$ sudo apachectl restart

If you would like to change the DocumentRoot of the server, you need to edit the httpd.conf file:

$ sudo vi /etc/apache2/httpd.conf

In here, you need to change the DocumentRoot setting:

DocumentRoot "/Users/myUser/myNewWebroot/"

...


PHP

PHP comes bundled up with Leopard as well. The important things to know here are where it got installed and where to find the configuration file.

Most likely, it got installed to:
/usr/local/php5

The configuration file should be located at:
/private/etc/php.ini

You only need to make sure that Apache knows that PHP is available, so edit httpd.conf:
$ sudo vi /etc/apache2/httpd.conf

And add the following lines (in the appropriate sections, to keep things tidy):
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

LoadModule php5_module libexec/apache2/libphp5.so

Finished with that, restart Apache, empty the browser cache and then load a php file for testing if it is correctly interpreted.
MySQL

Download the most recent dmg image from the MySQL site.

Before actually installing MySQL, I found it helps to restart the computer before proceeding with the installation. When running through the installation wizard, MySQL will get installed to:
/usr/local/mysql-VERSION

So, for example:
/usr/local/mysql-5.0.51b-osx10.5-x86/

Also, a symlink should have been created:
/usr/local/mysql -> mysql-5.0.51b-osx10.5-x86

You should also install the Preference Pane, which comes with the installation package as MySQL.prefPane

To start MySQL manually, run the following command:
$ sudo /Library/StartupItems/MySQLCOM/MySQLCOM start

You should also add MySQL to $PATH:
$ vi ~/.profile
$ export PATH=$PATH:/usr/local/mysql/bin
$ source ~/.profile

To check whether that was successful, run:
$ echo $PATH

The default settings for the root user are:

* Username: root
* Password: [leave blank]

Add-on: PHPmyAdmin

To get PHPmyAdmin installed, which comes in handy for managing your database(s), download the latest package from their download page. Extract that package to a directory somewhere in your DocumentRoot.

Open config.sample.inc.php with an editor of your choice and add the following details for your MySQL installation:

/*
* This is needed for cookie based authentication to encrypt password in
* cookie
*/
$cfg['blowfish_secret'] = 'whatever'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = ''; // use here your password

After you made those changes, save the file as config.inc.php.
PEAR

PEAR should also already be available on your Mac. The location is probably:
/usr/local/php5/bin/pear

It is a good idea to add the path to PEAR to $PATH, similar to setting the path for MySQL (see above). In addition, upgrade PEAR to the latest version like so:

$ sudo pear channel-update pear.php.net
$ sudo pear upgrade PEAR
Resources

* Installing MySQL on Mac OS X (MySQL Reference Manual)
* Installing MySQL 5.0 on Max OS X 10.5 client
* Installing MySQL on Mac OS X


Download the Mac OS X 10.4 Package from the MySQL Developer site here.

Be sure to pick the right architecture for your Mac.
Install MySQL

Mount the disk image and run the installer package called “mysql-5.0.XX-osx10.4-ARCH.pkg” or similar. Substitute minor version numbers and “ARCH” where appropriate.

I won’t go through the steps inside the installer, that would be tedious.

At the end of it all you will have a lovely working install of MySQL 5.0 in /usr/local/mysql-5.0.XX-osx10.4-ARCH and a handy symlink to that directory at /usr/local/mysql

Now for the problems.
Problem 1 – The startup item doesn’t work

launchd has killed the old StartupItems folder and functionality, someone should have told MySQL that was coming.

Here is a launchd plist file that you can use in it’s place:
view plaincopy to clipboardprint?

1.
2. 3. "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
4.
5.
6. GroupName
7. mysql
8. KeepAlive
9.
10. Label
11. com.mysql.mysqld
12. Program
13. /usr/local/mysql/bin/mysqld_safe
14. UserName
15. mysql
16.

17.



"http://www.apple.com/DTDs/PropertyList-1.0.dtd">


GroupName
mysql
KeepAlive

Label
com.mysql.mysqld
Program
/usr/local/mysql/bin/mysqld_safe
UserName
mysql



Save the above code into a file at /Library/LaunchDaemons/com.mysql.mysqld.plist

Change it’s ownership to root:

$ sudo chown root:wheel /Library/LaunchDaemons/com.mysql.mysqld.plist

Load the launchd job thusly:

$ sudo launchctl load /Library/LaunchDaemons/com.mysql.mysqld.plist

Stop MySQL and unload the job like this:

$ sudo launchctl stop com.mysql.mysqld
$ sudo launchctl unload /Library/LaunchDaemons/com.mysql.mysqld.plist

Unloading takes a couple of uncomfortable seconds to complete, just be patient.
Problem 2 – The preference pane doesn’t work

Well, that’s just tough. Presumably the preference pane relied quite heavily on the startup item. So with that dependancy broken it’s back to the command line.
Problem 3 – “mysql” not found on command line

I had success fixing this by adding the following line to a new file at ~/.profile

PATH=${PATH}:/usr/local/mysql/bin

Then exit the current terminal window and open a new one.

This may not be the bestest most official way to do it, but it works.
Problem 4 – Built in PHP doesn’t like the MySQL socket

True, the default socket for MySQL and MySQLi extensions on PHP is set to /var/mysql/mysql.sock but the MySQL we just installed creates the sock file in /tmp/mysql.sock

This is not a problem for everyone, most PHP apps don’t use the default mysql settings.

To change this just create a file at /etc/php.ini and put this in there:

[MySQL]
mysql.default_socket = /tmp/mysql.sock

[MySQLi]
mysqli.default_socket = /tmp/mysql.sock

There is no need to copy the whole php.ini.default file over to this location, of course if you already have a php.ini file, then just change or append these settings into that file.

Restart the built in Apache.




http://dev.mysql.com/doc/refman/5.0/en/mac-os-x-installation.html

Saturday, July 4, 2009