HomeHowTo › How To Use Apache/PHP/MySQL on Mac OS

How To Use Apache/PHP/MySQL on Mac OS

Mac OS comes with a pre-installed Apache2 webserver and all necessary PHP5 components to host PHP content. Both can be easily activated. If you also install a MySQL server you can turn your Mac into a full webserver.

I originally made this howto for OS X 10.7 (see section A) but since Apple made some changes in 10.8 I’ll address those as well (see section B):

 

A. OS X 10.7 (Lion)

1. Enable The Pre-Installed PHP Library

Open a terminal, cd to the apache2 directory, create a backup-copy of the httpd.conf file (just in case) and then open the httpd.conf in nano:

cd /etc/apache2
sudo cp httpd.conf httpd.conf.backup
sudo nano httpd.conf

In the httpd.conf look for this line and remove the preceded number sign (#) to uncomment it:

#LoadModule php5_module libexec/apache2/libphp5.so

Then leave the editor saving the changes:

<Ctrl+X>
<Y>
<Return>

 

2. Run The Built-In Webserver

Go to [System Preferences] –> [Sharing] and activate [Web Sharing]. Now Apache2 is running and you will be able to access the following two webspaces:

  • Your “personal” webspace: http://127.0.0.1/~username/
  • The computer’s “public” webspace: http://127.0.0.1/

You can open the root folders using the buttons in the System Preferences menu.

  • For your “personal” webspace it is: /Users/username/Sites
  • For the public webspace it is: /Library/Webserver/Documents

Use either of these to store your web content.

 

3. Download And Install The MySQL Server

Download the DMG image (i.e. mysql-5.5.21-osx10.6-x86_64.dmg) from the following site:

http://dev.mysql.com/downloads/mysql/

Mount the image and install MySQL by executing the PKG file (i.e. mysql-5.5.21-osx10.6-x86_64.pkg)

Also install the MySQL.prefPane

Go to [System Preferences] –> [MySQL] and start the MySQL server. When asked, allow incoming connections if the server is meant to be reachable from within the network.

To make the MySQL command line tools usable, the directory containing these tools has to be added to the PATH:

export PATH=$PATH:/usr/local/mysql/bin

Since the MySQL root user is not password-protected initially, you might want to set a password for root. Type the command exactly like this, you will be asked to enter a new password then:

mysqladmin -u root password

Then you can log in to MySQL by typing:

mysql -u root -p

Please note: On Mac OS the “localhost” address doesn’t work for some reason, so you have to use “127.0.0.1” in your PHP mysql_connect()  statement:

mysql_connect("127.0.0.1","root","password") OR die(mysql_error());

 

B. OS X 10.8 (Mountain Lion)

1. Enable The Pre-Installed PHP Library

This is exactly the same procedure as in OS X 10.7.

 

2. Run The Built-In Webserver

This works a little different since the Web Sharing option has been removed from the System Preferences. Now you have to start/stop/restart the Webserver using the Terminal:

sudo apachectl start
sudo apachectl stop
sudo apachectl restart

Another difference is, that there is no personal webspace anymore by default, so you only have the public webspace which is still located in /Library/Webserver/Documents.

If you want to re-activate user specific webspaces, follow these steps:

First you have to create the missing Sites folder within your profile:

cd ~
mkdir Sites

Then you have to cd into Apache’s user directory and create a config file that is named by your short user name followed by the suffix .conf:

cd /etc/apache2/users
sudo nano <username>.conf

Fill the file with this content (substituting username with your actual short user name):

<Directory "/Users/username/Sites/">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Finally restart the webserver. Then you should be able to access the user specific webspace via http://127.0.0.1/~username.

 

3. Download And Install The MySQL Server

This works just like it did on 10.7.

Leave a Comment