Mac OSX 10.6 Enabling MySQL, Apache2 and PHP.

by toy

Well I don’t know that I’m the only person on earth who has this problem. But I have some troubles to get MySQl, PHP and Apache2 work on my Macbook. Since I have been a windows user for 10 years, switch to Mac user is quite new for me. I use to install everything with .exe files and it’s dame easy!.

However, after a few months, Mac rocks and also dame easy.

Well, Let’s get started.

First of all, If you installed Mac Server, It’s already bundled with everything, PHP, Apache and MySQL. But If you’re not. Mac Snow Leopard already comes with Apache already you just have to enable it.

Apache


1. First go to System Preferences.
2. And then go to Sharing.
Enable PHP in Mac Snow
3. And click at Web Sharing, the indicator light will go green. That means your apache is now ready to use.
4. You can try this link at your browser http://localhost/~yourusername. If you see the page like below image, that means your Apache is now ready to use.
Apache 2 Mac Snow

Pretty Easy Isn’t it? now next step is PHP, You have just enabled apache, but that doesn’t mean you enable PHP already. Well Mac also integrated with PHP already. You just have to enable it, but in a different way.

PHP



1. Go to terminal
2. Type

1
cd /private/etc/apache2

3. Then

1
sudo mate httpd.conf

4. If you don’t have TextMate you can use TextEdit instead. Or any editor you like
5. Go to line 115 that says

1
#LoadModule php5_module        libexec/apache2/libphp5.so

6. Uncomment it, so it will be

1
LoadModule php5_module        libexec/apache2/libphp5.so

7. This will enable PHP in your computer.
8. Almost there, now go to terminal again, type

1
cd /private/etc

9. Just in case that you’re not in the etc folder
10. PHP is configured by php.ini, and you need that. But, now you have php.ini.default, you just have to change the file name to php.ini. Now type

1
sudo cp php.ini.default php.ini

11. You can now start Apache by going to System Preferences. Click Sharing in Internet & Network. Then place a checkmark in Web Sharing. When Apache starts up, System Preferences should display Web Sharing: On.
12. Create a page called test.php, and type in the following code: Save it in Macintosh ~username/Sites
13. And then test it. http://localhost/~yourusername/test.php.
14. You should get the php info page.

Reference:
http://foundationphp.com/tutorials/php_leopard.php

MySQL



1. Well, I suggest you to download .dmg file of MySQL, because it’s easier to install and it comes with System Preferences panel where you can start and stop MySQL in a click.
2. Install 3 files of MySQL and go to System Preferences, you should see a MySQL icon under section “Other” and open it.
3. Click Start MySQL Server, you should get a green indicator light.
4. Now, your MySQL is ready to use, but it’s not finished yet.
5. Go to terminal and type

1
/usr/local/mysql/bin/mysqladmin -u root password yourpassword

6. This will setup your root password
6. And I also suggest you to create a symbolic link to your the bin directory.
7. Go to terminal again and type. This command will take you to your home user directory.

1
cd

8. Type

1
mate .profile

9. Insert this line of code

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

Reference http://www.tech-recipes.com/rx/2621/os_x_change_path_environment_variable
10. Now the final touch.
One thing changed with Leopard is the socket for Mysql. It moved to /private/tmp, so you may need to configure your php.ini file to point it to the new location.

To do so, open the file ‘/private/etc/php.ini‘, (if no such file exists, then make a copy of ‘/private/etc/php.ini.default‘ naming it ‘php.ini‘) and edit that.

You have two lines to modify:

mysql.default_socket =

becomes:

mysql.default_socket = /private/tmp/mysql.sock

and mysqli.default_socket =

becomes:

mysqli.default_socket = /private/tmp/mysql.sock

of course, from the sharing pref pane, stop the server and restart it and voila!
Reference: http://mymacinations.com/2007/10/28/apache-php-and-mysql-on-leopard/