Setting up PHP, MySQL, and Apache in Mac OSX Leopard

One of the best features for web developers in Mac OSX Leopard is the inclusion of PHP and Apache. By default both are inactive and require you to get them ready for business. I will outline how to do this as well as get MySQL up and running so you can have a local development setup right on your Mac!

It’s all pretty easy to do but requires a little attention to detail in parts. So lets get started.


Most of the files we’ll be working with are actually hidden from Finder. If you use a text editor like Coda, BBedit, Text Wrangler, or TextMate you can choose to have hidden files “shown” so you can find them in your file structure. I personally use Coda so all that you have to do is click on the “View” menu and choose Show Invisible Files, as illustrated below:

Coda: Show invisible Files

You can also run this simple Terminal command to “show” all hidden folders in Finder:

defaults write com.apple.finder AppleShowAllFiles TRUE

Then restart Finder by typing the following:

killall Finder

You should now be able to see all hidden files.

PHP

To enable PHP browse to Apache’s http.conf file located in /etc/apache2/httpd.conf

Find this line:

#LoadModule php5_module libexec/apache2/libphp5.so

All you need to do is uncomment the line by removing the hash # symbol.

LoadModule php5_module libexec/apache2/libphp5.so

Now save http.conf. Don’t be alarmed if it asks for your system password… this is normal.

PHP 5.2.6 is the version shipped with Leopard. All the most popular extensions are already activated so you should be good to go. However if you need some of PHP’s extended features you may want to consider rebuilding PHP from source code as I’ve read the version that ships with Leopard has been problematic for some. I’ve never had any problems myself but it’s something to look out for. All relevant PHP settings are in the php.ini file. I won’t be covering any tweaks to that file in this article but you should be aware that it exists. By default, Leopard has an empty configuration file but provides a file which can be used as a template. From a new terminal window, type:

sudo cp /etc/php.ini.default /etc/php.ini

This creates your php.ini file, which is located in /etc/php.ini for your future reference.

You’ve now enabled PHP so lets move on to Apache.

Apache

Our friends at Apple were kind enough to ship a very modern version of Apache 2.2.6 for our development work. Kudos! To start up Apache click on the “Sharing” preference pane in System Preferences and enable “Web Sharing” like so:

Enable Web Sharing

You can also use the following command the start Apache from Terminal:

sudo apachectl start

You’ll be prompted for your system password. Type it in and press enter.

If you need to restart Apache, this will do the trick:

sudo apachectl restart

Keep in mind any editing you do to the http.conf or to the php.ini will require a restart of Apache. You can use the above command or simply restart “Web Sharing” in System Preferences… it’s up to you.

Testing PHP

So now hopefully if everything has gone according to plan, we’ll see some fruits of our labor. Go back to the “Sharing” Preference Pane in System Preferences and click on the URL/ IP Address below “Your computer’s website.” If all goes well you’ll see a page that says “Test Page for Apache Installation.” You can also type http://localhost/ in your browser which will take you to the same place.

Now in your text editor create a PHP file and type the following code:

<?php phpinfo(); ?>

Save it in /Library/WebServer/Documents/ (start from the top level directory of your hard drive, not the Library directory in your home directory) with the name test.php.
Now type http://localhost/test.php in your browser and you should see something like this:

PHP Info

Great success!

Setting up a personal website and virtual hosting

If you’re the curious type and tried to click on “Your personal website” in the Web Sharing panel you may have noticed that you were greeted with a “Forbidden” message. So lets go ahead and set it up so you can serve up files from your “Sites” folder. Open up your text editor and create a file named after your “Home” folder. In my case it would be StevieBenge.conf. This file will live in /etc/apache2/users/. You’ll need to replace StevieBenge with the name of your “Home” directory. I’ve heard reports that this file is created automatically on new installs of Leopard but I had to create mine manually. Add the following code to the .conf file:

<directory "/Users/StevieBenge/Sites/*/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</directory>

Like above, replace StevieBenge with the name of your “Home” directory. Save the file. This will allow Apache to serve files out of your “Sites” folder. Sweet! You’ll also need to restart Apache for this to take effect.

Moving right along, lets set up Apache for virtual hosts. Virtual hosts are essential if you want to serve multiple websites from your “Sites” folder. You can choose pretty much any “domain” name you want or mirror any production sites you have hosted on a live server. I like to name all of my local development sites with the extension .dev to keep easy track of things. Getting virtual hosts to work in Leopard was a huge stumbling block for me because of a lot of misinformation I read around the internet. I was was really happy to finally get this working. And as you’ll see it’s pretty straightforward.

So to get started, browse to /etc/hosts and add the following to the end of the file:

# Local aliases
127.0.0.1 steviebenge.dev

Remember to change steviebenge.dev to the name of a site in your “Sites” folder. 127.0.0.1 is an IP Address that tells your Mac that your site is hosted locally. The site will only be accessible on your Mac. Now we’ll need to reopen the.conf file we created earlier in /etc/apache2/users/:

<directory "/Users/StevieBenge/Sites/*/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</directory>
 
NameVirtualHost *:80
 
<virtualhost *:80>
    DocumentRoot /Users/StevieBenge/Sites/steviebenge.dev
    ServerName steviebenge.dev
</virtualhost>

Again make sure to change the directory structure above to reflect your “Home” folder and the folder within your “Sites” folder you want to serve files out of. Restart Apache for the changes to take effect. So to test it out and make sure everything’s working properly, go ahead and move the test.php created earlier into your new directory in your “Sites” folder. If it’s successful you’ll again see:

PHP Info

Too add more virtual hosts run through the above steps again. Simple as that!

MySQL

