Setup a local dev environment on Mac OSX Lion

willmot edited this page Feb 3, 2012 · 33 revisions

Before you start you may want to cleanup your previous ports and pear installations.

If you have apache, mysql or memcached running, be sure to stop them now.

###Backup existing configs

Move your configs and databases for later so they don't get deleted when we remove MacPorts.

$ mv /opt/local/apache2/conf/extra/httpd-vhosts.conf ~/Desktop/httpd-vhosts.conf
$ mv /opt/local/apache2/conf/httpd.conf ~/Desktop/httpd.conf
$ sudo mv /opt/local/var/db/mysql5/ ~/Desktop/mysql
$ mv /opt/local/etc/php5/php.ini ~/Desktop/php.ini

###Cleanup old installations

Remove Pear:

$ rm -rf ~/.pearrc ~/.pear

Remove MacPorts and all installed ports:

$ sudo port -fp uninstall --follow-dependents installed

Remove MacPorts:

$ sudo rm -rf /opt/local /Applications/DarwinPorts /Applications/MacPorts /Library/LaunchDaemons/org.macports.* /Library/Receipts/DarwinPorts*.pkg /Library/Receipts/MacPorts*.pkg /Library/StartupItems/DarwinPortsStartup /Library/Tcl/darwinports1.0 /Library/Tcl/macports1.0 ~/.macports

Backup and then delete you existing /usr/local/ directory:

$ sudo mv /usr/local/ ~/Desktop/local.backup/


Install Homebrew:

$ /usr/bin/ruby -e "$(curl -fsSL"

Make sure /usr/local/ is in your PATH, edit / create ~/.bash_profile and add the following:

export PATH=/usr/local/sbin:/usr/local/bin:$PATH


$ brew update

In the future when you want to upgrade installed binaries

$ brew update
$ brew upgrade

Install nginx memcached memcache mysql mcrypt mcrypt-php:

$ brew install nginx memcached memcache-php mysql mcrypt mcrypt-php

Setup MySQL

$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
$ mysql.server start
$ /usr/local/Cellar/mysql/5.5.19/bin/mysql_secure_installation

Set MySQL to launch on startup

$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/mysql/5.5.19/com.mysql.mysqld.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

Copy your existing databases back into place:

$ mysql.server stop
$ rm -r /usr/local/var/mysql
$ mv ~/Desktop/mysql/ /usr/local/var/mysql
$ sudo chown -R $USER:staff /usr/local/var/mysql
$ mysql.server start

Install php-fpm:

$ brew install --with-mysql --with-fpm

Create the php-fpm.conf file

$ cp /usr/local/Cellar/php/5.3.9/etc/php-fpm.conf.default /usr/local/Cellar/php/5.3.9/etc/php-fpm.conf

To set php-fpm to run as user, edit the conf:

$ open -a coda /usr/local/Cellar/php/5.3.9/etc/php-fpm.conf

Add change the user/group from nobody to your user and group:

user = willmot
group = staff

You'll also need to create the log directory

$ mkdir /usr/local/Cellar/php/5.3.9/var/log/

Enable the memcache & mcrypt extensions:

$ open -a coda /usr/local/etc/php.ini



You'll also need to set error_log as php will no longer output errors through Apache

error_log = /var/log/php_errors.log

Then create that file and chown it to your user and group:

$ sudo nano /var/log/php_errors.log
ctrl + x
$ chown willmot:staff /var/log/php_errors.log

Getting php-fpm to work with apache

First you need to get as Lion doesn't ship with it.

$ curl > mod_fastcgi-2.4.6.tar.gz
$ tar xvfz mod_fastcgi-2.4.6.tar.gz
$ cd mod_fastcgi-2.4.6
$ cp Makefile.AP2 Makefile

Edit Makefile:

$ open -a coda Makefile

Change top_dir to /usr/share/httpd and add a line CFLAGS=-arch x86_64. Then:

$ make

Move the extensions into place

$ sudo cp .libs/ /usr/libexec/apache2

Then add the following line to your httpd.conf to load

$ open -a coda /etc/apache2/httpd.conf
LoadModule fastcgi_module libexec/apache2/

Lastly add the following to your httpd.conf to get apache to use php-fpm, change willmot to your user.

<IfModule mod_fastcgi.c>  
	FastCgiExternalServer /Users/willmot/Sites/php5.external -host
	AddHandler php5-fcgi .php
	Action php5-fcgi /usr/lib/cgi-bin/php5.external
	Alias /usr/lib/cgi-bin/ /Users/willmot/Sites/
	<Directory /usr/local/sbin>  
		Options ExecCGI FollowSymLinks  
		SetHandler fastcgi-script  
		Order allow,deny  
		Allow from all  
	    AllowOverride All

You'll also want to setup your httpd.conf

open -a coda /etc/apache2/httpd.conf


User _www
Group _www

To your user, e.g.

User willmot
Group staff

Set ServerAdmin to your email address.

Set ServerName to localhost.

Set DocumentRoot and all other references to /htdoc/ to your Sites folder /Users/willmot/Sites/.

Set DirectoryIndex to index.html index.php.

Copy your old httpd-vhosts.conf file back into place:

$ sudo rm /etc/apache2/extra/httpd-vhosts.conf
$ sudo mv ~/Desktop/httpd-vhosts.conf /etc/apache2/extra/httpd-vhosts.conf

And then uncomment the line in httpd.conf that loads your httpd-vhosts.conf.

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

If you have a users httpd.conf in /etc/apache2/users/willmot-httpd.conf then you'll want to edit that and enable FollowSymlinks and AllowOverrides All.

Restart apache, you'll might also need to stop & start Web Sharing in System Preferences:

$ sudo apachectl graceful

If you get errors in your apache error_log that look like this

(61)Connection refused: FastCGI: failed to connect to server "/Users/matthew/Sites/php5.external": connect() failed, referer: http://local.wordpress/wp-admin/themes.php?activated=true

Then it's because the permissions aren't correct for folder where the fastcgi socket is written, run:

$ sudo chown -R $USER:staff /private/var/run/fastcgi/

You'll probably also want git and svn

$ brew install git svn

###Starting and stopping all this crap


$ sudo apachectl stop
$ sudo apachectl start
$ sudo apachectl graceful


$ /usr/local/sbin/nginx -s stop
$ /usr/bin/nginx


$ php-fpm
$ killall -c php-fpm


$ mysql.server start
$ mysql.server stop


$ memcached -d -m 1024 -M -p 11211
$ killall -c memcached