FredLawl's Blog

PHP & Homebrew

| Comments

Installing and managing your local machine via homebrew can be a daunting process for some. One minute all is well in the neighborhood, and the next moment—complete disaster. The disaster strikes when trying to set things up for the project you are going to work on the following morning. Hopefully, this blog post will be of use for some, as it may reduce some headaches when setting up your environment.

Assumptions

  1. Homebrew is already installed on your machine (if not see Homebrew
  2. You are familiar with MacOSX CLI (Terminal)
  3. You wish to not use MacOSX bundled PHP
  4. You are using Apache
  5. You wish to install mcrypt, and imagemagick

Installing PHP

Taken from homebrew-php:

  1. Type in brew update
  2. Type in brew upgrade
  3. Type in brew tap homebrew/dupes
  4. Type in brew tap homebrew/versions
  5. Type in brew tap homebrew/homebrew-php

After those commands have been typed in, we are ready for installing PHP. I am a fan of PHP 5.4 because PHP 5.5 has compatibility issues with MySQL (due to the deprecation of MySQL but that is beyond the scope of this post).

PHP

Type in brew install php54 to being the downloading/compiling process. This may take a quick minute. But once that is done, PHP has been installed and is at it’s latest release. In the time of this article it is version 5.4.27.

After the compiling has completed you need to add PHP to your path. This will ensure that when we run PHP it’ll use homebrew’s version. To do this, add this line to your ~/.profile or ~/.bash_profile or which ever dot file adds paths to your shell. export PATH="$(brew --prefix php55)/bin:$PATH"

You can verify your installation by typing php -v into terminal. You should see output similar to:

1
2
3
PHP 5.4.27 (cli) (built: Apr 23 2014 22:13:51)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

Apache

In order for your PHP to work on your local machine, you need to setup Apache to load the php5 module. To do this type the following into terminal:

1
$ sudo nano /etc/apache2/httpd.conf

(If you don’t have nano installed, you can type brew install nano first. If you are a savy-tech, you can use sublime text 2 or w/e editor you prefer.)

Find the line that says, LoadModule php5_module libexec/apache2/libphp5.so. Prepend that line with a ‘#’ so it looks like this: # LoadModule php5_module libexec/apache2/libphp5.so. And then add LoadModule php5_module /usr/local/opt/php54/libexec/apache2/libphp5.so underneeth. This will load the PHP 5.4 module into Apache.

Note: If you wish to install previous or future versions of PHP that line needs to be replaced. For example, had I installed php55 via brew, I would make that line: LoadModule php5_module /usr/local/opt/php55/libexec/apache2/libphp5.so

Save the file.

Type sudo apachectl -e info -k restart to ensure Apache does a full restart, and present you with a message saying so (in case any errors occurred)

Then verify Apache machine has the correct PHP installed. Google how to do that if you do not know how.

Packages

A lot of applications utilize packages that PHP does not inherently come with out of the box. MCrypt and IMagick are popular scripts that are used.

MCrypt

To install type, brew install php54-mcrypt. After that is done, verify it has been installed by typing php -i | grep mcrypt. This will output verifying whether the package has been installed. Restart Apache (look above).

ImageMagick

This package is a little bit more comprehensive because ImageMagick needs to be installed first before, the PHP module can be installed.

Type, brew install imagemagick. Then after the install type, brew install php54-imagick. If all goes well, PHP IMagick mod will be installed. Verify with php -i | grep imagick. Restart Apache.

Done!

After everything has been completed, your environment will have everything it needs to be 100% managed by homebrew. To install other PHP packages, just simply type brew search php54 to see all the available packages. :)

Comments