Apache already comes pre-installed on OS X but PHP is not enabled by default. To enable PHP we need to edit our Apache config file by opening the Terminal and typing sudo nano /private/etc/apache2/httpd.conf and hitting enter (put in your admin password when it asks.) Type Control+W to bring up the search option and type php5_module and hit enter. Remove the # from the beginning of that line and then type Control+X to exit, Y to save when it asks, and Enter to overwrite the same filename.
Next, we want to create our PHP config file since it isn’t there by default. In the Terminal type cd /private/etc and hit enter, then type sudo cp php.ini.default php.ini and hit enter. This makes a copy of the default configuration we can use to get started with. We’ll use that later. Now you can start up Apache by going to System Preferences, Sharing and checking the Web Sharing box. If it was already checked, uncheck it and then recheck it again to restart Apache to accept our changes.
Now you need to install MySQL. Luckily, there’s already an install package available at MySQL.com so head on over there and download the latest stable version of 5.X. Now extract the zip, open the dmg and run the .pkg installer. I would also recommend double-clicking the .prefPane file for quick access to startup and shutdown commands without needing the Terminal. I don’t necessarily recommend installing the MySQLStartupItem package unless you really need it. In fact, you really shouldn’t leave MySQL or your web server running at all when you aren’t using it.
Once MySQL is installed you need to start it up. If you installed the preference pane (the .prefPane file) you can just go into System Preferences and then click MySQL. From there just click the button to get it started. Now we are going to want to set a password for the root user. To do that just open up the Terminal and type mysql . If it can’t find the mysql command we can add it to your path by editing your bash profile. Just type sudo nano ~/.bash_profile in the Terminal to edit the file, entering your admin password when it asks. Then add a line that looks like this: export PATH=/usr/local/mysql/bin:$PATH and type Control+X, Y, Enter to quit and save your changes. Then you’ll need to reload your bash profile by typing source ~/.bash_profile
Now you should be able to type mysql in the Terminal from any directory to connect to mysql. From the mysql> prompt type the following, substituting yourpassword with your desired password and then hit enter: set password for ‘root’@'localhost’ = password(‘yourpassword’); To make sure it was effective, log out by typing exit; and then enter and log back in again but this time we want to supply our credentials. So type the following to show that we want to log in using the root user and a password: mysql -u root -p
To make sure PHP can connect we need to edit our php configuration to know where the MySQL socket is located. For this type sudo nano /private/etc/php.ini from the Terminal. When the file opens type Control+W to bring up the search box and type in mysql.default_socket and hit enter. After the equals sign on that line enter /tmp/mysql.sock and and type Control+X, Y, Enter to quit and save your changes. Now we need to restart Apache for the change to take effect by going to System Preferences, Sharing and unchecking then rechecking the Web Sharing box. (Or via command-line if you wish.)
Now to install phpMyAdmin just download the latest and extract it into your Sites folder in your user account (/Users/username/Sites/) or the Apache root (/Library/WebServer/Documents/), whichever you prefer. Browse to that folder in the Terminal and type mkdir config to make the config folder and then chmod o+rw config to make it writable. Then browse to the setup folder in your browser, which would be http://localhost/~user/phpmyadmin/setup/ if you installed it in your user’s Sites folder or just http://localhost/phpmyadmin/setup/ if you put it in the Apache root. From there click the New Server button and then Save to take the defaults (unless you know other options you need) and then Save again in the Configuration File settings of the Overview page. Now you should be able to log into your server by going to either http://localhost/~user/phpmyadmin/ or http://localhost/phpmyadmin/ and entering your root username and password. It’s highly recommended that you create another user with fewer privledges to do most of your work, though.
If you also want to be able to use a .htaccess file you’ll need to go to the Terminal and type sudo nano /private/etc/apache2/httpd.conf to edit your Apache config file. Then type Control+W and search for AllowOverride. Change that option from None to All so that it reads AllowOverride All and then type Control+X, Y, Enter to quit and save your changes. Now we need to do the same for each user account. Do this by typing sudo nano /private/etc/apache2/users/username.conf and making the same change. If you aren’t sure what users there are you can type ls /private/etc/apache2/users/ . After you make the changes you’ll need to restart Apache again.
Some of this I did today and other parts of it I did a while ago so if anyone wants to give it a run through and let me know how it turns out that would be great. I know it could use some screenshots but those will have to come later since I was already done with this when I decided to write the instructions out.
My final word on this is to be mindful of security and make sure no one who shouldn’t can access any applications that are running. I would recommend shutting down MySQL and Apache when you aren’t developing to keep risks to a minimum. I’m not going to go into security since I assume that you know what you’re doing but it was worth mentioning again that any servers running on your machine are a potential security risk if you don’t know how to keep it safe.