Mountain Lion and the AMP Stack

Another large cat has descended upon the innards of my laptop, and I’ve had to redo my entire development setup. The cat is called Mountain Lion. That's alright, I love the opportunity to clean up. Here is a tutorial on how to create an awesome and reliable Apache, MySQL and PHP development environment on Mountain Lion using homebrew and php-osx.

This time around, I’m not going near MAMP with any sized pole whatever. It works for what it’s meant to do. Sometimes. I’ve, like, straight-up wept, though, because of MAMP. Rather than subject oneself to that kind of penance, a super solid development environment can be had if you’re willing to do just a little bit more than flip a switch. The bit of extra work put into creating a custom AMP stack will make your heart break less and less. It’ll also mean you understand a bit more of what’s actually happening behind the scenes.

Apache - So, do that. Like this.

Mountain Lion comes with Apache, and we won’t replace it. What we will need to do, however, is turn it on.

As Apple makes their flagship OS more and more consumer focused, they continue to remove little things that confuddle the non-developer. One of these things is the “Web Sharing” checkbox in the system preference settings. We have to use a (gasp), terminal emulator to enable it now. So much better.

Open up the terminal emulator that comes stock with OSX. It’s called terminal.

At the prompt, enter the following: bash $ sudo apachectl start

Voi-FREAKIN-la. Apache is on.

Now we’ll want to configure a .conf file that allows us to develop using virtual hosts. This is as easy as easy pie by creating two additional .conf files for custom settings.

Using the terminal, navigate to /etc/apache2/users. Now, create a file called yourname.conf:

  $ cd /etc/apache2/users
  $ touch yourname.conf

This .conf file will be used to trick out our Apache install a bit, amending a few new directives to it. Once the file has been created, open it with your favorite editor. I recommend vim. Stop being a baby:

  $ vim yourname.conf

Add the following lines to yourname.conf, changing “yourname” to be your username:

  <Directory "/Users/yourname/Sites/">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>

  Include /Users/yourname/Sites/httpd-vhosts.conf

Next, create a folder called Sites at ~/Sites (~ means your root directory. So, yourname/). Inside, create a file called httpd-vhosts.conf.

A bit of explanation; the reason I’ve seperated the files yourname.conf and httpd-vhosts.conf, instead of consolidating them into a single file, is that I like to keep a vhosts.conf in the root of my localhost. It’s easy to get to, and I edit it all the time. Do whatever you want. Make three. That’s the nice thing about setting things up yourself.

Finally, add the following to httpd-vhosts.conf:

  NameVirtualHost *:80
  <Directory "/Users/yourname/Sites">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>

  <VirtualHost _default_:80>
    ServerName localhost
    DocumentRoot /Library/WebServer/Documents
  </VirtualHost>

  <VirtualHost *:80>
    ServerName domain.local
    DocumentRoot "/Users/yourname/Sites/domain.local"
  </VirtualHost>

The template at the bottom of the file can be easily copy/pasted for at least a billion local sites.

When working with local sites, it’s always good to alias them with your hosts file. This is achieved by adding a new line to the bottom of /etc/hosts, like so:

  $ sudo vim /etc/hosts

  # Add this, once you’ve opened the file
  127.0.0.1 yourlocalsite.local

Now, restart apache:

  $ sudo apachectl graceful

PHP

Having given up on the dreary sorrow that is MAMP, we might easily use the system default PHP that comes with OSX. Or, we can get really fancy and use a brilliant one line terminal command to install the hotfire. By getting fancy, I mean stupid simple.

Via –> http://php-osx.liip.ch

  $ curl -s http://php-osx.liip.ch/install.sh | bash -s 5.3

This will install PHP 5.3 into /usr/local, so we can ruin it without a care. Also, it comes preconfigured to be awesome. It’ll work with Symfony, XDebug, etc… Read up a bit on php-osx if you’d like to take it further. For instance, it’s easy to extend php.ini with additional .ini files. Again, for the sake of organization.

Now, we need to tell Apache to use this fresh version of PHP. Easy. Navigate back to yourname.conf, and add a single line to it:

  Include /etc/apache2/other/+php-osx.conf

We can also ensure that we’re using this version of PHP from the command line. Add the following to .bash_profile or .zshrc:

  PATH=/usr/local/php5/bin:$PATH

Next, reload your .bash_profile:

  $ source .bash_profile

MySQL

Now, we’ve got the AP, and we need the M. We’ll install MySQL with homebrew. I’ve written a post already that contains instructions on how to install homebrew –> Symfony and MAMP; A Love Story

  $ brew install mysql
  $ mysql.server start

Start mysql with mysql.server start. Now, install MySQL’s system tables:

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

Be sure to read the output of the above, as you’ve got a few options for creating a root user and password. Also, copy a mysql.cnf to /etc/. This will allow you to adjust your development MySQL settings.

  $ cp /usr/local/Cellar/mysql/your_mysql_version/support-files/my-small.cnf /etc/my.cnf

You’ve got a fresh AMP stack now, and sans MAMP. Rejoice.