Installning PHP5, Apache2 and MySQL 5 on OSX using MacPorts


I know, its the longest title of a post.. ever. But… This is something I deal with everytime I reinstall one of my macs, and for the sake of saving some time I thought of writing it down here.

I expect you have OS X 10.8 installed (or higher), but this should also work with OS X 10.7.



Before you can use MacPorts you need to install XCode and “Command Line Tools”. You can do this by opening XCode, opening the Xcode menu and choosing “Preferences…” (pic 1). In the new window that opens, choose “Downloads” (pic 2) and select “install” next to “Command Line Tools”.

Pic 1

Pic 1

Pic 2

Pic 2

So, now lets start with installing MacPorts. You can download MacPorts here: There are other options instead of MacPorts, such as Howebrew… however, I prefer MacPorts. Sometimes MacPorts seems to “hang” or “freeze” a bit at the end of the installation, saying something like “Configuring packages”. Just wait for it to finish, it will eventually.


When MacPorts is installed you should preform a self update, just to be sure you have the latest version. Open you terminal of choice, and type

When it finished, you can run the following command if any new ports tree’s were installed:

Installing the ports

Installing whats needed is not really that hard, you can start off with the following command:

Now we are installning PHP5 with a bunch of other applications, such as MySQL support Apache ect. This installation might take a while, so go get yourself a cup of coffee.

3 cups of coffee later, and your installation should be finished. However, we’re not there yet. We still have some configuring left to do.

First off, lets start with adding Apache to the startup of you computer, type the following command:

Now whenever your computer is restarted, apache will get loaded automatically.

Now, we should be able to start Apache… lets try it. Type the following command:

Second, we need to add PHP to the Apache configuration so that it will understand PHP files. Type the following command:

Also, we need to enable PHP in the configuration file. Open the apache configuration file in your editor of choice, mine is Nano:

Go to line 119 (In Nano: CTRL+w then CTRL+t and write 119), and enter the following lines:

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

Why?Well, now we told apache to associate .php file with PHP and .phps files with PHP-Source. Now go to line 234 containing “DirectoryIndex index.html” and add “index.php” to it. Why? Now we are telling Apache that index.php is also an index file for a directory.

Save the file (In Nano CTRL+O) and close it (In Nano CTRL+X). Now lets also restart Apache:

Third, we only installed the MySQL5 client… not the server. If you want the server you should also run:

Also, we need to create a PHP.ini file for PHP and set some settings, type the following command:

When done, go to http://localhost in your browser. You should now see “It works” on your screen. We have no files yet for PHP, but we’ll get to that.

Configuring Apache

Now, eventhough apache and PHP work and all… we still want to make life a little easier. So, lets start editing the htconf.conf file from apache to suite our settings.

First off, create a local sites folder. As of OS X 10.8 these are not created by default in your home directory. Type the following command:

Now, open the Apache configuration file in your editor of choice.

First off, lets make sure apache uses your home folder’s Sites directory. Search for “DocumentRoot” and change it to your home folder. If you are unsure what your home folder is, then open a new terminal window and type:

Your result should be something like:

Now, change DocumentRoot from:


Make sure you exchange “paulp” for your username.

Since we just change the documentRoot folder, we need to adjust some more settings. Search for “<Directory “/opt/local/apache2/htdocs”>” and exchange it from:

<Directory “/opt/local/apache2/htdocs”>


<Directory “/Users/paulp/Sites”>

Now, a few line below, you will find:

Exchange that to:

And do also change the following:


Now all we have left to do is setting the User and Group. Exchange the following:


Make sure you use Your username instead of paulp.

Now we should be fine and dandy… your localhost should work now. Lets have a try, type the following command in your terminal:

Now open the following URL: http://localhost/index.php

If all went well, you should now be able see a result like Pic 3:

Pic 3

Pic 3


Posted in 10.8, Apache, Apple, Development, Mac, MacPorts, OSX, PHP, Tutorials, XCode Tagged with: , , , , , , , , , , ,
2 comments on “Installning PHP5, Apache2 and MySQL 5 on OSX using MacPorts
  1. Ben Abbott says:

    I had Apache2, PHP, MySQL, and Mediawiki all working on 10.8, but now on 10.9 pointing to index.php just loads the contents and displays the text content.

    Have you upgraded to 10.9, and is PHP working for you?

  2. Paul Peelen says:

    Sorry for my late reply, but yes. I got everything working on 10.9. Today, I switches to homebrew since it is more stable and easier to use than macports. Besides, macports screws up my filesystem, whereas homebrew installes everything in its own folders and locations.

Leave a Reply

Your email address will not be published. Required fields are marked *