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
  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

23 Responses to “Installing PHP and Apache on Mac OSX – that was (pretty) easy”

  1. Marco Says:

    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. Stoyan Says:

    Oh, MAMP, that sounds easy :) Sounds like XAMPP that I was using on Windows.

    You live, you learn :)

    Thanks Marco!

  3. Ibrahim Says:

    Hi Stoyan :)
    you have to try this http://www.lighttpd.net/ is great web server

  4. Karl Swedberg Says:

    Hey Stoyan,

    It might make sense to use the ~/Sites directory, since it’s 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 don’t 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:

    Would you mind sharing why you wouldn’t use the version of Apache and “the PHP that’s 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:

    [...] Excerpt from: Installing PHP and Apache on Mac OSX – that was (pretty) easy … [...]

  7. Karl Swedberg Says:

    @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 I’d 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:

    Thanks Karl. I haven’t had a need for graphics generation in my apps. If that’s all I am missing then I will stick with the default installation.

  9. mitch Says:

    That’s a long-winded way to start and stop. Why not just use the apachectl as that’s 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 time…

    export PATH=/opt/local/bin:/opt/local/apache2/bin:$PATH

  10. Leonardo Todeschini Says:

    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:

    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:

    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:

    Great tutorial!! The only thing I ran into was file permissions. “You don’t have permission to access /test.php on this server.”

  14. Prashant Says:

    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
    /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:

    [...] 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:

    Figured you missed a few bits so I’d add them here,

    it’s 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:

    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:

    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:

    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. when i attempted to view my code in the browser

  20. webmaster ploiesti Says:

    webmaster ploiesti…

    [...]Installing PHP and Apache on Mac OSX – that was (pretty) easy / Stoyan’s phpied.com[...]…

  21. junkiest Says:

    Nice tutorial…. thanks !!!

  22. Avinash Pawar Says:

    Thanks very much — great and very helpful.

  23. Prateek Says:

    Was very helpful! Thanks so much

Leave a Reply