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
- Log into the mac ports prompt:
$ sudo port Password: MacPorts 1.700 Entering interactive mode... ("help" for help, "quit" to quit) [Users/stoyan] > - inside the Mac ports prompt simply do:
[Users/stoyan] > install php5
- This is it! Give it a bit of time to pull out all dependencies, including Apache2. The rest is just some configuration...
- Start Apache and make it start when you power on the computer next time:
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
- Test that Apache runs fine by pointing your browser to http://localhost/. You should see a page that says "It works!"
- 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]
- 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
- 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. - I'll store all web apps and pages and scripts in a directory called /localhost in my home directory:
mkdir ~/localhost
- 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">
- 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
- 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

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…
March 7th, 2009 at 2:47 am
Oh, MAMP, that sounds easy
Sounds like XAMPP that I was using on Windows.
You live, you learn
Thanks Marco!
March 7th, 2009 at 3:21 pm
Hi Stoyan
you have to try this http://www.lighttpd.net/ is great web server
March 7th, 2009 at 5:37 pm
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.
March 7th, 2009 at 5:56 pm
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.
March 7th, 2009 at 7:40 pm
[...] Excerpt from: Installing PHP and Apache on Mac OSX – that was (pretty) easy … [...]
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 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.
March 9th, 2009 at 5:18 pm
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.
March 31st, 2009 at 1:56 pm
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
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!
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
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.
April 18th, 2010 at 1:22 am
Great tutorial!! The only thing I ran into was file permissions. “You don’t have permission to access /test.php on this server.”
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
/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
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 [...]
February 13th, 2011 at 7:27 am
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”
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
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.
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
any suggestion would be appreciated. when i attempted to view my code in the browser
November 3rd, 2011 at 8:03 pm
webmaster ploiesti…
[...]Installing PHP and Apache on Mac OSX – that was (pretty) easy / Stoyan’s phpied.com[...]…
November 16th, 2011 at 6:56 am
Nice tutorial…. thanks !!!
December 2nd, 2011 at 12:14 am
Thanks very much — great and very helpful.
December 23rd, 2011 at 1:50 pm
Was very helpful! Thanks so much
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.
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.
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?
March 26th, 2012 at 5:07 pm
@Henry Read what you said and worked great for me. thanks for that, very helpful
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?
April 14th, 2012 at 9:35 am
[...] http://www.phpied.com/installing-php-and-apache-on-mac-osx-that-was-pretty-easy/ Comparte esto:FacebookTwitterImprimirCorreo electrónicoMe gusta:Me gustaSé el primero en decir que te gusta esta post. [...]
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.
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.
August 31st, 2012 at 3:51 pm
sweet.. thanks!
October 12th, 2012 at 9:17 pm
Thanks a tonne! You just made the learning curve very easy
January 30th, 2013 at 12:44 am
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.
February 8th, 2013 at 5:09 am
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
March 23rd, 2013 at 9:09 am
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.
April 26th, 2013 at 6:59 pm
Thanks very nice blog!
Feel free to surf to my blog post: where do bed bugs come from
May 17th, 2013 at 9:33 am
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.
Regards
R…