Setting up LAMP stack on OS X

Setting up LAMP stack for web development on OS X can be done with 3rd party software like MAMP but as Mac OS X comes with pre-installed Apache and PHP it’s easy to use the native setup. You just need to configure Apache, PHP and install MySQL.

Setup Apache2

Set up the Server Name to localhost to suppress the warning about fully qualified domain name and enable PHP module.

$ sudo vim /etc/apache2/httpd.conf
ServerName localhost:80
LoadModule php5_module libexec/apache2/

Create “virtual hosts” under your Sites. Change the username to your account’s username.

~$ sudo vim /etc/apache2/users/username.conf

  ServerName dev
  DocumentRoot /Users/username/Sites
  VirtualDocumentRoot /Users/username/Sites/%-2/htdocs
  UseCanonicalName Off

    AllowOverride All
    Order allow,deny
    Allow from all

Now Apache serves your projects from your home directory’s Sites folder. Apache will serve files from the htdocs folder like “~/Sites/projectname/htdocs”.

Now just restart Apache and check that it’s running.

$ sudo apachectl restart
$ ps aux | grep httpd

Setup PHP

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

Edit php.ini for easier debugging:

error_reporting  =  E_ALL | E_STRICT
display_errors = On
html_errors = On

Setup MySQL

MySQL can be installed directly from Oracle’s MySQL packages or by using Homebrew.

Install Homebrew

ruby -e "$(curl -fsSL"

Install MySQL using Homebrew

$ brew install mysql

Install the MySQL system tables and have it run as your system user:

$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

Start MySQL and check that it’s running

$ mysql.server start
$ ps aux | grep mysql

Reset the root password. Change the “5.5.27” to your installed version number.

$ /usr/local/Cellar/mysql/5.5.27/bin/mysqladmin -u root password 'YOUR_NEW_PASSWORD'

As we are using the Homebrew package for MySQL and the default php.ini file then PHP is trying to connect to MySQL through the default_socket at /var/mysql/mysql.sock which doesn’t exist as MySQL is using /tmp/mysql.sock. Just change all instances of /var/mysql/mysql.sock to /tmp/mysql.sock.

$ sudo sed -i "" "s:/var/mysql/mysql.sock:/tmp/mysql.sock:g" /etc/php.ini

And you’re done.







5 responses to “Setting up LAMP stack on OS X”

  1. […] the needed software or installing e.g. PHP and MySQL to your local machine. Although setting up the development environment (LAMP stack) in OS X is quite easy there’s also better option, to separate it from your machine and make it more […]

  2. […] started off with this guide. So, the Apache portion. Turns out that OS X ships with its own version of Apache. (As of writing, […]

  3. Werner van Schie Avatar


    It worked for me almost perfectly. I’m on OSX 10.9.2 – Mavericks.

    Two minor notes (prolly changed in the course of time):
    The correct location for Homebrew install is now https://raw.github/mxcl/homebrew/go/install
    The MySQL server version is included in the command line for changing the MySQL root password. Change the location into the correct version number.

    1. Shawn Avatar

      Great instructions! The only unclear part for me is the vhost stuff.

      What is the url you use in your browser if for example you create a “test” directory inside of /Users/username/Sites/?

      do you use http://dev or http://localhost with “test” somewhere in the url?

      Thanks for the guide!

  4. […] the sysadmin side I finally wrote about using PHP-FPM with Apache 2 on CentOS and howto set up LAMP stack on OS X. And as everything doesn’t run on PHP I also played with WebLogic Server and wrote how to […]

Leave a Reply

Your email address will not be published. Required fields are marked *