An OS X Drupal development environment with Homebrew

Posted by Jeff Beeman on Wed, 01/16/2013 - 09:55

This is a collection of notes I took while setting up a Drupal-ready Apache / MySQL / PHP environment on OS X using Homebrew and PEAR / PECL. I believe it should work to recreate an environment from scratch, but I've not run through the notes from start to finish on a fresh environment to validate. Hopefully this will help you get your own environment up and running easily!

Note: Mark Sonnabaum's Megalodon might be of interest to you, as well. For me, it involved learning a few too many things, and I also wanted as much as possible in my environment to be handled via a package manager like Homebrew or PEAR.

Update: Per Mark, Megalodon does entirely use Homebrew and Chef, I just misunderstood it. I look forward to seeing what I can do with it, once I dig in and understand it a bit more.

Update: Thanks, @stevepurkiss, for pointing out the typo on the first line for brew tap... That's been fixed!

Update: Thanks, @cashwilliams, for pointing out some issues with the my.cnf and order of packages being installed. I've updated a couple of the instructions below to reflect his findings.

Update: I've added instructions for initial setup of Drupal coding standards for PHP Code Sniffer.

TODOs

  • Write a small script that stops / starts all the services we install. Then, remove the auto-start bits (e.g. launchctl)

Install Homebrew and various packages

http://mxcl.github.com/homebrew/

brew tap homebrew/dupes brew tap josegonzalez/homebrew-php brew install mysql brew install graphviz brew install memcached brew install php53 --with-mysql brew install php53-xhprof php53-xdebug php53-uploadprogress php53-apc php53-memcache

Update ~/.profile to use new PHP

PHP_PATH="$(brew --prefix php53)/bin" export PATH=$PHP_PATH:$PATH

Setup MySQL

unset TMPDIR mysql_install_db --verbose --user=whoami --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp mysql.server start $(brew --prefix mysql)/bin/mysql_secure_installation mysql.server stop mkdir -p ~/Library/LaunchAgents cp $(brew --prefix mysql)/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Setup memcached

ln -sfv /usr/local/opt/memcached/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist

Setup PHP

Create a PHP-specific log area.

sudo mkdir -p /var/log/php sudo chmod -R ugo+rw /var/log/php

Update /usr/local/etc/php/5.3/php.ini with some defaults.

error_log = /var/log/php/php.log date.timezone = "America/Phoenix"

Install additional pear libraries

pear config-set auto_discover 1 pear install pear.phpunit.de/PHPUnit pear install pear.netpirates.net/phpDox-0.4.0

Setup Drupal coding standards for PHP Code Sniffer

pear install PHP_CodeSniffer cd ~/Projects/drupal drush dl --package-handler=git_drupalorg coder sudo ln -sv /Users/jbeeman/Projects/drupal/coder/coder_sniffer/Drupal $(pear config-get php_dir)/PHP/CodeSniffer/Standards/Drupal

Fix sendmail

sudo mkdir -p /Library/Server/Mail/Data/spool sudo /usr/sbin/postfix set-permissions sudo /usr/sbin/postfix start

Update Apache config to load new PHP

/etc/apache2/httpd.conf

Note: You'll want to change [username] to your user name.

/etc/apache2/httpd.confLoadModule php5_module /usr/local/Cellar/php53/5.3.18/libexec/apache2/libphp5.so ...

Virtual hosts

Include /private/etc/apache2/extra/httpd-vhosts.conf Include /Users/[username]/Library/VirtualHosts/*.vhost

MySQL config

Create /etc/mysql/my.cnf

sudo mkdir -p /etc/mysql sudo touch /etc/mysql/my.cnf

Add some default settings:

[client] default-character-set=utf8

[mysql] default-character-set=utf8

[mysqld] default-storage-engine=InnoDB collation-server = utf8_unicode_ci init-connect='SET NAMES utf8' character-set-server = utf8

Performance tuning

max_allowed_packet = 128M innodb_buffer_pool_size = 160M join_buffer_size = 4M tmp_table_size = 64M max_heap_table_size = 64M sort_buffer_size = 6M read_rnd_buffer_size = 4M key_buffer = 16M max_allowed_packet = 16M thread_stack = 256K thread_cache_size = 8 table_cache = 16 innodb_flush_log_at_trx_commit = 2 query_cache_limit = 4M query_cache_size = 128M

Setup a new VirtualHost

Add a vhost file: ~/Sites/Library/VirtualHosts/drupal-demo.vhost

ServerName drupal-demo.localhost DocumentRoot /Users/jbeeman/Sites/drupal-demo Options All AllowOverride All Order allow,deny Allow from all ErrorLog /var/log/apache2/drupal-demo_error.log CustomLog /var/log/apache2/drupal-demo_access.log combined

Add the local domain to /etc/hosts

127.0.0.1 drupal.localhost

References