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/

###Homebrew

Install Homebrew:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

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

Update:

$ 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 https://raw.github.com/adamv/homebrew-alt/master/duplicates/php.rb --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

Add

extension="/usr/local/Cellar/memcache-php/2.2.6/memcache.so"
extension="/usr/local/Cellar/mcrypt-php/5.3.6/mcrypt.so"

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
enter
$ chown willmot:staff /var/log/php_errors.log

Getting php-fpm to work with apache

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

$ curl http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz > 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/mod_fastcgi.so /usr/libexec/apache2

Then add the following line to your httpd.conf to load mod_fastcgi.so:

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

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 127.0.0.1:9000
	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
	</Directory>  
</IfModule>  

You'll also want to setup your httpd.conf

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

Change

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

Apache:

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

nginx

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

php-fpm

$ php-fpm
$ killall -c php-fpm

mysql

$ mysql.server start
$ mysql.server stop

memcached

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