I’ve been using MAMP for some time, unfortunately, it seems like outdated (seldom update), now I’ve switched to XAMPP and it is up to date.

To setup a PHP environment in Mac OS X 10.9:

1. Download XAMPP

You can download the latest version here. Then install it.

Bare in mind that by default, MySQL root user has no password, but will set it later

Installation

Install XAMPP

Once completed, launch it

Complete install XAMPP

Start MySQL server

Start MySQL server

  1. Select Manage Servers tab
  2. Select MySQL Database
  3. Start it

Then close it.

2. Add some PHP projects

Hit Command + Space key, type in terminal then hit Enter key.

Search for Terminal

Create project A

Create a directory to keep all projects _(e.g. public_html)_

1
$ mkdir ~/public_html

Create a project to that directory

1
$ mkdir ~/public_html/project_a

Create a home page for it

1
$ touch ~/public_html/project_a/index.php

then add the following content

index.php

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html>
<body>

<h1>Welcome to Foo Site</h1>

<p><?php echo 'Here you can put dynamic content.'; ?></p>

</body>
</html>

Create project B

For testing purpose, just duplicate the project A

1
$ cp -r ~/public_html/project_a ~/public_html/project_b

Edit the file ~/public_html/project_b/index.php

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html>
<body>

<h1>Welcome to Bar Site</h1> <!-- change the header to differentiate them -->

<p><?php echo 'Here you can put dynamic content.'; ?></p>

</body>
</html>

Done.

3. Setup virtual host

Now we have 2 projects, so we use virtual host (setup different domains) to differentiate them

Navigate to XAMPP directory

1
$ cd /Applications/XAMPP/etc/

Edit the file named httpd.conf, search for httpd-vhosts, you will see the line

1
2
3
4
5
6
...

# Virtual hosts
#Include etc/extra/httpd-vhosts.conf

...

uncomment the line, i.e.

1
2
3
4
5
6
...

# Virtual hosts
Include etc/extra/httpd-vhosts.conf

...

Now will look like

Uncommented vhosts

Navigate to deeper directory

1
$ cd extra/

Then edit the file named httpd-vhosts.conf, and it already come with this content

Default virtual hosts

Remove it and change to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName local.foosite.com
ServerAlias local.foosite.com
DocumentRoot "/Users/username/public_html/project_a"
<Directory "/Users/username/public_html/project_a/">
DirectoryIndex index.php
Options All
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
ErrorLog "logs/local.foosite.com-error_log"
CustomLog "logs/local.foosite.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName local.barsite.com
ServerAlias local.barsite.com
DocumentRoot "/Users/username/public_html/project_b"
<Directory "/Users/username/public_html/project_b/">
DirectoryIndex index.php
Options All
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
ErrorLog "logs/local.barsite.com-error_log"
CustomLog "logs/local.barsite.com-access_log" common
</VirtualHost>

Restart apache server

1
$ sudo /Applications/XAMPP/xamppfiles/xampp restart

Edit the /etc/hosts file

1
$ sudo vi /etc/hosts

Add the following content to bottom

1
2
3
# Virtual hosts
127.0.0.1 local.foosite.com
127.0.0.1 local.barsite.com

Save it.

4. Test it in browser

Open your browser (e.g. Safari), type in the URL local.foosite.com, then you will see

Foo Site

and then change the URL to local.barsite.com, then you will see

Bar Site

5. Add MySQL password for root user

In your browser, type in the URL localhost/phpmyadmin, select a user

Edit root user

  1. Select Users tab
  2. Click on the link

Scroll to Change password section

Change root user's password

Then type in the password you want (e.g. password).

Once completed, when you simply click on any link above, error appear.

phpMyAdmin error

To solve this, just have to edit the file located in /Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php

1
$ sudo vi /Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php

Add in the password you just set

phpMyAdmin new password

Save it and exit.

In your browser, refresh the phpmyadmin page. It should work now.

6. (Add on) Auto start XAMPP on machine boot up

1
2
3
4
5
$ cd /Library/StartupItems
$ sudo mkdir xampp # create xampp directory
$ cd xampp/
$ sudo touch xampp # add a file named `xampp`
$ sudo touch StartupParameters.plist # add a file named `StartupParameters.plist`

Add the content to the files

xampp

1
$ sudo vi xampp
1
2
3
#!/bin/bash

/Applications/XAMPP/xamppfiles/xampp start

(By default, XAMPP will install in the path above, if yours is different, just modify it.)

StartupParameters.plist

1
$ sudo vi StartupParameters.plist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist SYSTEM “file://localhost/System/Library/DTDs/PropertyList.dtd”>
<plist version=”0.9″>
<dict>
<key>Description</key>
<string>XAMPP</string>
<key>OrderPreference</key>
<string>Late</string>
<key>Provides</key>
<array>
<string>Starts Apache and MySQL</string>
</array>
<key>Uses</key>
<array>
<string>SystemLog</string>
</array>
</dict>
</plist>

Change the ownership

1
2
3
4
$ cd .. # go back 1 level up (directory)
$ sudo chown -R root xampp # change the owner of `xampp` directory
$ sudo chgrp -R wheel xampp # change the group of `xampp` directory
$ sudo chmod 755 xampp/xampp # change the permission of `xampp` file to -rwxr-xr-x

Done :)

References: