Installing a LAMP Stack on your Mac

Installing Apache, PHP5 and MySQL5 on your MacOSX is in fact a very easy task using MacPorts

1.- First of all download and install macports

2.- Make sure you have the latest port tree
Open your terminal and type

sudo port selfupdate

3.- Tell macports to install PHP MySQL and Apache

sudo port install php5 +apache2 +mysql5-server 
the "+" sign before the app names is because we will want this servers to run when starting up the system.

This process will take a while, because macports will download sources and then compile, so go grab a pizza, call your mom or play some xox 360, and be back in around 30 minutes.

4.- Once everything is installed is now time for configuration (yeah i already know you love that part)

Remember that your mac came out of the box with an apache instance installed, we need to make sure it is deactivated in order to avoid conflicts with the macports one. For doing this go to:

 System Preferences -> Sharing -> Web Sharing -> Turn Off
Init MySQL database
sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql
If you want to, you can change your default root password (default is empty)
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
I highly recommend you to change your default root password

5.- To start apache and mysql when your system boots do the following

sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

6.- You need to load PHP module on Apache and tell it how to handle .php files, go to the apache config file /opt/local/apache2/conf/httpd.conf and add these lines at the very bottom of the file

LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php
Restart apache to apply changes
sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start
sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper start

Done!

Your web sharing folder will be at: /opt/local/apache2/htdocs/

and you will be able to open your webserver at http://localhost

If you have experience playing with apache before, remember the config file is located at /opt/local/apache2/conf/httpd.conf

User Comments

to leave a comment.
2011-01-12 10:27 AM
Markos Ibarra said:
OR, if making it an "easy task" is the main priority, perhaps installing

MAMP http://www.mamp.info/en/index.html

will do. No MacPorts needed.
2011-05-17 12:08 PM
German Rodriguez said:
There is even an "easier task" for doing this, and I guess it gives you as much control as macports.

It is called HomeBrew, try it out!