You are on page 1of 9

Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoy...

http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-w...

phpied.com
Stoyan's blog about (x)html, ajax, bookmarklets, browsers, css, firebug, javascript, json, mdb2, mysql, pear, performance, php, phpbb, tools, yslow, yui, writing, music,... life and everything. Picassas progressive photo rendering 204 No Content

Installing PHP and Apache on Mac OSX that was (pretty) easy
This posts is one of those "note to self" kinda posts. I just finished installing PHP and Apache on my Mac OS 10.5.6 and though I should document the experience should I (or you) need to do it again.

It could already be there


The default OS install came with goodies like ruby and php already there. So I could use php on the command line already. But it wasn't "hooked" to Apache for proper web development. Also turns out Mac comes with some version of Apache, looks like it's disabled by default, but if it isn't, disable it from System Preferences / Sharing / Web Sharing. Now let's start fresh with PHP5 and Apache 2, ignoring the PHP that's already there.

Mac ports prerequisite


Mac ports makes installing many software packages a breeze on the Mac. If you don't have it already, do set up Mac ports first.

Ready? Set? Go
1. Log into the mac ports prompt:
$ sudo port Password: MacPorts 1.700 Entering interactive mode... ("help" for help, "quit" to quit) [Users/stoyan] >

2. inside the Mac ports prompt simply do:


[Users/stoyan] > install php5

3. This is it! Give it a bit of time to pull out all dependencies, including Apache2. The rest is just some configuration... 4. Start Apache and make it start when you power on the computer next time:
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist

