Configure your local web server with Apache serving PHP in OS X 10.8 (Mountain Lion)

UPDATE: this is tested to work as well in OS X 10.9 (Mavericks)

Sometimes our development set up will have a local web server ready for us, for example with Ruby on Rails you just need to execute rails s in your project folder and then go to http://localhost:3000 to see your application served. But when we want to set up a PHP local development environment, the first that we need to do is set up our own web server.

Note: PHP 5.4 has a built-in web server, but it’ll need some time until it gets supported in most shared servers, so I prefer to stick to PHP 5.3 for the time being.

Since OS X 10.8 Mountain Lion, Web Sharing in System Preferences > Sharing is not present any more. So we need to start it command line:

$ sudo apachectl start

Now if you go to http://localhost you should see a It works! text.

I like to use a specific folder to place all my development projects, rather than use the default location at /Library/WebServer/Documents/. I put them in ~/Dev

Then I can access each folder individually using virtual hosts.

For example I want the ~/Dev/myphpproject folder to be served from http://myphpproject.local

So let’s start by creating that folder:

$ cd ~/Dev
$ mkdir myphpproject

Then you need to add a virtual host configuration file for that folder to /etc/apache2/sites. But first we need to enable name-based virtual host support for port 80 (http). Create the file /etc/apache2/sites/0000_any_80.conf with this content:

NameVirtualHost *:80

This will be the first file picked by Apache when reading all the content files, and is a configuration setting that all of them will need.

Now we need a place where to put the configuration files for our sites. Create /etc/apace2/sites folder if you don’t have it.

Then we can create our new virtual host config file /etc/apache2/sites/myphpproject.conf with this content:

<VirtualHost *:80>
    ServerName myphpproject.local
    DocumentRoot /Users/jesus/Dev/myphpproject
    <Directory /Users/jesus/Dev/myphpproject>
      AllowOverride all              
      Options -MultiViews   
      Options +Indexes   
		  Order allow,deny
	    Allow from all

You can create more virtual hosts creating more files like the last one, and changing the names as desired.

Then we need to add the .local domain to our local hosts file. You can use this one liner:

$ echo " myphpproject.local" | sudo tee -a /etc/hosts

If you didn’t have the sites folder, you probably need to edit /etc/apache2/httpd.conf and add to the end:

Include /private/etc/apache2/sites/*.conf

If you had the folder is probable that you already have that Include already somewhere, but double check.

As you will be using PHP you need to make sure that in /etc/apache2/httpd.conf you have this line uncommented:

LoadModule php5_module libexec/apache2/

Then we can start Apache

$ sudo apachectl restart

To make sure we have PHP being correctly served:

$ echo "<?php phpinfo() ?>" | tee myphpproject/phpinfo.php

Now go to http://myphpproject.local/phpinfo.php (make sure to type the http:// in Chrome or it will look for that in Google), and you should see your PHP config info.

To make sure Apache has the correct permissions to do file operation in our project, we need to change the owner and group of the project folders and files to the ones Apache is using: _www:

$ sudo chown -R _www:_www myphpproject

And then, to make sure the system don’t annoy us all the time asking for password every time we make file changes in the project folder, add your user to the _www group:

$ sudo dseditgroup -o edit -a myusername -t user _www

And let members of the group to write read and execute files:

$ sudo chmod -R 775 myphpproject

Just remember that you will probably need to change those permissions when you upload code to server. As a rule of thumb I’d use something like this for files:

$ sudo find myphpproject -type f -exec chmod 0644 {} \;

and for directories:

sudo find myphpproject -type d -exec chmod 0755 {} \;

And now you are ready to start developing!

4 Responses to “Configure your local web server with Apache serving PHP in OS X 10.8 (Mountain Lion)”

  1. sl1e

    First Thank you!
    When I check to see if php is served correctly i get “!–?php: event not found”.
    apache2 is serving “myproject.local” to (“It works!”) but so far that is all I get.
    I already have a dir with a php website ready to be served and i’m on OSx 10.8.4 and not mountain loin. Hope thats not a big deal.

    • Jesús Carrera

      I think you made a typo. Try:

      $ echo "<?php phpinfo() ?>" | tee myphpproject/index.php

      Mind the opening “<"By the way, 10.8.4 is Mountain Lion

  2. Sl1e

    Hey, Thanks for the set up again!
    It had nothing to do with adding phpinfo to my index file that replaces the contents anyway and i knew it.
    I was missing the closing in my config file for vhosts and directory once i added those everything worked like a charm!

  3. Ritesh Patel

    Dude, great post. Followed it step by step and voila! Have it working :-)



Leave a Reply