Update 11/08/12: For an up-to-date guide as to how to install Apache, MySQL and PHP on OSX 10.8 Mountain Lion, I would like to refer you to you Neil Gee's excellent guide.
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/"
<Directory "/Users/myUser/myNewWebroot/">
...
</Directory>
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:
The configuration file should be located at:
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-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:
So, for example:
Also, a symlink should have been created:
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:
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
Thanks for linking to my site. I would like to point out that there is a new site with more up to date information on the subject. It’s also run by me and I’m kepping the new one up to date, the old one is still there but more as a reference.
hey Klaus! alles roger!?`
ich schreib grad das 2.te mal weil dein Formular beim Abschicken meinen Text gekillt hat nur weil ich keine email eingetragen hatte…
Nützlicher Eintrag von Dir hier! Bin grad auch am Mac-Apache hacken… damit ich ein paar htaccess und mod-rewrite Geschichten lokal testen kann. Mamp hat da in der Tat rumgezickt. Bin jetzt übrigens auch zum Coder mutiert… allerdings hauptsächlich Actionscript… Also machs gut! beste grüße, Flo
[...] OS X Apache, MySQL, PHP guide [...]
Thanks for the great guide. I just used it as a reference for writing a guide similar to yours.
Hello, after getting phpadmin up & running, I get the message:
“Cannot load mcrypt extension. Please check your PHP configuration.”
Do you see this and is it of any consequence?
Thanks – any guidance is greatly appreciated.
Cheers,
Dipesh.
Hi, Great guide, PHP works but after installing Mysql and pma I get the same error as @Dipesh and an Access Denied message.
You should try this page
i’ve done it and this problem is solved
http://michaelgracie.com/2008/12/26/plugging-mcrypt-into-php-on-leopard-10.5.6/
but now my problem is to configure phpmyadmin…. root and blank don’t work… maybe because i have not made the link for he path ?! i don’t think…
Hey there,
I tried following your instructions, but ran into a hiccup:
$ sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
That doesn’t run properly. Infact, StartupItems has nothing in it. Every step before that worked without a problem.
Was/is XCode required for all of this to work?
Woops.
Make sure you double click the startup package. That fixed my problem.
For phpMyAdmin — “Extract that package to a directory somewhere in your DocumentRoot.”
I’m on an iMac running OS X 10.5.6. DocumentRoot for me is apparently /Library/Webserver/Documents
What does ” … to a directory somewhere in your DocumentRoot” mean exactly? Do I need to create a phpMyAdmin directory and extract the files there?
Thanks for your help. I’m new at this kind of thing.
@Ed:
Copy the phpMyAdmin directory to /Library/WebServer/Documents/phpMyAdmin if you want it to be accessed by all users, or just put it in your “Sites” folder so only you can access it.
[...] If you want to set up PHP and MySQL I would suggest following this tutorial. [...]
[...] Apache and PHP (and MySQL) on Mac OS X Leopard Apache 2 + PHP 5 + MySQL 4 under OS X 10.3.9 Installing Apache, MySQL and PHP on Leopard apache, install, mysql, os x, php Name Email Website [...]
[...] ten artykuł korzystałem z informacji zawartych na stronie Building Apache, MySQL, and PHP warto też przeczytać Installing Apache, PHP and [...]
[...] Installing MySQL on Mac OS X via klauskomenda.com [...]
[...] Here’s a tutorial on setting up XAMPP and WordPress on your machine. Here are tutorials for Mac OSX, Ubuntu and Windows. I think it’s a good idea to get your local machine set up to run PHP, [...]
[...] Here’s a tutorial on setting up XAMPP and WordPress on your machine. Here are tutorials for Mac OSX, Ubuntu and Windows. I think it’s a good idea to get your local machine set up to run PHP, MySQL [...]
[...] Here’s a tutorial on setting up XAMPP and WordPress on your machine. Here are tutorials for Mac OSX, Ubuntu and Windows. I think it’s a good idea to get your local machine set up to run PHP, MySQL [...]
[...] Here’s a tutorial on setting up XAMPP and WordPress on your machine. Here are tutorials for Mac OSX, Ubuntu and Windows. I think it’s a good idea to get your local machine set up to run PHP, MySQL [...]
[...] http://www.klauskomenda.com/archives/2008/10/07/installing-apache-mysql-and-php-on-leopard/ [...]
[...] Here’s a tutorial on setting up XAMPP and WordPress on your machine. Here are tutorials for Mac OSX, Ubuntu and Windows. I think it’s a good idea to get your local machine set up to run PHP, MySQL [...]
Might want to update that link to “the most recent version of MySQL”, as it points to a very non-recent version (5.0.x, while the current version ATM is the 5.5.x branch).
Cheers!
@Daniel: Thanks for pointing it out. I updated the post with a more generic link to the MySQL download site (instead of having to keep it up to date with every new version).