5. Test that Apache runs fine by pointing your browser to http://localhost/. You should see a page that says "It works!" 6. Tell Apache that PHP exists:
$ sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so [activating module `php5' in /opt/local/apache2/conf/httpd.conf]

7. Create a php.ini file (PHP configuration) by copying the default .ini


sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini

8. If you need Apache stuff, like config files, error/access logs, htdocs... look around /opt/local/apache2. The web root for example is /opt/local/apache2/htdocs. I found it kinda convoluted so decided to move the web root to my home directory. So next two steps are optional. 9. I'll store all web apps and pages and scripts in a directory called /localhost in my home directory:
mkdir ~/localhost

1 of 9

1/18/2013 11:45 AM

Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoy...

http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-w...

10. Time to edit the Apache config to tell it about the new location of the web root. Open httpd.conf
sudo vi /opt/local/apache2/conf/httpd.conf

Search for "DocumentRoot" and replace the current value /opt/local... with /Users/[your username]/localhost. The result would be like:
DocumentRoot "/Users/stoyan/localhost"

Then do the same further down in the config where it says <Directory..., so it should read:
# # This should be changed to whatever you set DocumentRoot to. # <Directory "/Users/stoyan/localhost">

11. Apache also need to know that files that end with .php will be handled by the php module, so edit httpd.conf (the same one from the previous step). Search for "php" and you'll find:
LoadModule php5_module modules/libphp5.so

Add one more line so it looks like:


LoadModule php5_module modules/libphp5.so AddHandler application/x-httpd-php .php

12. restart Apache (see below) and you're all done

start/stop/restart Apache
Here's how you start/stop/restart Apache:
$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start $ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper stop $ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart

Or to stop and disable starting up every time you power on:


$ sudo launchctl unload -w /Library/LaunchDaemons/org.macports.apache2.plist

Verify that all is good


You can check that all is good by creating a PHP info script.
echo "<?php phpinfo(); ?>" > ~/localhost/test.php

Now point your browser to http://localhost/test.php. It should give you the php info page (a looong page of PHP-related information) This entry was posted on Saturday, March 7th, 2009 and is filed under mac, php, tools. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

33 Responses to Installing PHP and Apache on Mac OSX that was (pretty) easy
1. Marco Says:
March 7th, 2009 at 2:42 am

This is nice but ehm why not just install MAMP? This is completely effortless and it also gives you MySQL and some other goodies you probably want anyway

2 of 9

1/18/2013 11:45 AM

Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoy...

http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-w...

2. Stoyan Says:
March 7th, 2009 at 2:47 am

Oh, MAMP, that sounds easy You live, you learn Thanks Marco! 3. Ibrahim Says:
March 7th, 2009 at 3:21 pm

Sounds like XAMPP that I was using on Windows.

Hi Stoyan you have to try this http://www.lighttpd.net/ is great web server 4. Karl Swedberg Says:
March 7th, 2009 at 5:37 pm

Hey Stoyan, It might make sense to use the ~/Sites directory, since its already there. Also, for every new web app/site I work on, I create a virtual host. This shell script is fantastic: http://code.google.com/p/virtualhost-sh/ For those who dont mind paying a little $$, MAMP Pro has a nice gui for starting/stopping apache,setting up virtual hosts, and configuring apache, php, MySQL, Postfix, etc. 5. Claude Says:
March 7th, 2009 at 5:56 pm

Would you mind sharing why you wouldnt use the version of Apache and the PHP thats already there.? In my case Apache 2 and PHP 5 already existed and work fine. Thanks. 6. Installing PHP and Apache on Mac OSX - that was (pretty) easy Says:
March 7th, 2009 at 7:40 pm

[...] Excerpt from: Installing PHP and Apache on Mac OSX that was (pretty) easy [...] 7. Karl Swedberg Says:
March 7th, 2009 at 8:17 pm

@Claude, Probably the biggest reason for me is the lack of GD library support in the installed PHP. Had a heck of a time trying to add it and gave up after I heard that Id have to recompile PHP. Too much hassle. MAMP comes with a lot more libraries/modules ready to go. Seems really easy to me. But, hey, if the Apache and PHP that are already there work for you, then stick with them. 8. Claude Says:
March 9th, 2009 at 5:18 pm

Thanks Karl. I havent had a need for graphics generation in my apps. If thats all I am missing then I will stick with the default installation. 9. mitch Says:
March 31st, 2009 at 1:56 pm

Thats a long-winded way to start and stop. Why not just use the apachectl as thats all the wrapper is doing? /opt/local/apache2/bin/apachectl start /opt/local/apache2/bin/apachectl stop And perhaps add the macports bin paths to the default path in your ~/.profile to save typing the full path each

3 of 9

1/18/2013 11:45 AM

Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoy...

http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-w...

time export PATH=/opt/local/bin:/opt/local/apache2/bin:$PATH 10. Leonardo Todeschini Says:


May 20th, 2009 at 10:39 pm

The tutorial is fantastic, Congratulatios! I use MAMP to solve my problems, but this gave us an alternative (very well explained) if we wonder on how to deal with the hidden APACHE and PHP ready installation inside our MacOS X Leopard traps. I had my first experience with a computer at 1986 on an old fashioned MSX, and those were such hard times where we shoud really understand what to do and how to do, nowadays we have everything just done, ready to go, config free. I assume that most of times I prefer to use things that are designed to make my life easier, but the geek spirit deep inside always make me google around to understant and have control of everything I use on this new and exciting IT world. Thanks again for the great step by step well explained tutorial, this made my comprehension much more comfortable, nice job! 11. Tom Harrison Says:
September 27th, 2009 at 3:27 pm

Thanks very much great and helpful. One thing I noticed was that when I installed php5 with macports, the sample php ini files were in /opt/local/etc/php5 and called php.ini-development and php.ini-production, so step 7 would now be
sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

12. enwerce Says:


February 26th, 2010 at 11:05 am

The tutorial is fantastic, Congratulatios! I use MAMP to solve my problems, but this gave us an alternative (very well explained) if we wonder on how to deal with the hidden APACHE and PHP ready installation inside our MacOS X Leopard traps. 13. Steven S. Says:
April 18th, 2010 at 1:22 am

Great tutorial!! The only thing I ran into was file permissions. You dont have permission to access /test.php on this server. 14. Prashant Says:
July 23rd, 2010 at 4:24 am

Hi there, Thanks for the nice article. It was useful. I have a question, hope someone can help answering it. I am using MAC and apache is installed at /opt/local/apache2 From the httpd.conf file, it seems that apache2 is using /opt/local/apache2/modules/libphp5.so module to relate to php pages. However, I do not see a pertinent php.ini file anywhere. There were several in many folders like /private/etc/php.ini /opt/local/etc/php5/php.ini-development

4 of 9

1/18/2013 11:45 AM

Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoy...

http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-w...

/opt/local/var/macports/software/php5/5.3.2_0+apache2+macosx/opt/local/etc/php5/php.ini-development but I moved all of them to some other name and still my php pages were working from browser as long as the apache server was running. So I am kind of perplexed as to which php.ini file is it using but there are none now left with this name. This is important for me because I have to add an extension=db4.so in order to get Berkeley DB working through my php pages. Hence, I do not know where to add this extension or how to tell to PHP where it should try to look for Berkeley DB because otherwise it gives me the error: Fatal error: Class Db4 not found in /opt/local/apache2/htdocs/simple_counter.php on line 3 Thanks in advance, Prashant 15. Bringing Push Notifications Beyond iOS Apps: Take A Look At The Snail Mail Push Alerts Project :: App Advice Says:
January 22nd, 2011 at 6:49 pm

[...] vary by user Prowl iOS app and Prowl account Access to a PHP proxy server (one possible Mac OS X how-to) Arduino and PHP scripts [...] 16. Luke Says:
February 13th, 2011 at 7:27 am

Figured you missed a few bits so Id add them here, its worth changing the Index in your httpd.conf sudo vi /opt/local/apache2/conf/httpd.conf DirectoryIndex index.html changes to.. DirectoryIndex index.php index.html Also I added aliases for apachestart, apachestop, apacherestart in my ~/.bash_profile alias apachestart=sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start alias apachestop=sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper stop alias apacherestart=sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper restart 17. Chirayu Says:
May 5th, 2011 at 11:36 pm

This is great tutorial. But I am lover of using default installation for the PHP and APACHE what is pre configured. It is noted here that using pre-configured PHP gives issues with GD Library. I need to use GD libraries in my most of applications. Can somebody suggest how to install GD library in the present installed PHP and recompile the PHP to use with? It will be the great help for me. Thanks 18. MrNewbie Says:
October 1st, 2011 at 9:36 am

hi i tried following the instruction provide. the install seemed to work fine but i keep getting a forbidden error any suggestion would be appreciated. 19. MrNewbie Says:
October 1st, 2011 at 9:37 am

hi i tried following the instruction provide. the install seemed to work fine but i keep getting a forbidden error

5 of 9

1/18/2013 11:45 AM

Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoy...

http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-w...

any suggestion would be appreciated. when i attempted to view my code in the browser 20. webmaster ploiesti Says:
November 3rd, 2011 at 8:03 pm

webmaster ploiesti [...]Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoyans phpied.com[...] 21. junkiest Says:
November 16th, 2011 at 6:56 am

Nice tutorial. thanks !!! 22. Avinash Pawar Says:


December 2nd, 2011 at 12:14 am

Thanks very much great and very helpful. 23. Prateek Says:
December 23rd, 2011 at 1:50 pm

Was very helpful! Thanks so much 24. John Patrick Says:


March 5th, 2012 at 1:25 pm

Thanks I just used this to set up a new development machine after not configuring one for a while and it worked a treat. Definitely saved a lot of digging around for info. 25. Henry Says:
March 17th, 2012 at 10:19 am

Great tutorial! Keep in mind when changing Directory in httpd.conf that there are 2 different Directory tags: one is Directory / and the other is Directory /path/to/htdocs/. The latter is the one to change. Changing Directory / will cause permission problems. 26. joblima Says:
March 26th, 2012 at 5:04 pm

having problems when setting up apache. When i go to the page localhost/test.php it gives me 403 forbidden anyone know on to fix this? 27. joblima Says:
March 26th, 2012 at 5:07 pm

@Henry Read what you said and worked great for me. thanks for that, very helpful 28. joblima Says:
March 26th, 2012 at 9:12 pm

Im running into an issue when running drupal in my local server. Drupal seems to think php extensions are disabled. Is there anyone who knows a fix or running into a similar problem? 29. Apache y PHP en MacOSX mediante ports El Blog del Azul Says:
April 14th, 2012 at 9:35 am

[...] http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-was-pretty-easy/ Comparte

6 of 9

1/18/2013 11:45 AM

Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoy...

http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-w...

esto:FacebookTwitterImprimirCorreo electrnicoMe gusta:Me gustaS el primero en decir que te gusta esta post. [...] 30. Hirak Says:
May 3rd, 2012 at 10:25 am

In step 11. replacing AddHandler application/x-httpd-php .php with AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps worked for me. 31. desi Says:
August 19th, 2012 at 11:09 pm

thanks i was about to re-install apache but after reading few lines on this page i realized i unchecked the option of web sharing. good stuff here. 32. ram Says:
August 31st, 2012 at 3:51 pm

sweet.. thanks! 33. Rohan Shah Says:


October 12th, 2012 at 9:17 pm

Thanks a tonne! You just made the learning curve very easy

Leave a Reply
Name (required) Mail (will not be published) (required) Website

Author
Stoyan Stefanov ssttoo at ymail com @stoyanstefanov on Twitter

7 of 9

1/18/2013 11:45 AM

Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoy...

http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-w...

Follow

674

JavaScript Patterns

Categories
(x)HTML(5) .net 3d abap Ajax api AWS book bookmarklets BOSS browsers Bulgaria California canvas CMS CSS facebook firebug firefox font-face Fun gmail Google Gears hosting HTTP IE images JavaScript JSON kitsch Life and everything mac marketing mashup mdb2 misc hackery mobile

8 of 9

1/18/2013 11:45 AM

Installing PHP and Apache on Mac OSX that was (pretty) easy / Stoy...

http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-w...

Music mvc mysql netflix News/personal OPML packt PEAR performance php PHP certification phpBB Pixel Ads pov-ray project management SAP SEO smush.it Spam speaking standards Stumbled upon tools Uncategorized Usability Web1.0 WebAudio WordPress writing yahoo ydn yslow yui Zend framework

Learn more...

phpied.com is proudly powered by WordPress, hosted by DH Entries (RSS) and Comments (RSS).

9 of 9

1/18/2013 11:45 AM