Setting up Apache + PHP + MySQL on OSX with Homebrew

Recently I’ve been doing some hacking for Chamilo LMS, a Free Software E-Learning system. For various reasons I had to do this on OSX, which meant setting up a web development environment.

This lead me to MAMP, an OSX bundle with Apache + MySQL + PHP. However MAMP had many weird quirks that forced me to maintain a local set of patches just to get things working.

I finally had the time to look into a saner option, take some notes in the process, and write a blog post.

Important advice. This post enables it.

Important life advice. This post enables it.

A popular alternative was using OSX’s installed Apache with a newer PHP version from Homebrew. This sounded dangerous because you can’t just apt-get install --reinstall apache if you break something.

I preferred a pure Homebrew installation also because:

  • No need for root. Everything is user–writable in /usr/local.
  • Vanilla versions for everything, and easy to keep up–to–date with upstream.
  • Easy to destroy and replicate.
  • Usable, non–root, PEAR.
  • You would brew most of this for a hybrid setup anyway.

I couldn’t find a simple and straightforward resource for this specific setup, so I present you with my tutorial/recipe to get Apache + MySQL + PHP, via Homebrew, running on OSX.

I took these notes on OSX 10.7.5 while building Apache/2.2.26 (Unix) with PHP 5.5.10 and MySQL 5.6.16.

#1- A healthy Homebrew install

First of all you need to install Homebrew. Head over to Homebrew’s homepage for instructions. Even if you already have it, I recommend a health check and an update+upgrade:

$ brew doctor
$ brew update && brew upgrade

Pay attention to whatever that says and follow the instructions if there’s any.

You also want to add Homebrew binary paths to your $PATH, even if the directories don’t exist yet. Add this to .bash_profile or whatever your shell uses:

#2- PHP and all its additional recipes

The PHP recipes are maintained in a different repo, but don’t worry, this is not a duct–taped chaos, it’s properly maintained:

$ brew tap jgonzales/php
$ brew install php55 --with-intl

You might want to add --with-pgsql if you want to work with PostgreSQL. MySQL, Apache and CLI support is built by default.

After the installation there are two quick things to do. First, timezone configuration:
(editing /usr/local/etc/php/5.5/php.ini)
date.timezone = America/Lima (or whatever works for you)

And second, PEAR permissions:
$ chmod -R ug+w /usr/local/Cellar/php55/5.5.10/lib/php

#3- Setting up boring MySQL

Now let’s brew MySQL:
$ brew install mysql

With that done, go ahead and start the server so we can initialize the basic tables, and secure the installation defaults:
$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
$ mysql.server start
$ mysql_secure_installation

Remember: You shouldn’t use sudo, so be careful with your muscle memory!

Clint Eastwood... Yeah.

Mr. Eastwood acknowledges the warning.

#4- Brewing Apache (httpd)

And finally, it’s time to brew Apache. You can do so with:
$ brew install httpd

When that’s done burning your lap, you can start configuring Apache. I chose to keep httpd.conf as pristine as possible and do everything in a VirtualHost.

Remember you need to run this on something other than port 80 because you won’t run httpd as root:

Note that I’m enabling virtual hosts and using my own paths.

If you want to configure Apache differently, you probably know what you are doing. So, get off my lawn you damn kid.

#5- Brownie points: PHPMyAdmin

In my virtual hosts configuration I included some lines for PHPMyAdmin. That’s because I’m old school and I love PHPMyAdmin. You can follow into my wrinkled foot steps with:
$ brew install phpmyadmin

#6- Profit

You can check everything is fine by creating an index.php with a call to phpinfo() inside your configured root from httpd-vhosts.conf.

Make sure OSX’s Apache is not running and start (or restart) the servers:
$ apachectl start/stop/graceful(restart)
$ mysql.server start/stop/restart

Now open go to http://localhost:8000/ and http://localhost:8000/phpmyadmin.

Enjoy your success. Maybe do a victory dance:

This might seem like a lot of work, but it’s fairly quick with this cheat sheet. It’s also very safe and easy to keep updated with brew update && upgrade.

A final note: All the suggestions and instructions that are printed after installing each package can be read again with brew info <recipe>. No need to write them down.