Now here is where the fun begins. It’s odd that Apple didn’t include MySQL with Leopard as it’s pretty much synonymous with PHP. Anyway, you have a couple options for installing MySQL on Leopard. You can roll your own and compile and install from source code or use MySQL’s package installer for Leopard. I won’t discuss how to compile MySQL here but I did find an excellent tutorial on how to do this over at Dan Benjamin’s Hivelogic site. I came pretty close to giving this a shot but I opted for the MySQL package installer. Dan brings up many salient points on why it’s beneficial to compile your own install in his tutorial, so I’d highly recommend checking it out.

So head over to the MySQL downloads page and grab a copy of the Mac OS X 10.5 (x86) installer (currently 5.0.67). You can also optionally download the MySQL GUI Tools, which I would recommend as that is how I prefer to administrate MySQL. The Mac Installer is labeled MAC OSX 10.4 (Universal binaries) but, in my experience, it will work without issue on Leopard.

Install MySQL and the GUI Tools. There’s also a Preference Pane that you can install that allows MySQL to be stopped and started from System Preferences. Go ahead and install that as well.

After you install the MySQL Preference Pane, find it in System Preferences and start MySQL like so:

MySQL Preference Pane

If you prefer starting MySQL from Terminal, you can do so with this command:

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

And you can use this command to stop MySQL:

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

Now we’ll need to put the mysql.sock in the proper directory so MySQL can communicate with PHP.

Getting MySQL and PHP to communicate

The default location of mysql.sock is in the /tmp/mysql.sock directory. We’ll need to change this to /var/mysql/mysql.sock as this is where PHP will look for it.
So first off, create a my.conf (or in this case my.cnf) file in your text editor and save it as my.cnf in the /etc folder with the following code:

[client]
socket = /var/mysql/mysql.sock
 
[mysqld]
socket = /var/mysql/mysql.sock

Next move mysql.sock to it’s new directory by typing the following code in a Terminal window:

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Believe it or not, that’s pretty much it. One thing to keep in mind is the MySQL GUI Tools will still look for the mysql.sock in the old location so when you log in to MySQL Administrator enter /var/mysql/mysql.sock in the “Connect Using Socket” box under More Options:

MySQL Administrator

Now you’re ready to create your first MySQL Database. The MySQL Administrator makes this really easy, which I will cover in a future article. You can also administer your MySQL databases from Terminal. As I stated earlier, I prefer the GUI Tools but learning how to do things from Terminal is a useful skill to have. It’s totally up to you.

Bonus Information

I’ve read that you can also deal with the mysql.sock issue by editing the mysql.default_socket = line in your php.ini file to reflect the location of mysql.sock. I chose not to do this, but it may work for others

You may want to to change php.ini to report all PHP errors while you are in development. This can be done locating the following line:

error_reporting = E_ALL & ~E_NOTICE

And changing it to:

error_reporting = E_ALL

This is fine in your local development setup but of course not advised for a production server.

At the beginning of this article I illustrated how to “show” hidden files in Finder. Here’s that Terminal command again:

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

You can also “hide” those same hidden files with this command:

defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder

Another great resource for an all-in-one development setup is MAMP. Using MAMP negates the need to do any of the above tweaking as it has its own versions of Apache, PHP, and MySQL. It’s perfect for those that want to get up and running quickly with a minimum of fuss.

Conclusion

Well this should get you started with PHP, MySQL, and Apache in Mac OSX Leopard. My inspiration for writing this article was to combine a lot of the information I read from other sites while getting my MacBook Pro set up. Many of the articles I came across were written around the time Leopard was released last Fall so some of the information was a bit out of date. Not that any of the above methods are terribly original on my part, but I’d like to credit some of the sources that helped me:

procata.com: This was the best resource I found and well worth checking out as it shows you how to install the PEAR extension. Has some great tips that I didn’t cover here.

Installing MySQL on Mac OS X: MySQL’s own how-to guide.

adactio.com: Jeremy Keith’s guide.

foundationphp.com: Excellent screen shots with this PHP and Apache tutorial.

Thanks for reading and check back as I’ll be writing more articles about working with PHP, MySQL, and Apache. Feel free to keep the discussion going in the Comments below.

