Setup Apache, PHP, and Mysql in Mac OSX Leopard

I don’t consider myself a web developer by any means. But I do have multiple web sites and I like to play around with the latest web applications (WordPress, Concrete5, Drupal, etc) that come out. I have learned over the years not to use my live servers for testing or playing around. So I have setup a test environment on my local Mac OSX system. I want to show you how I setup Apache, PHP, and Mysql on my Mac (Some refer to it as MOXAMP or MAMP) for handling the development of my websites.

One of my favorite features in Mac OSX Leopard is the inclusion of Apache and PHP. It helps in getting a environment set up pretty quickly. The only thing that is not included but very important is Mysql. By default Apache and PHP are inactive and need to be enabled by you.

Most of the examples I will show you will be from the command line. I find once you get the hang of working from the command line, everything else is a peice of cake (Mmmmm cake).

PHP

To enable PHP we will need to edit Apache’s configuration file.The file is called httpd.conf and can be found in /etc/apache2/

sudo vi /etc/apache2/httpd.conf

If you need help with vi check out this link

Note: Every time you use the <code>sudo</code> you will most likely be prompted for your system password. Just type it in and hit ‘Return’

Find the following line:

#LoadModule php5_module libexec/apache2/libphp5.so

We need to uncomment out this line. This is done by removing the # sign. It should look like this afterwards:

LoadModule php5_module libexec/apache2/libphp5.so

Save the file and quit.

By default, Leopard has an empty configuration file but provides a file that can be used as a template. From a new terminal window, type:

sudo cp /etc/php.ini.default /etc/php.ini

This creates your php.ini configuration file, located in /etc/php.ini.

PHP is now enabled. We can move on to Apache.

Apache

To start up Apache click on your System Preferences and go to the “Sharing” preference pane and enable “Web Sharing”:
picture-1
This can also be done from the command line using the apachectl command:

sudo apachectl start

If you need to restart Apache, try this:

sudo apachectl restart

Anytime you make changes to Apache’s httpd.conf or PHP’s php.ini it will require a restart of Apache for the changes to take effect.

Now we can test to make sure PHP works. Create a file (using vi or your favorite text editor) called test.php in your /Users/<username>/Sites/ directory. <username> is the name of the user you’re currently logged it as. In the file, insert the following:

<?php phpinfo(); ?>

Save the file.

Now, open your web browser and type in the address http://localhost/~username/test.php

You should see something like this:

phpinfo

MYSQL

For this part of the installation you will need to head over to MySql’s website and download the latest dmg file.

After downloading the file, mount it and run the installer. After the installation you will was to double-click the MYSQL.prefPane icon. This will install the MYSQL preference pane in the System Preferences, making it easier to stop and start the services.

After you’ve done both, head over to your ‘System Preferences’ and click on the ‘MySQL’ icon and click start.

picture-6

You can also start MySQL from the command line:

sudo /usr/local/mysql/support-files/mysql.server start

To stop MySQL:

sudo /usr/local/mysql/support-files/mysql.server stop

Configuring MySQL for PHP

The next thing we need to do is make PHP and MySQL aware of each other. In order to do that we will need to change the location of the mysql.sock. The default location of mysql.sock is in the /tmp/mysql.sock directory. This needs to be changed to /var/mysql/mysql.sock as this is where PHP expects it.

Create a my.cnf file in the /etc folder

sudo vi /etc/my.cnf

Insert the following code:

[client]
socket = /var/mysql/mysql.sock

[mysqld]
socket = /var/mysql/mysql.sock

Instead of moving the mysql.sock from /tmp to /var/mysql we will create a link. This is to avoid any possible problems that moving the file could cause with MySQL.

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

That’s pretty much it. There are some things I didn’t cover in this post, like PHPmyadmin and Virtual Directories, but I will cover at a later time. I wanted to give you the least amount of steps needed to get up and running.

Before you start installing and applications I would recommend setting a password for MySQL’s root user. By default this is blank.

To change the password:
$ mysqladmin -u root password NEWPASSWORD

Resources

Here are some links I would recommend checking out. Many of them only covering one specific component of the Apache, PHP, and MySQL setup.

Installing MySQL on Mac OS X – MySQL’s guide to installing on OS X

Foundation PHP

Apache’s httpd documentation

PHP.net

Share and Enjoy:
  • Digg
  • Technorati
  • Facebook
  • Mixx
  • Google Bookmarks
  • StumbleUpon
  • Yahoo! Buzz
  • Reddit

Leave a Reply

Additional comments powered by BackType