PHP LAMP Mac FAQ: Can you recommend a good way to develop Mac PHP/LAMP applications on Mac OS X?
When it comes to PHP development on Mac OS X, I've always battled my way through getting Apache, MySQL, and PHP on the Mac to work the way I needed them to. I'll skip most of the details here, but getting Apache and PHP to work involves modifying the Apache configuration files under /private/etc/apache2
or /private/etc/httpd
, and also getting past some Mac/MySQL/PHP socket errors I ran into.
The reason I can comfortably say "I'll skip all the details here" is because I recently started using MAMP for my Mac PHP MySQL development, and I have no plans on looking back to the old days any time soon. In this article I'll demonstrate how I got MAMP to work for my Mac LAMP needs, in particular working through a couple of issues not covered in the MAMP installation docs, and also using MAMP with CakePHP.
Mac LAMP development with MAMP (Mac OS X, Apache, MySQL, and PHP)
First of all, MAMP is a terrific, free all-in-one Mac LAMP/PHP solution. Just download MAMP and install the MAMP folder in your Mac Applications folder, and you're almost ready to start your Mac/Apache/MySQL/PHP project. In fact, if you don't work from the Mac OS X command line (i.e., the Terminal), MAMP might be 100% complete.
Assuming you don't work from the command line, you might be ready to use MAMP. If so, here's what it looks like when you start it:
However, since I do work from the Mac command line I had to make a couple of quick changes to my environment to get MySQL and the CakePHP "bake" script to work properly.
MAMP Apache and MySQL ports
One quick note all MAMP developers need to know: By default MAMP runs Apache on port 8888, and MySQL on port 8889. If you want to change those, you can easily change them through the MAMP GUI. In the GUI, just click the MAMP menu item, then select Preferences, and change the ports as desired.
Mac LAMP - The MAMP MySQL client
If you're going to use the MAMP MySQL client from the command, here's a quick tip that can save you some time. The most important thing to know is that you need to use the MySQL command line client that came with MAMP. In my case, because I already had MySQL installed on my Mac, I ran into a few errors until I realized this.
The MAMP MySQL client is installed in the /Applications/MAMP/Library/bin
directory, so to invoke that client and log into your MAMP MySQL database as the root
user, you can invoke the MySQL client like this:
/Applications/MAMP/Library/bin/mysql -u root -p
Because I don't like typing long commands out like that on a regular basis, I created an alias for this command, like this:
alias my="/Applications/MAMP/Library/bin/mysql -u root -p"
This way, in the future, all I had to do was type this command:
my
and then supply the MySQL root password, and I was in.
Note: I created this alias because 90% of the time I need my other MySQL server running. If you just need to access the MAMP MySQL server, just add the MAMP "bin" directory to your Mac/Unix PATH variable.
The MAMP MySQL root password
That makes for a nice segway to MAMP MySQL important tip #2:
The MAMP MySQL root password is "root"
Solving the MAMP/PHP/MySQL/CakePHP error
Once I got into MySQL, everything about MAMP and CakePHP worked just fine, until I tried to run the CakePHP cake bake
command. This command actually starts up okay, but when I tried to generate the code for a CakePHP controller, I got the following error message:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/private/tmp/mysql.sock' (2) in /Applications/MAMP/htdocs/caketest1/cake/libs/model/datasources/dbo/dbo_mysql.php on line 374
Fortunately -- since I had been down this road with MySQL and Mac OS X before -- solving this problem was no big deal. I just ran the following commands to create a link to the MAMP MySQL socket from the /private/tmp/mysql.sock location it was complaining about, like this:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /private/tmp/mysql.sock sudo chown _mysql /private/tmp/mysql.sock sudo chmod 777 /private/tmp/mysql.sock
Once I ran these commands, the CakePHP cake bake
script worked just fine.
Note: I think I got this error message because I invoked the cake bake
script using the wrong PHP binary(!). I just now noticed that I am using the PHP binary from the /usr/bin
directory. After looking in the config file in /Applications/MAMP/conf/php5/php.ini
, if I had used the php
binary that came with MAMP, I am 99% sure I would not have seen this error.
The MAMP installation and your PATH environment variable
I didn't notice that I was using the wrong PHP binary until now because I thought I had it covered when I changed my Unix PATH to work with MAMP. Unfortunately I didn't do this properly at that time. Here's my story:
Somewhere along the line I realized that I wanted to issue Terminal commands related to Apache, MySQL, and PHP, so I dug around, and thought I found all the MAMP binaries in this directory:
/Applications/MAMP/Library/bin
While most of the MAMP binaries are in this directory, unfortunately the MAMP PHP binaries are not in this directory. I'll skip what I actually did here because it's slightly wrong, but what I should have done is run the following command to make sure I was using all the binaries that come with MAMP, including the php
binary that comes with MAMP's PHP5 installation:
PATH=/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php5/bin:$PATH
That command places the two MAMP bin directories in my PATH before any other directories, and therefore whenever I type mysql
, apachectl
, or php
from the Mac command line, I would have gotten these commands from my MAMP installation directory before the /usr/bin
directory, which is what I want in this case.
Mac LAMP - Other MAMP FAQs
What's the location of the MAMP MySQL socket?
/Applications/MAMP/tmp/mysql/mysql.sock
How do I use the mysql or mysqladmin command line clients?
/Applications/MAMP/Library/bin/mysql -u root -p /Applications/MAMP/Library/bin/mysqladmin -u root -p
How do I connect to MySQL from PHP?
$link = mysql_connect('localhost', 'root', 'root');
What ports are things running on?
Apache runs on port 8888 by default, and MySQL runs on port 8889 by default.
Where is the php.ini file located?
/Applications/MAMP/conf/php5/php.ini
What is the MAMP URL on my system?
Probably this: http://localhost:8888/MAMP/?language=English
What is the PHPMyAdmin URL?
Probably this: http://localhost:8888/MAMP/phpmyadmin.php?lang=en-iso-8859-1
What is the URL of the MAMP phpinfo page?
Probably this: http://localhost:8888/MAMP/phpinfo.php
It's been a while since I last used MAMP - How do I start MAMP?
Look in your Applications folder. Click the MAMP folder, then click the MAMP application icon in that folder.
Know any other great MAMP tutorials?
This Drupal MAMP tutorial is very helpful: http://drupal.org/node/66187
How do I change my MAMP Apache or MySQL ports?
Look in the MAMP Preferences settings, under the Ports tab.
How do I change the MAMP Apache document root setting?
Look in the MAMP Preferences settings, under the Apache tab.
Mac LAMP development - MAMP summary
MAMP is a great free tool for running Apache, MySQL, and PHP on the Mac OS X platform. If I wasn't a Mac Terminal command line user, or if I didn't need to use the CakePHP cake bake
command, the MAMP installation might have been flawless. However, since I did need to work from the Mac command line, I ran into the problems noted above. Once I worked my way through those problems MAMP worked flawlessly, and I'm really indebted to the MAMP creators for creating such a nice tool.