128 Responses to “Setting up PHP, MySQL, and Apache in Mac OSX Leopard”

  1. April

    Stevie,
    I feel like such a goofball, but I can’t get this to work. I’ve tried so many times, it’s crazy. No matter what I do, I get to the virtual hosting part and get a “can’t find the server” or “can’t find local host” every time. I updated to Leopard, so I’m wondering could it be older apache files are messing me up? I also have mysql already installed, though its not running yet cause I haven’t been able to get beyond running virtual hosts. Help. I’m wondering if anyone else has this problem and if you’ve come across a solution? Thank you for this post, the descriptions are fantastic.
    April

    Reply
    • tomasio

      After some time w/o working /w php I had to locally test some changes and I simply could not find the correct Adress to display the customer’s website in my browser. Searching this page made saved my a**. THX :)

      Reply
  2. Stevie

    Hi April. Thanks for stopping by. I would recommend getting MySQL up and running first and then deal with the Virtual Hosts. When I was setting up my G5 at work I had a really difficult time getting the Virtual Hosts to work as it had previously been running Tiger. I had no such issues on my MacBook Pro though.

    An important detail to not overlook is anytime you make any changes to Apache files, remember to restart Apache to activate the changes. It’s an easy thing to forget and the root of many frustrations.

    If you have any specific questions, feel free to use my contact form and I’ll do my best to help you out…

    Reply
  3. Stevie

    Thanks… glad people are getting some use out of it. I had a tricky time getting things all squared away so I’m more than happy to share. Thanks for stopping by…

    Reply
  4. leopardnewbie

    Hi Steve,

    Could you sense what could be wrong, when my MySql does not connect after I restarted my MBP.

    I did use MySql Administrator which you had described above. The first time the connection went thru, used the MySQl Query browser to work on Northwind schema and got my php stuff to work.

    Now I get Connection Error : Could not connect to MySQL instance at localhost.
    Error:Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’(2)(code 2002)

    I tried it thru terminal, it does get the same error.

    Pinging the Host is not successful either

    Would very much appreciate your trouble shooting, yourself being a pro…

    Thanks

    Reply
  5. leopardnewbie

    Some add-ons to the troubleshooting situation requested above :

    I did not complete the steps you had mentioned in

    ” MySQL Administrator on Mac OSX Leopard: A Few Tips and Tricks · superfancy on 08.23.2008″

    like

    1. Setting password for root
    2. Adding a new mysql user
    3. creating a new db
    4. assigning privileges to user

    Would these activities influence for the connection error?

    Thanks for any suggestions

    Reply
  6. Stevie

    Hey. Well my first question is, did you install the MySQL Preference Pane? If so, head over to System Preferences and at the bottom under Other, click on MySQL. From there you’ll be able start MySQL.

    In Jeremy Keith’s article I link to above, he set’s up a Terminal command that automatically starts MySQL when your Mac boots up.

    I just start MySQL manually because I don’t use it everyday.

    Hope this at least gets you on the right track…

    Reply
  7. Stevie

    I would concentrate on getting MySQL up and running as you won’t be able to add any users till it successfully starts up…

    Reply
  8. leopardnewbie

    I get the error
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ERROR! Manager of pid-file quit without updating file
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    when I try to start the mysql server thru the terminal like:

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

    Meanwhile in an attempt to view the data folder in mysql, I issued the command
    chown 755 to that folder, which I guess is not right. How do I revoke it.

    Thanks again for your time

    Reply
  9. Stevie

    Whoa… never seen that error before! I am unsure how to revoke the above command. There’s a syntax for doing those types of Terminal commands but I can’t remember off the top of my head.

    Man sounds like you’re having some problems. I struggled too getting this stuff working in previous attempts in the past. I spent many hours and days googling and doing things by trial and error.

    Reply
  10. leopardnewbie

    BTW, to revoke the chown 755 issued earlier to the mysql/data folder , I used the command:

    sudo chown -R root:wheel data

    which changed the owner to root again

    …just in case it would be useful to someone reading this

    Reply
  11. leopardnewbie

    Hi Stevie,

    So I finally got my mysql running..

    I dumped the previous version of mysql and re installed again.

    Your site surely gives a lot of insight to any newbie like me… I must reiterate…

    Thanks

    Reply
  12. Stevie

    Excellent. I was going to recommend reinstalling as a last resort. I know that worked for me on my G5 tower at work which used to have Tiger on it. Cool man glad you got it working… now you can finally do some real work.

    Reply
  13. cosmoanut

    hi leopardnewbie,
    I have the same error (ERROR! Manager of pid-file quit without updating file), did found a solution for this?

    Reply
  14. Scott

    Hi Stevie

    Thumbs up for a very helpful site.

    I have stumbled across one problem which is to do with
    “create a my.conf file in your text editor and save it in /etc/my.cnf with the following code:”
    I don’t seem to have the my.cnf folder and when tying to make a new folder to save the my.conf file in I get the message that the /etc folder is read only

    I’m a complete newbie so any pointers would be great.

    Reply
  15. Stevie

    Hey Scott. Thanks for the nice words. Here’s something you can try…

    Go into the root directory of your Mac and find the private folder. There you’ll find the etc folder. Right-click on it and click Get Info and verify that the system has Read & Write permissions. If it has both then you should be able to create a my.cnf file…

    Reply
  16. Kevin

    Stevie, just wanted to say thanks for the detailed guide. Managed to get all the way up to the MySQL install, but couldn’t find good documentation on what to do. This worked like a charm. Definitely appreciate it!

    Reply
  17. Stevie

    Kevin… thanks for dropping by. My main inspiration for writing this article was the lack of any MySQL setup tutorials pertaining to OSX. Glad you found it useful!

    Reply
  18. John

    Stevie,
    Thanks so much for putting this stuff on the web. It is very straightforward. Unfortnately, I am experiencing the same difficulty that April did on September 22nd. Please advise.
    Sincerely,
    John

    Reply
  19. spiftic

    Fantastic!!
    The best and the easiest method so far and i have looked everywhere. This worked like a charm.

    Cheers :)

    Reply
  20. JC

    with regard for this error: (ERROR! Manager of pid-file quit without updating file)

    try this:

    sudo chown _mysql /var/mysql

    Reply
    • Corey

      Worked a treat for me! Was probably why “System Preferences” kept needing to restart everytime I clicked on the MySQL preference pane also. Thanks JC.

      Reply
  21. Darren

    Just so it’s clear sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock doesn’t move the mysql.sock file it just creates a sym link to it. That is all that is needed to get mysql going with php on leopard.

    Reply
  22. Stevie

    Darren… that’s a great point. One that I didn’t make clear in my article. Thanks…

    Reply
  23. Chris V

    Stevie, I still need a little clarification please. From your following instructions.

    “create a my.conf file in your text editor and save it in /etc/my.cnf with the following code:”

    Am I creating a my.conf or a my.cnf file? and am I just installing it in the /etc/ folder (i.e. /etc/my.conf – or – /etc/my.cnf)? A post above spoke about a my.cnf folder (i.e. /etc/my.cnf/my.cnf)

    The reason I ask is that when I create a my.cnf file and copy it to the /etc/ folder, mySQL stops running, but when I create a my.conf file with the same information in it mySQL continues to run.

    Is there a reason why this would be the case?

    This is the code I used:

    [client]
    socket = /var/mysql/mysql.sock

    [mysqld]
    socket = /var/mysql/mysql.sock

    What exactly does this file do?

    Thanks. =)

    Reply
  24. Stevie

    Chris:

    Yeah the file is just called my.cnf and is placed in the /etc folder. At least this is how it is on my machine. My experiences with setting up PHP and MySQL in the past have all been subtly different, but this is what worked for me.

    Basically and too the best of my knowledge, all the my.cnf does is tell PHP where the to look for the MySQL connection.

    Hope this helps…

    Reply
  25. Mike

    A very useful document.

    A couple of things to note that gave me trouble in my setup:

    1) In the “Testing PHP section” you have the line:

    My computer didn’t like it until I changed it to:

    (deleted the first space) and then everything was fine.

    2) Once website sharing was enabled, I still didn’t have access to my personal website, despite everything looking fine. Eventually tracked the problem down to permission on my home directory (eg. /Users/ ) being wrong – they were 700 instead of 755. Once that was fixed, I could move on.

    Reply
  26. Stevie

    @Mike WordPress for some reason adds a space between the opening php bracket and the question mark. Trying to track down the issue but that is a good point nonetheless.

    I occasionally have permissions issues as well.

    Reply
  27. Stevie

    I removed the syntax highlighter on phpinfo(); for better cut and paste action…

    Reply
  28. werner

    Steve,
    In the part of your article where you talk about virtual hosts the first line of the code segment should read:

    Note the /*/ at the end of the path. That might have been the problem for the people that couldn’t make their virtual hosts work.
    Regards,
    Werner

    Reply
  29. werner

    Steve,
    the comment box doesn’t take the code tage either. I mean first line with the path “/Users/StevieBenge/Sites”. The path sould be “/Users/StevieBenge/Sites/*/”
    Werner

    ps if this doesn’t work send me your email and I’ll send you a PM

    Reply
  30. werner

    Stevie,
    You are welcome, I just went to the same discovery reading – I don’t know how many – mostly incomplete “tutorials” about this on the web. My angle is that I develop Joomla websites and I am setting up my Mac for debugging with Eclipse and XDebug. I am about to publish a blog when I came accross your article. It confirmed what I had seen except that I don’t have to do anything with the mysql socket. After I installed mysql it worked…
    Anyway, good luck with your web design biz. Check out my blog if you have a minute… http://www.theglinkacompany.com/blog

    Reply
  31. Stevie

    @werner: Thanks for the nice words. I use WP and Drupal for most sites that I do, but I am curious about Joomla. I will definitely check out your blog. Thanks!

    Reply
  32. Sean

    Hello. I’m having a problem. I’m so used to setting up PHP and what not on Windows machines that I’m having trouble on my macbook pro. This is my first time using a macbook pro by the way.

    Anyway, after doing the initial test to get PHP and Aapache up and running everything was initially fine. I then followed the part about sharing the web services and adding the line to the hosts file. I got all the way to making the test.php file and putting it in my Sites folder and that’s where I’m stuck.

    Every time I try to go to http://localhost/test.php all that shows up is that actual text of the file test.php. It doesn’t run the phpinfo() function. It’s almost as if php isn’t running.

    Any suggestions because I’m at a complete loss.

    Reply
    • kris

      A late answer, turn off Apache then turn on again (in system preference, websharing)

      Reply
  33. Sean

    Hey, I figured out my problem. It turns out I have a few things to learn about editing in Dreamweaver. I’m used to my IDE that I use in Windows. When you create a new php file in it you start out with a blank page, the way it should be.

    However when I opened up Dreamweaver on my macbook pro and said to create a new php file it turns out that I was initially in live view mode but didn’t realize it. When I sent to split view mode I saw that even though I said to create a new blank php file Dreameweaver actually created an html template file and so the phpinfo() function I was trying to execute was simply being output to the screen. Once I got rid of all of the html code everything ran just fine.

    Reply
  34. Gary

    Hi Stevie,
    a quick thanks. Your instructions were right on the nail, especially for a first time user like me so I am happily up and running now.
    Cheers,
    Gary

    Reply
  35. Lucas

    Hello all,

    I’ve got multiple problems with running both virtual hosts and mysql. Let me bother you with it, as some of you may have already gone through it.. :
    1. After following all steps about VH, I have a permission problems and error 403.
    2. After downloading all files needed to work with mysql I cannot connect to the server neither through mysql preferences in system preferences nor through terminal. In both cases my Mac is working through my command for about 1M, and then nothing happens..

    I am the only owner/user of my mac, so I assume I have access to all files and permission to display them.

    I hope somebody can help me with it.

    Best,
    Lucas

    Reply
  36. Martha

    This article was EXTREMELY helpful. However, I was unable to make mysql work when I moved the mysql.sock file to /var/mysql/mysql.sock

    I had to move the file back to /tmp/mysql.sock

    even after changing the my.cnf file.

    I’m interested in any ideas as to why this is.

    Martha

    Reply
  37. Stevie

    @Lucas: Make sure you restart Apache before accessing your virtual hosts. I’m unsure about the second problem you’re having. Sounds like MySQL could be corrupted perhaps?

    @Martha: Did you restart MySQL after you moved mysql.sock to /var/mysql/mysql.sock? Off the top of my head that would seem to be a likely culprit.

    Reply
  38. Martha

    I attempted to restart MySQL after I moved mysql.sock to /var/mysql/mysql.sock and it would not start. Do you suggest that I try moving it again and restarting again?

    Please advise and thank you.

    Reply
  39. Stevie

    @Martha: I would give that a shot yeah. Are you running a Mac with and Intel or PowerPC processor?

    Reply
  40. Martha

    I’m running MAC with Intel processor

    Ok. Yesterday, I moved mysql.sock to /var/mysql/mysql.sock and I changed the my.cnf file but mysql failed to start after the move. I moved mysql.sock back to /tmp/mysql and still, no luck. So I reinstalled Mysql. Mysql was running this morning and php was running until I moved mysql.sock back to /var/mysql/mysql.sock again at this point mysql failed to start. I moved the mysql.sock file back to /tmp/mysql.sock and everything is working except… my ability to access a database.

    Before I moved the mysql.sock file I did attempt to edit php.ini and the socket path there but this file is a read only even when I use the sudo command.

    any thoughts?

    Reply
  41. Stevie

    @Martha: Ok sounds like mysql.sock wants to be in tmp/mysql for some reason. This is interesting because on the PowerPC machine I use at work it is like that too. Strange. I’m going to take a stab in the dark and recommend that check if you have rights assigned to the MySQL account trying to connect to the database. I wrote an article detailing how to assign privileges and working with MySQL Administrator. Check it out and see if that helps…

    http://superfancy.net/coding/mysql-administrator-on-mac-osx-leopard-tips-and-tricks/

    Reply
  42. Brian

    Extremely helpful. You included everything into one place, made it very simple to understand. I thank you deeply for this great info. Had it set up in 30 min!

    Reply
  43. Martha

    Thanks for the link! I think I”m getting closer:

    I was unable to start via the MySQL administrator but when I start either through the command line
    sudo /usr/local/mysql/support-files/mysql.server start or through the system preferences pane and then I go into the system administrator the detail shows:

    User: root
    Host: localhost
    Socket: /tmp/mysql.sock

    Server info:
    MYSQL Version: mySQL 5.1.35-log via socket
    Network name: localhost
    IP: 127.0.0.1

    Client information

    Version: MySQL Client Version 5.1.8
    Network name: mvanorshoven.local
    IP: 192.168.1.108
    Operating System: Darwin 9.7.0

    ok. so now when I’m in the MySQL Administrator world I added a password to root user, I added myself as a user and when I clicked on catalogs, the database that I had created along with the tables are there! this is exciting.

    Uh oh, now when I go back to the Schema Privileges page and attempt to expand the user ‘martha’, the only item below it is %. However, below root I have localhost, root@mvanorshoven.local and root@127.0.0.1.

    Ok, I highlighted ‘martha’ and then added ‘localhost’ (two buttons next to the sillhouette icons) and I added 127.0.0.1 as well as mvanorshoven.local.

    So that all looks great but still, when I’m in firefox and run the following php file:
    <?php
    $dbhost='localhost:/tmp/mysql.sock';
    $dbuser='mvanorshoven';
    '$dbpass=' ';

    $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connect to mysql');
    print "Connected to MySQL”;

    $dbname=’nativeplants’;
    mysql_select_db($dbname) or die (‘Unable to select database’);
    ?>

    The message I receive is:
    Connected to MySQL
    Unable to select database

    So where to next? I did put passwords into the MySQL admin screen but when I entered that password in the $dbpass field the error was:
    Access denied for user ‘mvanorshoven’@'localhost’ (using password: YES)…

    Reply
  44. Martha

    This article, http://superfancy.net/coding/mysql-administrator-on-mac-osx-leopard-tips-and-tricks/, was helpful!

    Ok. So I did set up MySQL administrator but was unable to start the server via the administrator. Maybe that’s not it’s function. I am able to start the server via the System Preferences Pane as well as from the command line.

    I was also able to access my database, nativeplants, as well as it’s individual tables, commonname and grasstable, via the MySQL administrator. And… I made the connection without needing to put in a password for root, even though I set up a root password through the MySQL administrator when I edited the account.

    I am still baffled though, as to why the following file:

    <?php
    $dbhost='localhost:/tmp/mysql.sock';
    $dbuser='mvanorshoven';
    $dbpass='';

    $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connecto to mysql');
    print "Connected to MySQL “;

    $dbname=’nativeplants’;
    mysql_select_db($dbname) or die (‘Unable to select database’);
    ?>

    produces the following message
    Connected to MySQL
    Unable to select database

    I’m very new at this so I’m wondering if I am connecting to the database but erroneously printing out the wrong error messages or visa versa.

    Reply
  45. Ruth

    Thanks very much! I’ve just moved over from Tiger to Leopard and was having real issues getting virtual hosting to work. Your instructions on editing the my.conf file have done the trick and solved my 403 error. Much appreciated.

    Reply
  46. Caleb

    This is an excellent tutorial on how to setup PHP, MYSQL, and APACHE on a Mac. Thank you so much for this!! :)

    Reply
  47. Ashley

    Thanks so much for the tutorial. I had to modify the php.ini file, to change MySQL to /tmp/mysql.sock.

    I also changed the directories in my.cnf to /tmp/mysql.sock so mine looked like:
    [client]
    socket = /tmp/mysql.sock

    [mysqld]
    socket = /tmp/mysql.sock

    Without these changes, I was getting an error everytime I tried to start MySQL

    Reply
    • kris

      Same problem for me, no mysql in /var/ but it stay in etc, so, the terminal command seem to no move the file… it occure no way to run MySql, well, i have delete the “my.cnf” and i am able to switch on again Mysql

      Reply
  48. Arjen

    In some error messages from Apache, it asks the user to contact the system administrator and shows an email address.

    Do you know how to change the email address that is shown in this error message from Apache? Where does it get it from?

    Reply
  49. Stevie

    @Arjen: Thanks for visiting. If you look on or around line 151 in your httpd.conf file, you’ll see:

    ServerAdmin you@example.com

    Change the example email address to your desired address, then restart apache. I think that should do it.

    Reply
  50. Augusto

    Hello Stevie,

    Im not a programmer so this question may be stupid, but anyway. All these settings have to be done using the root user when logging into my Mac? or my Administrator user?
    Because if Im logged as Administrator I can’t edit the httpd.conf file.

    Thanks for your help

    Reply
    • Stevie

      @Augusto: Yes that is correct… you need to be logged in as an Administrator to make changes to system files.

      Reply
  51. Augusto

    Hi Again Stevie,

    Thanks for your answer…

    Im logged in as Administrator and still cannot edit the httpd.conf file. How can I give the Administrator account the rights to write/edit this file?

    Im sorry, but besides that Im not a programmer I am new to Mac. :)

    Thanks Again.

    Reply
  52. Jim

    Hi,

    Good tutorial but I’m stuck at trying to edit the /etc/hosts file. I can open the file using pico and edit but I can’t save the file. Must be a permissions thing but I checked the file permissions of the etc folder and it does say system: read/write. Can you help? I have another question too…you say edit the etc/hosts file but is that the mMacHDD/etc/hosts or is it the MacHDD/private/etc/hosts ? Or are they the same?

    I skipped that section and proceed through to get php to work.Then I worked on mysql but I can’t get it to start in either the admin pane or the terminal. In teh erminal I get this:

    imac:~ me$ sudo /usr/local/mysql/support-files/mysql.server stop
    ERROR! MySQL manager or server PID file could not be found!
    imac:~ me$ sudo /usr/local/mysql/support-files/mysql.server start
    Starting MySQL
    …… ERROR! Manager of pid-file quit without updating file.

    Comments or ideas?

    Reply
  53. Stevie

    Hi Jim. First off, you must have Administrator rights to edit and save UNIX files. Make sure your user account includes admin rights. If you’re on Snow Leopard, I had a similar problem where Coda (as well as TextEdit) wouldn’t allow me to save UNIX files either. I rectified the problem by purchasing a copy of TextMate. This was right when Snow Leopard came out so this may issue may have been rectified. I think I may have just edited the hosts file with Terminal before I purchased TextMate.

    Yes you are correct… MacHDD/etc/hosts and MacHDD/private/etc/hosts are the same. The former is just a symbolic link to the latter.

    As for your MySQL issues, I would reinstall to be sure it is installed correctly. Luckily I’ve never really had any weird issues with MySQL on any of my computers so I have almost no experience in debugging it. Again if you’re on Snow Leopard, make sure you install the 64-bit version. Good Luck…

    Reply
  54. Jim

    Stevie,
    Thanks for the prompt reply. I’m not on SnowLeopard yet. I’m currently running 10.5.8 (I should have mentioned that earlier). I downloaded mysql-5.1.43-osx10.5-x86_64.dmg, mysql-gui-tools-5.0-r12-osx10.4-universal.dmg

    When I first started this process, I could not even edit httpd.conf so I found a tip online about using pico in Terminal and that worked just fine.

    1. Open terminal.
    2. sudo pico /etc/apache2/httpd.conf
    3. Make changes and save.

    So I assumed I could do the same with the hosts file. Now yesterday I could not do that. This morning I could. Go figure. Obviously a finger problem :) So now my /etc/hosts file looks like this:

    #
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting. Do not change this entry.
    ##
    127.0.0.1 localhost
    255.255.255.255 broadcasthost
    ::1 localhost
    fe80::1%lo0 localhost
    127.0.0.1 myname.dev

    Is that right? Two 127.0.0.1 listed? Anyway, I’m rolling through the steps again to see what happens.

    Thanks,
    Jim

    Reply
  55. Jim

    Stevie,
    I’m stuck at trying to get MYSQL to start either in the pref panel or in terminal. The Pref panel prompts for the password, I enter it and nothing happens. In terminal:
    imac:~ myname$ sudo /usr/local/mysql/support-files/mysql.server start
    Password:
    Starting MySQL
    …… ERROR! Manager of pid-file quit without updating file.

    When I first d//l the mysql and installed it yesterday it was working because I even had the widget on the dashboard showing server local status running a green line graph.

    I’ve kept a text file of what I’ve done. Can I email you?

    THanks,
    Jim

    Reply
  56. Jim

    I’m still struggling with getting the mysql to work via prefpane. I think I’ve messed up the prefpane. I somehow ended up with two mysql icons in the system pref panel. In my frustration I just dragged one of the icons to the trash. Now I get an error message when I click the other one. I rebooted. Back in the system pref panel it now shows a gearbox icon instead of a mysql icon. When I clicked it, it showed “Loading com.mysql.MYSQL” then an error window opened that says “Preferences Error: You cannot open com.mysql.MySQL preferences becasue ti doesn’t work on an Intel-vbased Mac.’ SO I just closed it and went searching online.

    I found this bug report. http://bugs.mysql.com/bug.php?id=28854 I read the post about:
    **************
    I could not start up the server using the Preference Pane, however. I had to run
    “/Library/StartupItems/MySQLCOM/MySQLCOM start” manually. Then I could connect with the
    CLI client without any problems.

    Once started, I could not stop the server using the Preference Pane either. However,
    stopping it from the CLI with “/Library/StartupItems/MySQLCOM/MySQLCOM stop” worked
    fine.
    ***************
    I really don’t know at this point what I’m doing. BUt to hell with it, I tried those start and stop commands in Term and so far as I know they were accepted.

    imac:~ jim$ /Library/StartupItems/MySQLCOM/MySQLCOM stop
    Stopping MySQL database server
    imac:~ jim$

    Maybe that solves my problem, maybe not. I do know that I now have a problem with that mysql icon in the Systems Panel. I’m thinking I should just completely dump mysql off the computer and start over. But I don’t know how to do that either. More reading to do.

    I thought mac’s were suppose to make my life easier.

    Reply
  57. Jim

    So, some progress.

    I completely unistalled mysql by following this http://akrabat.com/computing/uninstalling-mysql-on-mac-os-x-leopard/

    I then reinstalled mysql-5.1.44-osx10.5-x86.dmg and installed the startup items and the pref pane.

    I could then start/stop mysql either through the pref pane or using the Terminal commands:
    sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
    sudo /Library/StartupItems/MySQLCOM/MySQLCOM stop

    Green “running” in the MySQL Pref Pane. Cool!

    From this point I continued on with your tutorial”Getting MySQL and PHP to communicate” and create the my.cnf. Just as soon as I create it, the MySQL server stopped and cannot be started unless I remove the my.cnf file.

    So, this is where I am now stuck. Ideas?

    Reply
  58. Tom

    Hey Jim,

    I have exactly the same issue as you. I had installed MySQL and had is running fine yesterday. Since then I have restarted my macbook and now can’t get the server to start.

    Exactly the input in terminal producing exactly the same error message.

    If you get it sorted then please post and let me know

    Reply
  59. Khalid

    This is an excellent tutorial on how to setup PHP, MYSQL, and APACHE on a Mac. Thank you so much for this!!.I just hope this discussion is still open.

    I faced one problem with my sql. When I looked at /etc I could not find sql.sock that is where I got stuck. Also, when I tried to start mysql via terminal I get the following:
    Oryx:~ kskhater$ sudo /usr/local/mysql-5.5.2-m2-osx10.6-x86/support-files/mysql.server start
    sudo: /etc/sudoers is mode 0644, should be 0440
    Segmentation fault

    Reply
  60. Khalid

    Ok. I fixed the problem with sudo. It was permission problem.

    But the problem of not finding sql.sock is still with me.

    Reply
  61. Jon

    Hi! I had the same “ERROR! Manager of pid-file quit without updating file.” problem.

    I had installed mysql using MacPorts and it turned out mysqld.sock defaulted to be in ‘/opt/local/var/run/mysql5′ – a directory that didn’t exist! So after creating ‘run’ and ‘mysql5′ and setting owner and group to ‘_mysql’ followed by fixing up php.ini to point at mysqld.sock (and restarting Apache) everything works!

    This is on an old PPC Mini with OSX 10.5.8.

    Reply
  62. Sarah.

    Thank you for your explanations ! I was stuck with the mysql socket problem for a while. Found other pages, checked out books, but your indications just solved everything. Mysql is up and running for me now.

    Reply
  63. david

    Do you know how I can use this setup when logged in as a network user? I can’t use the System/Library because my network user profile is not in there.

    Reply
  64. Morten Rasmussen

    Need help!!!

    When adding the def

    DocumentRoot /Users//Sites/mortenrasmussen.dev
    ServerName .dev

    to the .conf file and load the test.php file, I get

    “Forbidden
    You don’t have permission to access /test.php on this server.”

    Reply
  65. jackieB

    Thank you sooooooo much. I had read how to enable Apache and PHP on so many sites that had differing views. Your guide is simple and it works! I hope the MYSQL section is as easy to follow.

    Reply
  66. Jim C.

    I am becoming extremely frustrated at being so close yet not yet ‘there!’ Several weeks ago, I was taking a couple of courses on PHP/MySQL and had everything running in Snow Leopard. I now have a new iMac (with Lion, of course) and cannot seem to be able to get things set up!

    In your “Testing PHP” section you say entering “http://localhost/” will display a page titled “Test Page for Apache Installation.” That may not be applicable in Lion, but all I get is a 404 error with “The requested URL / was not found on this server.” However, I can get the PPHP Info page by using “http://localhost/~myUserName/my_phpinfo.php” (a php file with the same phpinfo() call.

    That program is actually in ~/Sites/ directory. Sure enough, if I my the “test.php” file to the same directory, it displays the PHP info correctly.

    Do I need to establish “virtual” web-servers? Or have I, in fact, done that already?!! I had edited the httpd.conf file with DocumentRoot “/localhost/~xairbusdriver” but have that commented out right now.

    As you can see, I’m stumbling around in the dark and getting bruises from falling into too many ‘pits!’ ;-)

    Thanks for any help, your site is the best I have seen…but without details about what to do when the steps don’t result in the expected way. And, after all, there are only in infinite number of possibilities… 8-| LOL!

    Reply
    • Stevie

      @Jim C.

      Sorry for the delay in response, I haven’t yet worked with Lion so I am unaware if there any nuances in the setup protocol that differ from Snow Leopard or Leopard. It does sound like you need to establish some virtual directories if you want to serve websites out your Sites folder.

      Reply
  67. Ashish

    Hi Stevie,

    Thanks a lot for the step by step instructions that you have provided. I am new to the world of dvelopment and need PHP and SQL for a university course. and hence i stumbled upon your blog. I tried making the change “httpd.conf” file as you have mentioned in teh PHP seciton but i recieve an error message saying “”httpd.conf” E212: Can’t open file for writing”. i get the same message when i try to create the “my.conf” file. Can you please advise me on the same?

    Ashish

    Reply
  68. Prasanth

    I started learning Php today only. Your article is excellent which made me to setup PHP, Apache n MySQL very easily. Excellent job!!

    Reply
  69. Nick Timmons

    Hi Jim,
    When I tested the php file using apache it didn’t read the code it just displayed it. It’s not reading html either. Any ideas what my issue could be.

    Reply
  70. JR

    Hello,
    Thank you very much for your instructions. I think they are very clear, but definitely an error on my lack of knowledge. When I get to the part to setup the virtual sites, and following your instructions, basically like you said I just changed your name to mine in the users conf file. I still get the forbidden error. When in the sharing panel and click on the computer’s website, I get It Works, and all tests up to this point went well. Also I am assuming all your above edits are done at root level, correct.

    Thank you so much,
    JR

    Reply
  71. Vaibhav Pandey

    Very cool article. Must say this is one of the most useful, simple to understand article on the topic of installing / enabling web development software on Mac. Great work!!!

    Reply
  72. Dennis Nolan

    First re web sharing copied from the page at the root when turned on
    If you’re connected to the Internet, your website can also be available to friends everywhere. Just send them the address shown in Sharing preferences.

    I can not get phpinfo to run. Files with a .php extension are not served but change to a .html extension and it is served.
    The test.php file when run is printed out.
    If I change it to test.html all I get is a blank screen.

    php is installed somewhere as
    $ php -v returns
    PHP 5.3.6 with Suhosin-Patch (cli) (built: Jun 16 2011 22:26:57)
    Copyright (c) 1997-2011 The PHP Group
    Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

    Any idea on what is wrong?

    Dennis.

    Reply
  73. Trista

    As far as getting PHP and MySQL to communicate, I am unable to save the my.cnf file to the /etc directory with the following error:

    You do not have sufficient privileges to perform this operation (MacOS Error code: -5000)

    Get info will not let me change the permissions and sudo chmod doesn’t seem to be working either. Any other suggestions?

    Reply
  74. Avideh

    Hi,
    Thank you for your great article.
    Im having trouble finding http.conf. I do not know where it is located at. I found apache.conf file but it does not contain #LoadModule php5_module libexec/apache2/libphp5.so
    Please help me as I have been trying to learn PHP and MYSQL and being stuck on the installations for 2 weeks now.
    Thanks,

    Reply
  75. harris

    Need help!!!

    When adding the def

    DocumentRoot /Users//Sites/mortenrasmussen.dev
    ServerName .dev

    to the .conf file and load the test.php file, I get

    “Forbidden
    You don’t have permission to access /test.php on this server.”

    Reply
  76. harris

    i am always getting this error

    Forbidden

    You don’t have permission to access /test.php on this server.

    whenever i try to open
    localhost/test.php
    nothing coulld fix the problem
    i think there’s some permission issue
    wtsup with that?
    any idea?
    i cant figure out the problem
    please help

    Reply
  77. Steve Lawrie

    I already had php running properly and I was very impressed when php and mysql where communicating but unfortunately after the machine was closed down the communication ceased. Please help!

    Reply
  78. Dani

    Thanks for the post, it was great.
    But at the mysql part of it I’m in trouble :-/
    I do not have mysql.sock file in my /tmp/ directory.
    So when I’d like to connect with MySQL Administrator I get an “Error: Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (2) (code 2002)”
    Could anyone help me please?!

    Reply
    • Lake Michigan Guy

      I believe your issue is one that deals with permissions as you are creating a soft link from one folder (/tmp/mysql.sock) to another (/var/mysql/mysql.sock).

      I don’t have the answer yet, but it involves the use of the UNIX chown (CHange OWNership) command.

      I’ll post a follow up when I have this hammered out.

      Reply
  79. Kris

    Hello,
    @Harris, maybe the mistake in your syntaxe :
    DocumentRoot /Users//Sites/mortenrasmussen.dev
    ServerName .dev
    SHOULD BE
    DocumentRoot /Users/Sites/mortenrasmussen.dev (single/ between users & sites)
    mortenrasmussen.dev
    @Stevie, thanks, i missed 3 days trying to access my local website looking around the web, until i found your tuto , but i have trouble when i whant to install a cms-eshop : the Pre-installation Check shows there is no permission to files and sub-directory.
    An issue please ??

    Reply
  80. Stevie

    @Kris: Your Virtual Directories are all setup correctly if you’ve gotten as far as attempting to install CMS software. The issue you’re having is permissions on the directories in your Sites folder. You’ll either need to use Terminal to change those or using the “Get Info” dialog box. Google will be your friend in showing you how to do this.

    Reply
  81. Edwin

    I’m having trouble saving the file test.php

    I’m pulling my hair out trying to make this work.

    Please help!!

    Reply
  82. SimonW

    Okay so the mysql.sock file isn’t there! I’m brand new to Linux based programming and don’t know an awful lot about macs so I might be missing something obvious.

    mysql.sock doesn’t exist, infact the whole mysql/ folder doesn’t exist. I have looked in tmp and var and neither seem to have the file or folder. Why would that be? How do I fix this?

    Thanks.

    Reply
  83. Sonja

    Hi Stevie. I am a complete novice trying to teach myself database programming. With your help I have been able to get php/apache running on my Mac OS 10.5.8. I downloaded MySql 5.5.20-osx10.5-x86_64. I have a problem installing the PrefPane. The MySql icon is in my SystemPref, but tells me “You cannot open MySQL preferences because it doesn’t work on an Intel-based Mac.”
    I would appreciate some help.
    Thanks!

    Reply
  84. David R.

    Please help! I had this running fine before but I recently had to replace the hard drive. I’m still running Snow Leopard (10.6.8) and cannot get past ‘Testing PHP’. When I click on my computer’s website in System Preferences I get the following error (much like the gentleman running Lion above):

    “Not Found

    The requested URL / was not found on this server.”

    I have not been able to find a solution to this on the web. My search parameters are either too wide or so narrow that the returns are non-existent. I even tried MAMP and the Apache and MySQL servers won’t start. I’ve restarted my computer but that didn’t help. I’m running on a Linux network but I’m logged in as a local user. Any help you can provide would be so much appreciated.

    Thank you!

    Reply
  85. Henry Minden

    Hey Stevie,

    This was a great tutorial that I think is quickly becoming the standard for peeps on the MAMP stack. However, I did have some trouble when I tried to configure MySQL 5.5 with OSX 10.6.8. I wanted to post this to try to save someone a bit of time and hair pulling.

    I used the 64bit DMG from mysql.com, and could not get the server actually running until I deleted the my.cnf file.

    What ended up working for me was changing the php.ini file as you suggested, however the one line you left out that may affect some users is this line:

    mysqli.default_socket = /var/mysql/mysql.sock

    should be set to

    mysqli.default_socket = /tmp/mysql.sock

    if you forego creating the my.cnf file you suggested.

    MySQL now works and connects to PHP. Thanks again. I hope this helps someone.

    Reply
  86. Maziar Kosarifar

    HI, Tnx for your tutorial but I can’t find although I did all you said but still when I run my php code it doesn’t running and on the webpage you can find my code instead of it’s behaviour !!!

    Can you help me with that ?

    Reply
    • Dele

      Check my comment below. It solves this issue. I also encountered this issue.

      Reply
  87. Dele

    Thanks for this nicely written article on how to set things up. I battled for a while only to discover that one needed to add the :

    AddHandler php5-script .php

    After enabled php5 module in the http.conf file.

    You article omits this. What I realised was Apache could not interpret my php files. Rather it was spitting my source code to the page.

    I hope this helps someone out there. It was a mission to finally nail it on this.

    Reply

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>