OS X Mountain Lion 10.8 Set Apache and PHP Web-Server

last updated in Categories , ,

I am using Apple OS X Mountain Lion 10.8.4 on my Macbook pro. For my school project I need to write an app in PHP 5 and Apache. However, I am unable to find the Web Sharing option from the Sharing panel in System Preferences. How do I install Apache and PHP5 in OS X version 10.8.4? How do I enable HTTPD and test my web app?

Apple removed the web-sharing option from the Sharing panel in System Preferences in OS X version 10.8.x. But worry not, Apple still ships Apache+PHP5 and it is disabled by default for security reasons. In this tutorial, you will learn how to enable and run Apache server with PHP 5.


Configuration files

  1. Apache configuration file – /etc/apache2/httpd.conf
  2. PHP 5 configuration file – /etc/php.ini

Finding default Apache DocumentRoot

Open the Terminal and type the following command:

grep --color DocumentRoot /etc/apache2/httpd.conf


grep --color '^DocumentRoot' /etc/apache2/httpd.conf

Sample outputs:

DocumentRoot "/Library/WebServer/Documents"

You need to store or create php/html/images/js/css app files at DocumentRoot i.e. /Library/WebServer/Documents directory.

How do I start / stop / restart Apache server?

Type the following command to start / stop / restart apache web-server in OS X:

sudo apachectl start
sudo apachectl stop
sudo apachectl restart
sudo apachectl graceful

Test your setup

Fire a browser and type url:
Sample outputs:

Fig.01: Running Apache for the first time
Fig.01: Running Apache for the first time

How do I change the default index.html?

Type the following command to create or edit the index.html file:
sudo vi /Library/WebServer/Documents/index.html
Sample code:

	<title>My first html page</title>
<h1>Hi, User</h2>
<p>This is a test web-page. It was created on OS X 10.8 Mountain Lion using the following:</p>
	<li>Vim text editor</li>
	<li>Apache 2.x</li>
	<li>PHP 5.x</li>
	<small>&copy; 2013 nixCraft.</small>

Save and close the file. Fire a browser and type url:
Sample outputs:

Fig.02: Updated index.html file
Fig.02: Updated index.html file

Configure and Turn on PHP 5 support for Apache

Edit the file /etc/apache2/httpd.conf
sudo vi /etc/apache2/httpd.conf
OR better try vi/vim syntax to open file and go to line called php5_module:
sudo vi +/php5_module /etc/apache2/httpd.conf
Find and make sure the following line is uncommented:

LoadModule php5_module libexec/apache2/libphp5.so

Sample outputs:

Fig.03: Enabling php5
Fig.03: Enabling php5

Type the following command to restart apache web-server in OS X:
sudo apachectl -k restart
sudo apachectl restart
Create a test php script as follows:
sudo vi /Library/WebServer/Documents/phpinfo.php
Add the following code:

 //Display the configuration info, line, php.ini location, build date, Web Server, System, Version and more.

Type the following url:
Sample outputs:

Fig.04: Testing PHP 5 in OS X v10.8.x
Fig.04: Testing PHP 5 in OS X v10.8.x

How do I start/reload Apache on boot?

Make sure Apache+PHP5 get reloaded when you reboot the system:
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

And, there you have it, the Apache and PHP 5 installed, configured and working correctly on the latest version of Mac OS X 10.8.


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

7 comment

  1. The better option is to install a virtualization product and run your development code there. I would check out virtualbox and vagrant.

  2. Hi, I have been struggling for a while trying to configure my MAMP, I’ve work with Linux before and I manage to configure my LAMP pretty well. But MAC is ginving me a hard time. What I’ve could do so far is starting the apache serve although i dont know if the apache server I started is the one that i am seeing on my browser, that because when i go to localhost i see the It Works! message, but if i change the index.html in /Library/WebSever/Documents/ and reload the browser or open another browser and search the address localhost, I am still getting the same It Works message and not the one i’d change. I have try different browsers, clearing cache, incognito windows. I getting crazy here. I would appreciate some help

  3. Followed your tutorial exactly as written.
    However, when I run the url http://localhost/phpinfo.php all that displays is:

    Seems for some reason either Apache and/or my localhost are not executing the php code but just displaying the contents????

    Is a chmod required??
    Have thrashed a bit on this with no luck.

    Any thoughts???
    I am running Mac OS X 10.8.5 on a MacBook Pro.
    I am using Google Chrome, Safari, and Firefox as browsers for testing.


  4. Hey there,
    This worked well. There are a bunch of instructions out there for using PHP + Apache on Mac OS X with their default install libraries (or XCode or whatever installs it), and they all seem dated.

    This one worked perfectly. Very easy.


    Still, have a question? Get help on our forum!