Part 1: OS X 10.11 El Capitan Web Development Environment
Developing web applications on OS X is a real joy. There are plenty of options for setting up your development environments, including the ever-popular MAMP Pro that provides a nice UI on top of Apache,PHP and MySQL. However, there are times when MAMP Pro has slow downs, or out of date versions, or is simply behaving badly due to its restrictive system of configuration templates and non-standard builds.
It is times like these that people often look for an alternative approach, and luckily there is one, and it is relatively straight-forward to setup.
In this blog post, we will walk you through setting up and configuring the OS X built-in Apache and multiple PHP versions. In the second blog post in this two-post series, we will cover MySQL, Apache virtual hosts,APC caching, and Xdebug installation.
This post was updated 10/22/2015 to reflect OS X 10.11 El Capitan rather than OS X 10.10 Yosemite. If you had this setup working with OS X 10.10 Yosemite, and have upgraded to 10.11 El Capitan, you will need to perform the
httpd.conf changes again as the upgrade process overwrites the file. Also you will have to fix homebrew as outlined below as permissions on
/usr/local directory are modified in such a way that brew will not working 100%.
This guide is intended for experienced web developers. If you are a beginner developer, you will be better served using MAMP or MAMP Pro.
OS X 10.11 El Capitan comes with the Apache 2.4 pre-installed, but there is no longer a Web Sharingpreference pane in the System Preferences.
Not to fret! A simple Web sharing preference pane is available from clickontyler.com. This is a scaled down free version of the more powerful VirtualHostX. VirtualHostX is a pretty UI for controlling Apache, but you don’t really need it.
Another approach is simply to use the provided
apachectl command from your terminal. Using the terminal is a pretty scary prospect for many people, but there’s no need to fear it.
To start up the web server, simply bring up the Terminal (/Applications/Utilities/Terminal) and type:
$ sudo apachectl start
To stop apache you would type:
$ sudo apachectl stop
To restart apache when you have made configuration changes, simply type:
$ sudo apachectl -k restart
-k will force a restart immediately rather than asking politely to restart when apache is good and ready
So start Apache now, and then try to reach your server in a browser by pointing it at your localhost, you should see a simple header that says It works!.
If you cannot reach your site via
http://localhost you might need to add an alias in your
127.0.0.1 localhost. It should exist by default.
The first thing we will want to do is to change the document root for Apache. This is the folder where Apache looks to serve file from. By default, the document root is configured as
/Library/WebServer/Documents. As this is a development machine, let’s assume we want to change the document root to point to a folder in our own home directory. To do this, we will need to edit Apache’s configuration file.
You can use whatever command line editor you are comfortable with, just realize this is a
root-ownedfolder so you will need to use
sudo to be able to edit and save the file. I prefer to use Sublime Text 3with the commandline command
subl, others will probably prefer
vi but If you have never used a command line editor, try
$ sudo nanoapache2/httpd.conf
Search for the term
DocumentRoot, and you should see the following line:
Change this to point to your user directory where
your_user is the name of your user account:
You also need to change the
<Directory> tag reference right below the DocumentRoot line. This should also be changed to point to your new document root also:
In that same
<Directory> block you will find an
AllowOverride setting, this should be changed as follows:
User & Group
Now we have the Apache configuration pointing to a
Sites folder in our home directory. One problem still exists, however. By default, apache runs as the user
_www and group
_www. This will cause permission problems when trying to access files in our home directory. About a third of the way down the
httpd.conf file there are two settings to set the
Group Apache will run under. Change these to match your user account (replace
your_user with your real username), with a group of
User your_user Group staff
PHP 7.0 Additional Changes
Although this is not needed for PHP 5.X, it is needed for PHP 7.0 and as we cover that below it’s safer to just add it now. So scroll down until you see something like:
<FilesMatch "^\.([Hh][Tt]|[Dd][Ss]_[Ss])"> Require all denied </FilesMatch>
Below this paste the following:
<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
Also you must set the Directory Indexes for PHP explicitly, so search for this block:
<IfModule dir_module> DirectoryIndex index.html </IfModule>
and replace it with this:
<IfModule dir_module> DirectoryIndex index.php index.html </IfModule>
Save the file.
Create a Sites Folder
Of course you will now need to restart your Apache with
sudo apachectl restart to pick up the changes you just made to the configuration file.
Now, you need to create a
Sites folder in the root of your home directory. You can do this in your terminal, or in Finder. In this new
Sites folder create a simple
index.html and put some dummy content in it like:
<h1>My User Web Root</h1>.
$ mkdir ~/Sites $ echo "<h1>My User Web Root</h1>" > ~/Sites/index.html
Restart apache to ensure your configuration changes have taken effect:
$ sudo apachectl -k restart
Pointing your browser to
http://localhost should display your new message. If you have that working, we can move on!
This process relies heavily on the OS X package manager called Homebrew. Using the
brew command you can easily add powerful functionality to your mac.
Xcode 7.1 and the Command Line Tools are required so make sure you install Xcode via the Mac App Store first. Launch Xcode and agree to the Terms and Conditions, then install the Command Line Tools with this command:
The full installation documentation can be found on the Homebrew Wiki, but we will cover the essentials. Install the homebrew with the following command in your terminal:
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Just follow the terminal prompts and enter your password where required. This may take a few minutes, but when complete, a quick way to ensure you have installed
brew correctly, simply type:
$ brew --version
You should probably also run the following command to ensure everything is configured correctly:
$ brew doctor
It will instruct you if you need to correct anything.
If you had this setup working previously with OS X 10.10 Yosemite and have upgraded to 10.11 El Capitan, you will get an error concerning the permissions on
/usr/local due to new SIP changes in El Capitan. To address this simply run:
sudo chown -R $(whoami):admin /usr/local
If you already have brew installed, make sure you have the all the latest available brews:
$ brew update
Now you are ready to brew!
Install PHP 5.4, 5.5, 5.6, 7.0
Now we have HomeBrew installed it is a simple procedure to install PHP. We will proceed by installing bothPHP 5.4, PHP 5.5, PHP 5.6, and PHP 7.0 and using a simple script to switch between them as we need. First though we have to
tap into the PHP formulas and run the following commands.
You can get a full list of available options to include by typing
brew info php54, for example. In this example we are just including MySQL support (required for Joomla, WordPress, etc.)
$ brew tap homebrew/dupes $ brew tap homebrew/versions $ brew tap homebrew/homebrew-php $ brew install php54 $ brew unlink php54 $ brew install php55 $ brew unlink php55 $ brew install php56 $ brew unlink php56 $ brew install php70
Of course you can choose which versions to install. You many not need all the PHP versions listed above.
This may take some time as your computer is actually compiling Apache.
NOTE: If you get an error during compilation:
configure: error: Cannot find OpenSSL's <evp.h> you need to reinstall your Xcode command line tools with:
Also, you may have the need to tweak configuration settings of PHP to your needs. A common thing to change is the memory setting, or the
date.timezone configuration. The
php.ini files for each version of PHP are located in the following directories:
5.4/php.ini local php/5.5/php.ini local php/5.6/php.ini local php/7.0/php.inilocal php/
You have successfully installed your PHP versions, but we need to tell Apache to use them. You will again need to edit the
/etc/apache2/httpd.conf file and search for
#LoadModule php5_module. You will notice that this is line is commented out. We can ignore it because this is pointing to the version of PHP that came with OS X. Below the other
LoadModule lines, add this:
LoadModule php5_module local php54 apache2/libphp5.so
This will tell Apache to use PHP 5.4 to handle PHP requests. We will add the ability to switch PHP versions later.
You should also take this time to uncomment the
mod_rewrite.so module definition so that it will be available:
LoadModule rewrite_module libexecmod_rewrite.so
Let’s restart Apache again, now that we have installed PHP:
$ sudo apachectl restart
Validating PHP Installation
The best way to test if PHP is installed and running as expected is to make use of phpinfo(). This is not something you want to leave on a production machine, but it’s invaluable in a development environment.
Simply create a file called
info.php in your
Sites/ folder you created earlier. In that file, just enter the line:
Point your browser to
http://localhost/info.php and you should see a shiny PHP information page:
If you see a similar phpinfo result, congratulations! You now have Apache and PHP running successfully.
PHP Switcher Script
We hard-coded Apache to use PHP 5.4, but we really want to be able to switch between versions. Luckily, some industrious individual has already done the hard work for us and written a very handy little PHP switcher script.
It is useful to have a your own local
bin folder in your home directory. We will create one and setup the
$ mkdir -p ~/bin/ $ curl -L https://raw.githubusercontent.com/conradkleinespel/sphp-osx/master/sphp > ~/bin/sphp $ chmod +x ~/bin/sphp
Update Your Path
Now we want to ensure this script is easily found when we are in the terminal, so we need add our
/Users/your_user/bin directory to our path, as well as putting HomeBrew’s preferred
/usr/local/sbin before OS X’s default
/usr/bin folder. Depending on your shell your using, you may need to add this line to
We will assume you are using the default bash shell, so add this line to a your
.profile (create it if it doesn’t exist) file at the root of your user directory:
Replace your_user with your actual user account. E.g.
At this point, I strongly recommend closing ALL your terminal tabs and windows. This will mean opening a new terminal to continue with the next step. This is strongly recommended because some really strange path issues can arise with existing terminals (trust me, I have seen it!).
You can double-check and ensure your path has been updated to put the
/usr/local paths first with the following command:
$ echo $PATH
Apache Setup – Take 2
Although we configured Apache to use PHP earlier, we now need to change this again to point the PHP module to a location used by the PHP switcher script. You will again need to edit the
/etc/apache2/httpd.conf file and search for the
Brew PHP text that you added earlier. Edit the
LoadModule line, to this:
# Brew PHP LoadModule for `sphp` switcher LoadModule php5_module /usr/local/lib/libphp5.so #LoadModule php7_module /usr/local/lib/libphp7.so
It is important that you remove all other commented out
LoadModule php5_module entries at this point or it will confuse the
sphp switcher. Also the Commented out line for
php7_module is important and required if you are installing PHP7 as it uses a unique PHP module handler.
IMPORTANT: Switch to your PHP 5.4 version with the
sphp switch command :
$ sphp 54
PHP version 54 found Unlinking old binaries... Linking new binaries... Linking /usr/local/Cellar/php54/5.4.45_3... 17 symlinks created Linking new modphp addon... Fixing LoadModule... Updating version file... Restarting system Apache... Done.
Testing the PHP Switching
After you have completed these steps, you should be able to switch your php version by using the command:
$ sphp 55
You will probably have to enter your administrator password, and it should give you some feedback:
PHP version 55 found Unlinking old binaries... Linking new binaries... Linking /usr/local/Cellar/php55/5.5.30... 17 symlinks created Linking new modphp addon... Fixing LoadModule... Updating version file... Restarting system Apache... Done.
Test to see if your Apache is now running PHP 5.5 by again pointing your browser to
http://localhost/info.php. With a little luck, you should see something like this:
Updating PHP and other Brew Packages
Brew makes it super easy to update PHP and the other packages you install. The first step is to update Brew so that it gets a list of available updates:
$ brew update
This will spit out a list of available updates, and any deleted formulas. To upgrade the packages simply type:
$ brew upgrade
You will need to switch to each of your installed PHP versions and run update again to get updates for each PHP version and ensure you are running the version of PHP you intend.
Activating Specific/Latest PHP Versions
Due to the way our PHP linking is set up, only one version of PHP is linked at a time, only the current activeversion of PHP will be updated to the latest version. You can see the current active version by typing:
$ php -v
And you can see the specific versions of PHP available by typing:
$ brew info php55 homebrew5.5.30 (bottled), HEAD https://php.net Conflicts with: php53, php54, php56, php70 local/Cellar5.5.28 (498 files, 51M) Poured from bottle local/Cellar5.5.29_3 (327 files, 49M) Poured from bottle local/Cellar5.5.30 (327 files, 49M) Poured from bottle ...php55: stable
You will see all the versions of PHP 5.5 brew has available and then you can switch to a specific version by typing:
$ brew switch php55 5.5.28
OK, that wraps up Part 1 of this 3 part series You now have a fully functional Apache 2.4 installation with a quick-and-easy way to toggle between PHP 5.4, 5.5, 5.6, and 7.0. Check out Part 2 to find out how to setup your environment with MySQL, Virtual Hosts, APC caching, YAML, and Xdebug. Also take a gander at Part 3to find out how to setup SSL for your Apache Virtual Hosts.
841 total views, 4 views today