How to enable PHP and MySQL on OS X Snow Leopard
Since Snow Leopard ships with Apache and PHP preinstalled, there is no need to install third party tools like MAMP to run a local development enviroment. All you have to do is enable PHP in the httpd.conf and install MySQL.
To do so open a terminal window and edit the Apache config file located at /etc/apache2/httpd.conf
Uncomment the line
#LoadModule php5_module libexec/apache2/libphp5.so |
(should be on line 116) by removing the leading #
Restart the webserver with “apachectl restart”
Your apache should now run with php enabled. If you want to check if everything works so far, you can place a php file inside your web directory containing
<?php phpinfo(); |
The result should look something like this
Now that you have PHP running, you most likely want MySQL as well. You can download MySQL here http://dev.mysql.com/downloads/mysql/
Simply run the installation package and make sure to install the MySQL.prefPane as well.
Now you can start MySQL in System Preferences > MySQL
By default the MySQL socket is located in /tmp but PHP is looking for it in /var/mysql/
We can change this by editing the php.ini file.
But first we have to copy the default php.ini.
“sudo cp /etc/php.ini.default /etc/php.ini”
Now open /etc/php.ini and edit line 1216.
It should state:
mysql.default_socket = /tmp/mysql.sock |
Save the file and restart Apache again
Finaly, test if PHP can connect to your MySQL server by creating a file in your web directory conatining
<?php $link = mysql_connect('localhost', 'root', ''); if (!$link) { die('Could not connect: ' . mysql_error()); } printf("MySQL server version: %s\n", mysql_get_server_info()); <?php$link = mysql_connect('localhost', 'root', '');if (!$link) { die('Could not connect: ' . mysql_error());}printf("MySQL server version: %s\n", mysql_get_server_info()); |