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.
- Homebrew is already installed on your machine (if not see Homebrew
- You are familiar with MacOSX CLI (Terminal)
- You wish to not use MacOSX bundled PHP
- You are using Apache
- You wish to install mcrypt, and imagemagick
Taken from homebrew-php:
- Type in
- Type in
- Type in
brew tap homebrew/dupes
- Type in
brew tap homebrew/versions
- 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).
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
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:
(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.
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.
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.
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).
This package is a little bit more comprehensive because ImageMagick needs to be installed first before, the PHP module can be installed.
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.
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
brew search php54 to see all the available packages. :)