Happy hacking!


Habits and engagement

Nir Eyal — Automatic customers: How to design user habits at WordCamp San Francisco 2012:

I came across this video by Nir Eyal about his model for analyzing and designing user engagement through habits.

Successful services, he says, develop habits through triggers, actions, rewards and investments.

A trigger is some kind of prompt that suggest an action promising a reward, these can be external (Twitter notification) or internal (boredom).

Promising because our brains are wired for the thrill of search, of promise, rather than results. That’s why we keep reloading the Twitter feed hoping for good tweets this time.

Then comes the investment. Asking users to devote effort (following friends) in exchange of further rewards (better content) loads the value and triggers that will get the user to come back: direct messages, replies, suggested content.

Or, simply put: the more you use Twitter, the more triggers and rewards you create, the more you hook yourself.

This is really interesting as it is not limited to technology, the science behind it is actually nothing new. I’m curious to see how this can be applied to Free Software and personal projects.

Check out the abridged version linked above, and the longer workshop as soon as you have the time.


Adopted mindset

This just wrinkled my brain. Be nice to programmers:

In order to be a good programmer I need to adopt a certain mindset. That mindset is slowly making me unhappy.

Programming builds an acutely negative mindset over time. I’m always asking the question “what’s wrong with this?” Positive people are always focusing on “what’s good about this?”

Go ahead and read the full transcript, you might recognize yourself.

There are some other ideas in the original article that linked me to this transcript.


Smartphone sin interrupciones

El año pasado finalmente compré un smartphone y un plan de datos.

Lo primero que descubrí es que todas las aplicaciones vienen con todas las notificaciones activadas. Una locura.

Aún sin notificaciones no es difícil pasarte media hora “jaloneando” la pantalla de Twitter o Facebook, donde terminas viendo solamente el nuevo puntaje de CandyCrush de tus tíos. Y, peor, no haces nada más que interrumpir tu cerebro.

Así que he tomado algunas precauciones:

  • Primero, desactivar todas las notificaciones siempre.
  • Segundo, no unirme a nuevos servicios de mensajería. Todo el mundo está en Facebook (por ende Messenger), o si tienen un Android, en Hangouts.
  • Tercero, desactivar “Añadir iconos al escritorio automáticamente”. Mientras más lejos queden las aplicaciones que distraen, mejor.
  • Cuarto, mantener el escritorio solo con aplicaciones para hacer cosas y no para consumir cosas.
  • Finalmente, apagar el internet a menos que lo necesite o esté esperando algo en particular. Esto además hace que el plan de datos te dure más.

Como cualquier cambio de costumbre, puede que sea difícil darle una oportunidad a vivir “tan desconectado”, pero aún no he tenido ningún problema.

Una última cosa: el aparato este que tienes (no importa el modelo) también recibe llamadas y mensajes de texto. Si es realmente urgente, te van a llamar.

Desenchúfate que siempre hay cosas más interesantes que el feed de Facebook.

Booting Fedora 20 from USB on a black MacBook3,1 (2007)

Here’s a trick that is so obvious, I’m still hitting my head against the desk.

Let’s say you want to install an EFI-friendly distribution on your MacBook. However, your MacBook does not detect bootable USB drives on startup (known bug on old Apple hardware).

I found a blog post that had success rebuilding the ISO without EFI support and installing in BIOS mode, but I had a clear memory of getting this to work a few months ago without doing that. I just couldn’t remember how.

Well, turns out that OSX can boot from USB drives as long as you tell it to do so in advance. You can do this in the “Boot disk/Startup disk” panel in System Settings:

Startup disks panel in OSX
You don’t see me complaining that everything on the web is in English, do you?

You only need to select the drive and close the panel, or reboot directly using that handy Reboot button. So considerate of Apple.

Important: After you install, or try, your EFI friendly live USB you have to go back to the panel and select your OSX drive again, otherwise your MacBook will keep waiting for the (now gone) USB drive for about 30 seconds on startup.

For Debian, the last few times I have just booted with a CD in BIOS mode without problems. After the installation you can configure Debian for EFI boot following this instructions.

I haven’t tried any other distribution on this machine.