Installing PHP and Apache on Mac OSX – that was (pretty) easy

March 7th, 2009. Tagged: mac, php, tools

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
    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"
    [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/

    Add one more line so it looks like:

    LoadModule php5_module        modules/
    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)

Tell your friends about this post: Facebook, Twitter, Google+

46 Responses

  1. 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. Oh, MAMP, that sounds easy :) Sounds like XAMPP that I was using on Windows.

    You live, you learn :)

    Thanks Marco!

  3. Hi Stoyan :)
    you have to try this is great web server

  4. 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:
    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. 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.

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

  7. @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. 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. 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. 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. 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. 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. Great tutorial!! The only thing I ran into was file permissions. “You don’t have permission to access /test.php on this server.”

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


    From the httpd.conf file, it seems that apache2 is using


    module to relate to php pages. However, I do not see a pertinent php.ini file anywhere. There were several in many folders like


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

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


  18. 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. 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…

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

  21. Nice tutorial…. thanks !!!

  22. Thanks very much — great and very helpful.

  23. Was very helpful! Thanks so much

  24. 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. 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. 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. @Henry Read what you said and worked great for me. thanks for that, very helpful

  28. 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. [...] Comparte esto:FacebookTwitterImprimirCorreo electrónicoMe gusta:Me gustaSé el primero en decir que te gusta esta post. [...]

  30. In step 11. replacing

    AddHandler application/x-httpd-php .php


    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps

    worked for me.

  31. 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. sweet.. thanks!

  33. Thanks a tonne! You just made the learning curve very easy :)

  34. In my MBP I installed Xammp. There’s an xammp for mac. I read somewhere that you can’t do virtual host in mamp free version so I just decided to install xammp. So far I was able to setup virtual host and fits my needs.

  35. Hi,

    I was looking for a simple tutorial for installing php5 and apache2 on my mac os X lion and found your page awesome !! Thanks

    Jacques from Québec libre

  36. Worked for me, after some head scratching. Following the instructions, including changing the location of my pages in section 9, I had to copy the php.ini as per Thomas Harrison above. Adding DirectoryIndex to include index.php is also useful from Luke on Feb13. Adding the line (or uncommenting the ServerName) and changing it for your own machines name solves the problem of apache failing to restart. ServerName My-mac-mini:80 or whatever yours is called.

    As far as using MAMP or other particular configurations, it is not the issue here. This tutorial gets you up and started. Does what it says on the tin.

  37. Thanks very nice blog!

    Feel free to surf to my blog post: where do bed bugs come from

  38. Hello,
    I have installed everything apache, php, MySQL, and configured my virtual host with few different server names in user directory. Can I create multiple different virtual hosts in my root directory? how can I do it.

  39. I like the valuable info you supply for your articles.
    I’ll bookmark your blog and check again right here regularly. I am reasonably certain I’ll be informed many new stuff right here!

    Good luck for the next!

    My page laser spine institute

  40. With havin so much content and articles do you ever run into any issues of plagorism
    or copyright infringement? My website has a lot of exclusive content
    I’ve either authored myself or outsourced but it looks like a lot of it is popping it up all over the internet without my authorization. Do you know any solutions to help stop content from being ripped off? I’d certainly appreciate

    Here is my page; christmas trees

  41. [...] server on your own mac. Many step-by-step instructions are available on the internet. For instance, Installing PHP and Apache on Mac OSX – that was (pretty) easy is a pretty good article about how to get apache and php run your [...]

  42. It’s really a great and helpful piece of information. I’m happy that you shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.

  43. Hello to every , for the reason that I am
    actually keen of reading this weblog’s post to be updated regularly. It contains nice material.

    Also visit my blog post best mattresses for back pain

  44. Thank you for sharing.

    I’ve just realized that it misses GD that someone mentioned on previous posts.

    For others need this lib, just do: sudo port install php5-gd

  45. I have done this on FreeBSD systems. Compiled from source. I was hoping to find an installer script. I am using Apache w/php on Mac
    already but the installation on my 10.5 does not have adequate gd extensions for my dev purposes. I would upgrade to later OSX but have a boat load of expensive Adobe software that I don’t want to upgrade.

  46. This is the approaching Fallout 4 Beta Keys. With
    this Fallout 4 Generator you will be capable to produce beta keys to use on your
    Xbox 360|Ps3 |or even Computer. Giving you likelihood to
    enter the entire world of the new redesigned Fallout four : Overlooked Get in a beta period.

    We are a big gaming group with lots of keys on every single platform.
    Our keys are coming from different developers these kinds
    of as Obsidian Entertainment who by themselves need beta testers for this sport prior to
    release date. Every single beta keys coming
    from our Fallout four Beta Keys Generator will be
    produced from our databases and uncover the unused valid keys and offer them to you in a one simply click.

    Each and every keys. As we can continually get beta
    keys and feed our databases our programs are constantly
    up-to day.

Leave a Reply