Mac OS X Apache PHP MySQL Configuration

Mac OS build in Apache2 for the default web server. PHP is comes with the standard build as well, but you need to enable it by editing the Apache configuration file. under this path.

/etc/apache2/httpd.conf

and enable the module with the following line

#LoadModule php5_module libexec/apache2/libphp5.so

To enable the module, you only need to un comment it by removing the “#” in front, so the line should look like this

LoadModule php5_module libexec/apache2/libphp5.so

and save the file.

The default document root

/Library/WebServer/Documents

It is easier to open the hidden files using TextWrangler, it is open source and its free.

To start Apache, Go to System Preference panel -> Sharing -> Web Sharing.

or in Terminal

Start Apache

sudo apachectl start

Restart Apache

sudo apachectl restart

Stop Apache

sudo apachectl stop

To test php functionality crate a file with the following php script.

<?php phpinfo(); ?>

To enable Personal Website, create a configuration file under your user folder

/etc/apache2/users/[username].conf

with the following directives

<directory “/Users/[username]/Sites”>

Options Indexes MultiViews FollowSymLinks

AllowOverride All

Order allow,deny

Allow from all

</directory>

replace your username with [username]

Virtual Host Directives

<directory “/Users/[username]/Sites/*/“>

Options Indexes MultiViews FollowSymLinks

AllowOverride All

Order allow,deny

Allow from all

</directory>

NameVirtualHost *:80

<virtualhost *:80>

DocumentRoot /Users/[username]/Sites/[foldername]

ServerName virtualhostname

</virtualhost>

Now our Apache server is ready for the php next we need the database for the dynamic contents. I choose MySQL for my database the following is hot to setup the php to connect to the MySQL server to get data.

First you need to install MySQL

You can obtain the latest version from MySQL web site.

Here is the full documentation from the MySQL site

After the installation you need to start the MySQL server, open terminal and key in the following

Start

sudo /usr/local/mysql/support-files/mysql.server start

Stop

sudo /usr/local/mysql/support-files/mysql.server stop

MySQL socket file

When running MySQL and PHP on the same Mac OS X 10.4 server, you may find that PHP cannot connect to MySQL. When PHP is communicating with a MySQL server on the same host, it uses a socket file to communicate, and looks for it at /tmp/mysql.sock. On Mac OS X Server 10.4, MySQL creates this socket file at /var/mysql/mysql.sock.

Resolution

To resolve this issue, you can either change the location where MySQL creates its socket file, or modify the location where PHP looks for the file. Please note that the first option is less secure than the second. Before delving in, you should also review new information relevant to PHP and MySQL on Mac OS X Server 10.4.4 and later.

To change the location where MySQL creates its socket file, do the following:

Create a config file and put it under /etc folder and the content as follow

[client]

socket = /var/mysql/mysql.sock

[mysqld]

socket = /var/mysql/mysql.sock

and save it as my.cnf

Use the terminal to create the directory for the socket file:

sudo mkdir /var/mysql

sudo chown _mysql /var/mysql

Note: For my self I’ve change the php directives to point to the /tmp/mysql.sock file if your mysql.sock file is not there just copy one from the /var/mysql/mysql.sock file

If you done the above and still not working, check on the error log file there will be more information to help you to resolve the problems.

Default log file location

Apache2 server error log file

/private/var/log/apache2/error_log

php error log

/private/etc/php.ini

set the directives display_errors from off to on, and the error will display on your page.