Skip to content

Instantly share code, notes, and snippets.

Last active December 21, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-harrison/6254595 to your computer and use it in GitHub Desktop.
Save michael-harrison/6254595 to your computer and use it in GitHub Desktop.
OSX PHP Development Setup
# PHP Installation
# ----------------
# Installed via phpbrew
phpbrew install php-5.3.25 +default
phpbrew ext install openssl
phpbrew ext install mycrypt
# MySQL Installation
# ------------------
# Installed via brew
brew install mysql
# Follow the instructions post install
# Pear
# ----
# Not necessary but good to have. For full details of install see | php
# Composer Installation
# ---------------------
# See full details of composer installation at
curl -sS | php
mv composer.phar /usr/local/bin/composer
# PhpUnit Install
# ---------------
# Add the following to the composer.json for your project:
# "require-dev": {
# "phpunit/phpunit": "3.7.*"
# }
# Go to the root of your project then run
composer install
# or if you have run composer before for the project
composer update

Apache Setup on OSX

Apache is packaged with OSX so it's ready to go more or less out of the box. In versions of OSX prior to 10.8 there was the option to turn this on via Sharing in System Preferences by ticking the Web Sharing option. However in OSX 10.8 this option has been removed. Following are the steps for getting things going in 10.8 and 10.7

Start up the apache server:

OSX 10.8
Open terminal then execute the following:
(NB: You will be asked for an admin password.)

> sudo apachectl start
> sudo defaults write /System/Library/LaunchDaemons/org.apache.httpd Disabled -bool false

The above will start Apache then make it so it automatically starts when you reboot

OSX 10.7
Open System Preferences > Sharing > Check Enable Web Sharing

Create your User Config

Open terminal (aka bash) then execute the following:

> sudo vim /etc/apache2/users/`whoami`.conf

Once the editor is up put in the following configuration making sure to change username with your user name. If you are unsure of what your user name is then execute whoami from bash.

<Directory "/Users/username/Sites/">
     Options Indexes MultiViews FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow from all

Create your website config

We're going to keep the configuration fairly simple but allow for you to easily make more config files without having to use sudo all the time. As a part of the standard setup for OSX you get a folder called Sites in your home directory. If it's not there then create it and apply the appropriate permissions to the directory for Apache.

> mkdir ~/Sites
> chnmod o+x ~/Sites

The second command is worth applying yo your Sites folder to ensure you have the right permissions required by Apache otherwise you will get Forbidden errors with visiting your site. In fact every directory leading to the root of your will need to have these permissions otherwise you'll get a Forbidden error.

So you have a common place for your website config files create a conf folder in site (ie mkdir ~/Sites/conf) then do the following:

> sudo vim /etc/apache2/httpd.conf

Go to the end of the file and the following line (substituting your username for myusername) then save it:

Include /Users/myusername/Sites/conf/*.conf

You're now ready to add your website config:

> vim ~/Sites/conf/my-new-website.conf

Then put in the following changing my-user-name, my-website-root and my-website.local appropriately:

<VirtualHost *:80>
  ServerName my-website.local
  DocumentRoot /Users/my-user-name/Sites/my-website-root/
    <Directory /Users/my-user-name/Sites/my-website-root>
        Options +FollowSymlinks +SymLinksIfOwnerMatch
        AllowOverride All

If the source is in another directory then you can symlink to it. Be aware that all folders leading to the root of the other directory will need to have the appropriate permissions (ie apply chmod o+x to each directory). To do the symlink do the following:

ln -s ~/Site/my-web-site ~/Code/path/to/source

Add a hosts file entry

In order for you to view your local website through your browser using my-website.local you will need to add a hosts file entry.

> sudo vim /etc/hosts

Once the editor opens go to the end of the file and add the following lines my-website.local
::1 my-website.local

Note the first line is IPv4 and the second is IPv6. The IPv6 is required because without it each visit to your site will be delayed by OSX trying to find the IPv6 version then timing out.

Restart Apache

Now you've done doing the configuration you'll need to restart Apache. Should you make any changes your .conf file then you will need to restart Apache. You can do the restart as follows:

> sudo apachectl restart

Now you're done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment