Setting up Apache on Snow Leopard

posted on: September 2, 2009 in: Development OSX
58

sl-apache

I’ve upgraded both my Macs to Snow Leopard, and a few things have changed with the built in Apache webserver.

As a web developer it’s important for me to have a fully functional development environment so the first thing I had to do with the new install is get Apache set up and running as I need it.

This post shows how to set up PHP, mySql, Virtual Hosts & .htaccess

PHP

By default PHP is disabled so let’s turn that on.

In the finder navigate to the /etc folder, this houses all the files we need to configure. This is a hidden system level folder so to access it press “apple shift G” and type /etc.

go-to

Find the folder Apache 2 and open httpd.conf

This is the main config file for Apache, I use textMate to edit these files but you can use any text editor.

Go to line #115 and uncomment (remove the starting #) “LoadModule php5_module”

Save the file, you will be asked to enter your password.

After every change you make to the Apache or PHP config files you will need to restart the webserver. This can be done in the Terminal with “sudo apachectl graceful” or go to System Preferences and untick then tick Web Sharing in the Sharing Pane.

Now PHP is running.

Error Messages when using date() function

PHP has been upgraded to V5.3 and the Snow Leopard version doesn’t have a timezone set, this will give you errors when you use the date() function. To fix this go back to the “/etc” folder and open “PHP.ini.default”

Go to Line #997 and enter your timezone, here in the UK it’s Europe/London find your timezone.

Remove the semicolon and the line should read “date.timezone = yourtimezone”

Save this file as PHP.ini in the etc folder, restart the webserver and PHP is now all good to go.

mySQL

mySQL doesn’t come as standard on OS X so we need to install it luckily this is nice and easy because we can get a dmg file. I choose the 64bit version, head over and grab it scroll down to “Mac OS X (TAR packages) downloads” and choose “Mac OS X 10.5 (x86_64)”.

Open the dmg and run the installer. I also installed the prefPane, double click the “MySQL.prefPane” It’s a good idea to use this as you can use System Preferences to start/stop mySQL.

Setting the password

The mySQL install doesn’t set a password so we need to set it.

For this task we need to use the Terminal. Open Terminal located in the “Applications/Utilities” folder and type “/usr/local/mysql/bin/mysqladmin -u root password yourpassword” (where yourpassword is the password you want.) hit enter and the password is set.

terminal

Fixing the PHP socket

Before we can use mySQL we need to point PHP to the correct place. Open the PHP.ini file as before and change line #1213 to mysql.default_socket = /tmp/mysql.sock

Save this file and restart the server.

phpMyAdmin

I use phpMyAdmin to manage mySQL. So here’s how to do that.

Head over to the phpMyAdmin Site and download the latest version.

Unzip the downloaded file and place in either your Sites folder or the servers root (Library/WebServer/Documents) I use the root as I class this as a management tool and like to keep my Sites folder for projects.

This needs configuring too. It does come with a documentation file so you could go through that and set it up as you like. I keep mine nice and simple and have it so I don’t need to log in each time.

Open “config.sample.inc.php” and add after “$i++” the lines:

$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'yourpassword';
$cfg['Servers'][$i]['auth_type'] = 'config';

Remove the line “$cfg['Servers'][$i]['auth_type'] = ‘cookie’;”

Save this file as config.inc.php.

In a browser go to “http://localhost/phpMyAdmin/” if you moved the folder to the root as I did or if you placed into your Sites folder go to “http://localhost/~yourusername/phpMyAdmin”.

Virtual Hosts

Open the “httpd.conf” and uncomment line #461 which reads “Include /private/etc/apache2/extra/httpd-vhosts.conf”, then save.

Now open “/etc/apache2/extra/httpd-vhost.conf”

I commented out each VirtualHost block and made my own underneath, You really do need this one:

<VirtualHost *:80>
DocumentRoot /Library/WebServer/Documents
ServerName localhost
</VirtualHost>

After this block you can add your own

<VirtualHost *:80>
DocumentRoot /Users/youruser/Sites/yoursitefolder
ServerName yoursitesname
</VirtualHost>

You will need to add a new VirtualHost block for each VirtualHost you wish to set up.

Save this file and we’re almost done. Open “/etc/hosts” and add “127.0.0.1 yoursitename” you will need to add a new line for each Virtual Host you have set up where “yoursitename” matches the name chosen name in the VitrualHost block in the previous step.

Save this file and restart the webserver. Now in a browser type the VirtualHost name and you will see the corresponding site. A link to “/” within this site will take you to the root of that site as if it was on a remote server.

.htaccess

Open the “httpd.conf” file and change line #210 to read “AllowOverride All”.

Open “/etc/Apache2/Users/youruser.conf”

Change AllowOverride to All

Add a new Directory Block

<Directory "/Users/youruser/Sites/*/">
Options Indexes MultiViews FollowSymLinks
</Directory>

Save and restart the server. Everything should now be all set up and you can use .htaccess and mod_rewrite.

58 Comments

Leave a comment
  1. Marco said on October 10, 2009 @ 23:20

    Fantastic tips. I was almost giving up setting phpMyAdmin and I was going back to XP but I followed your instructions and everything worked smoothly.

    Great post!

  2. Marco said on October 14, 2009 @ 06:28

    To have the date.timezone affect the server I also had to rename “PHP.ini.default” into “PHP.ini”

  3. kev said on October 14, 2009 @ 10:32

    Hi Marco thanks for your comments, glad I was of some help. Yes you are right you do need to change php.ini.default to php.ini.

  4. mr. c. said on October 14, 2009 @ 20:10

    appreciate this. was struggling with .htaccess rewriting.

  5. chapman said on October 20, 2009 @ 17:13

    Thanks – another tip for some – make sure the extension is mysql, not mysqli.

    $cfg['Servers'][$i]['extension'] = ‘mysql’;

  6. damon said on October 21, 2009 @ 04:05

    Marco,
    For some reason I cannot save my httpd.conf file. It does not prompt me for a password, but just says that the file cannot be saved. I’m using TextEdit. Suggestions?

  7. Dan said on October 24, 2009 @ 16:07

    Thanks from me too.

  8. Chewbunny said on October 31, 2009 @ 18:25

    Marco,
    You can’t save the file because it is owned by root and you are not allowed to edit. The way I get around this is to edit in the terminal using vi (a text editor) but this may not be a little old fashioned. NOt sure how this is achieved in the more standard applications – is there a run as option?

  9. matt said on November 4, 2009 @ 16:49

    In answer to Damon, I had the same problem with overwriting the httpd.conf file from TextEdit. But I found a work-around.

    Open up the httpd.conf file and make the edit to it, then ‘Save as’ to another location like your desktop (make sure you add the .conf extension) Then drag and drop the edited httpd.conf file from your desktop into the apache2 folder. It will complain about something and then ask for a password, but it will copy over.

    Hope it helps.

  10. Herman said on November 15, 2009 @ 21:57

    How come when i call localhost in browser everything is ok, but when from my iphone or other comps in network type the IP adress (10.0.1.11 <— is the webserver) nothing shows up, blank page.

    What am i doing wrong??

    ty for help

  11. kev said on November 16, 2009 @ 20:16

    Hi Herman

    You are right, you do need to type the ip address on another machine/iPhone. Make sure all devices are on the same network. You can get the IP address from system prefs > sharing > web sharing it will normally be something like http://192.168.0.2. If you are using the sites folder you will need to put /~yourusername. Hope this helps.

  12. StationStops said on November 20, 2009 @ 21:50

    An outstanding tutorial and very nicely formatted, nice work.

    As a longtime Linux developer I recently switched to using Snow Leopard as a staging server. This saved me a lot of rooting around.

  13. Terry said on November 21, 2009 @ 22:21

    I upgraded from Leopard to Snow Leopard on my early 2009 Mac mini. I tried getting virtual hosts setup using a number of other tutorials and none of them worked. I came across this one and was getting closer. The problem I am running into is that when I hit “http://mysite.local”, it simply shows me the default index.html.en page under /Library/WebServer/Documents/
    I’ve checked and double checked all of these files as per the instructions in this article:
    /private/etc/apache2/httpd.conf
    /private/etc/apache2/extra/httpd-vhosts.conf
    /private/etc/hosts

    When I look at my /private/var/log/apache2/error_log file, I don’t see any errors showing up.
    My best guess is that the httpd-vhosts.conf file is not being loaded even though I have uncommented the line to include it. I even tried adding some english sentences to trigger an error on apache restart and it doesn’t complain.
    Can someone point out something I might be missing?
    Thanks

  14. Andrew said on November 23, 2009 @ 04:07

    Thanks for the clear tutorial, Kev. One small detail – going to the MySQL page, you go to the “package format” section to download the .DMG with the installer file, not the “TAR packages”, which uncompress into a bunch of files and folders rather than an installer.

  15. Andrew said on November 23, 2009 @ 04:37

    Also – there’s a 64-bit pref pane for starting and stopping MySQL at http://www.swoon.net. Otherwise System Prefs has to relaunch in “32-bit mode” every time you want to start or stop MySQL from the pref pane.

  16. benoit said on December 3, 2009 @ 18:12

    Thanks for this tutorial, was very helpful. One detail – I’m working on new iMac 21.5″ snow leopard and I did everything you write is working find if my file are in webserver document but if i try on site I have this error:
    “Forbidden
    You don’t have permission to access /~Imac/index.php on this server.”
    index.php is your phpinfo.
    Did I miss something??

  17. Marco said on December 16, 2009 @ 15:58

    Damon:

    the file do not get saved because it need root privileges.

    >sudo vi httpd.conf
    password: {put your own admin password}

    Edit the file using vi.. check in the web for a small tutorial.

  18. danny said on January 15, 2010 @ 04:49

    I’m new to mac so excuse me if ther may be a simpler way but for me this was pretty simple. i had the same issue of not being able to save, and the resolve i found was simple for me. all i did was:
    1. Go to the folder that houses the file you are trying to save
    2. click and go to get info
    3. Go to sharing and permissions
    4. Go to the bottom where you see the lock icon and unlock, at this point you will get prompted for your password, once you’ve done that
    5. You can either change the permission for the “everyone” Profile to read/write or add your profile (which should come up automatically) to the list by clicking on the + icon on the bottom left
    6. Once done you should be able to save your file. don’t forget to revert back to the read only profile if you need to.

    Note the PHP file is in the etc folder which is greyed out. and i couldn’t change permission so i had to go to show original which then brought me directly to the etc folder where i was able to carry out the above.

    Hope this helps out

  19. Robert said on January 23, 2010 @ 05:44

    Kev, thank you immensely for this clear and helpful tutorial. But..I have run into what I suppose is a password problem with mySQL that I do not know how to solve.

    I got mySQL installed and running, then I set my PW (let’s say it was “zygote”) in Terminal by entering: “/usr/local/mysql/bin/mysqladmin -u root password zygote” and got no complaint…just the usual prompt for the next command.

    Next, I downloaded and installed phpMyAdmin folder in Library/Webserver/Documents (changing the name of the folder from “phpMyAdmin-3.2.5-all-languages” to “phpMyAdmin”), and added these lines to saved config.inc.php file:

    $cfg['Servers'][$i]['user'] = ‘root’;
    $cfg['Servers'][$i]['password'] = ‘zygote’;
    $cfg['Servers'][$i]['auth_type'] = ‘config’;

    Restarted the web server. But…when I entered “http://localhost/phpMyAdmin/” in Safari, I got this error:

    “Welcome to phpMyAdmin
    Error
    MySQL said:
    #1045 – Access denied for user ‘root’@’localhost’ (using password: YES)
    phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.”

    Any suggestions would be most welcome!

  20. Randal Maile said on January 28, 2010 @ 18:19

    This is a great post!! Nice and Clean! Great work – thanks for the info!!!

  21. Karl said on January 30, 2010 @ 23:40

    This is a great post, very clear and intuitive..! Thank you

  22. Jon Norris said on February 2, 2010 @ 21:44

    For Robert: Total newbie at all this but after many many hours I tried ‘mysql’ as the user, and no password (so just leave the quotes like this ”) and it worked. Nothing to do with root at all!

    Now I find phpmyadmin wont let me make a new database so I’ll read more on users!

  23. Peter Hobley said on February 6, 2010 @ 03:08

    Thanks for a great tute. Was impressed that I managed to get mine going first time.

    I’ll admit that configuring to run a localhost has always been a mystery to me, although I’ve had them working in the past. After this tutorial it makes a bit more sense now. Just a bit, mind, but it’s working a treat!

  24. Penelope said on February 11, 2010 @ 20:06

    Hello. Everything was very clear and I performed all the steps but the last one. There is no way I can save changes to myusername.conf even thou I have set permissions to myself to read and write.

    What can I do? I tried saving as on the desktop and dragging it to the folder… which worked but then my pro didn’t recognize my username and had to reinstall Snow Leopard after calling AppleCare.

  25. Penelope said on February 11, 2010 @ 20:10

    Also, after reinstalling SL my user name came with a number 1 in front of it… like this: “Username 1″ but on the Web Sharing it comes like “http://10.0.1.200/~Username/” without the number 1. Does that affect the above configuration when adding the directories to the hosts file?

  26. Matteo said on February 17, 2010 @ 17:21

    Hi, very good tutorial.
    Unfortunately I cannot rename the php.ini.default to php.ini because I’m not authorized to write into the /etc directory…
    Any hint about this, please?

  27. Phil said on February 25, 2010 @ 11:24

    Great tutorial.. Its working great.. Im first time Mac user and full convert..

    Thanks again for the great article.

  28. Vandana Tiwari said on February 26, 2010 @ 01:30

    Hi All,

    I dont have experience in Mac OS . I am facing some wired problem . In my Mac os i have apache installed in directory /private/etc/apache2 i want to configure virtual host but in my machine there is no folder /private/etc/apache2/extra i have serached for httpd-vhost.conf file , but there is no such file . Even in httpd.conf file there is no line which include httpd-vhost.conf file .

    Version of apache installed – Apacahe 2.2.11
    Version of MAC OS – 10.6

    Please advise on this issue

    Thanks
    Vandana

  29. Howard said on March 27, 2010 @ 09:32

    Hi thanks for the tutorial.
    When I log into phpMyAdmin, I get “Cannot load mcrypt extension. Please check your PHP configuration”. Any ideas?
    Thanks.

  30. Marco said on April 2, 2010 @ 08:47

    @Matteo,
    open the Terminal application found in Application/Utilities folder. Reach your php.ini.default file by changing directory to its location and then type the following :

    >sudo cp php.ini.defaul php.ini
    password: {put your own admin password}

    sudo command is going to run the Copy (cp) command after you provide the administrative password of you account (login account). Your login account need to be set with administrative privileges which is usually the case unless someone else is configuring your computer.

  31. JC Hendee said on April 6, 2010 @ 22:27

    I truly hate having to run out and get yet another app for every little thing on Mac, considering HTTP services on the PC side are very easy and clean. But if you want the fast way around all write permission problems with the files in this set up process, go get TextWrangler.

    It runs the necessary terminal commands on the fly to let you edit these files locked down by the “root” (the REAL administrative account in a Mac). The most you’ll have to do is answer a few dialogs and use your login password. It will save you minutes or even hours of struggling with editing these text files. And its line count and various go-to functions will also speed up your process. The application is FREE and very well constructed.

  32. Nancy said on April 18, 2010 @ 19:23

    Kev: Thank you for a rather painless tutorial! I’ve got a virtual host running on my Macbook Pro, along with PHP!

  33. Bas Ben said on April 19, 2010 @ 10:40

    Wow! tnx for all the great tips… I was about tog go back to Xampp or MAMP

    thanks

  34. Greg said on May 2, 2010 @ 18:31

    Hey this worked perfectly. Thanks for the tutorial.

  35. Soundfreak said on May 23, 2010 @ 05:23

    Nice post ! Thanks a lot !!

  36. Shahid said on May 26, 2010 @ 02:24

    excellent for new mac user!

  37. Bob Sica said on June 4, 2010 @ 16:43

    Thanks for the advice, but it doesn’t work for me. Can anyone explain this .htaccess error:

    [Fri Jun 04 11:38:51 2010] [alert] [client 99.250.28.33] /Library/WebServer/Documents/.htaccess: Invalid command ‘\xef\xbb\xbfAuthType’, perhaps misspelled or defined by a module not included in the server configuration

  38. Jacques Renatus said on June 17, 2010 @ 10:10

    I had a major problem getting Drupal 6.17 to see my MySQL database during setup, I finally determined that the file …somesite.dev/sites/default/settings.php contains the following line

    $db_url = ‘mysql://username:password@localhost/databasename’;
    The line above needs to be modified, e.g.
    $db_url = ‘mysql://mrwonderful:teddybearsarescary@localhost/drupaldatabaseofdoom’;

    I tried something similar with a Drupal 7 (alpha) settings.php file (it’s a little tricky since Drupal 7 appears to use an array rather than a string to hold this info) but I got an error upon refresh that included the line

    No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)

    which indicates that the problems that I’ve had and that others have reported elsewhere are caused by the fact that the OS X .dmg from MySQL puts mysql.sock in a different directory than var.

  39. da said on June 28, 2010 @ 19:25

    Thanks. Good info. That said, your title says ‘set up Apache in OSX’ but the article is actually assuming you already have Apache up and running in OSX.

  40. Sarah said on January 23, 2011 @ 10:21

    Thanks for the information! But when I’m in Terminal trying to set the MySQL password it gives me the error ‘/usr/local/mysql/bin/mysqladmin: connect to server at ‘localhost’ failed
    error: ‘Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2) ‘. I’ve changed the path as you stated.

    I’m also having a problem accessing the PhpMyAdmin folder – http://localhost/~myusername/phpmyadmin .

    I can get to my test file if I just type in http://localhost/~myusername/test.php, but nothing inside the folder will show up..not even if I move the test.php into the phpmyadmin folder. It seems like a permissions problem, but I believe I’ve set them appropriately.

    Any suggestions are much appreciated!

  41. Mac OS X 10.6, PHP and MySQL » Wolfgang Reutz's Blog said on February 10, 2011 @ 13:15

    [...] Chapman has a good documentation about how Setting up Apache on Snow Leopard. It’s worth a read and has all you may need to sucessfully setup apache on Mac OS X 10.6.x [...]

  42. steward said on February 19, 2011 @ 13:22

    New to mac. Very new. Just installed XAMPP (coz it’s what I always do with windows). It was a one-click install for php, mysql and apache. So why go this route?

  43. Barry said on March 5, 2011 @ 22:31

    PLEEEAAASSEE. HELP! I used the administrator account and tried using textwrangler and it wouldnt let save the php.ini.default as php.ini it said:

    This operation could not be completed, because an error occured.
    You do not have sufficient privileges to perform this operation (MacOS Error code: -5000)

  44. Just passing by said on March 19, 2011 @ 22:42

    Mac OS X 10.6.6

    In terminal type:

    su vi /etc/apache2/httpd.conf

    You’ll be asked to enter your admin pass and can save the file after the required changes have been made.

  45. Hugh said on April 29, 2011 @ 19:36

    Hello,

    I’m trying to block the access to certain files with a .htaccess file and a .htpasswd file. I changed all the setting in the httpd.conf file (allowoverride set to all), and username.conf file but i get an Internal Server error.
    Anyone knows how to fix this ?

  46. devric said on May 18, 2011 @ 01:19

    Hi, for some reason when i try to enter the mysqladmin, in the bin folder, it cant be found
    “-bash: mysqladmin: command not found”
    did i miss anything??

  47. Jim said on June 3, 2011 @ 00:17

    I tried to get clean urls so many ineffective ways and finally got it thanks to your last box:

    Options Indexes MultiViews FollowSymLinks

    Thank you thank you thank you

  48. Tanner Hans said on July 13, 2011 @ 23:02

    I followed all given hints, but as soon as i activated virtual host in httpd.conf (line 461), I can’t access http://localhost/ no more, nor phpmyadmin. I get the message 403 forbidden: You don’t have permission to access…

    I’m a little desperate :-( and grateful for help!

  49. kev said on July 14, 2011 @ 22:56

    Hi Tanner. Try checking your config for errors. Terminal: apachectl configtest. Also make sure that you still have: 127.0.0.1 localhost in your hosts file (it should be the first line after the #comments).

  50. Jim Forsyth said on September 2, 2011 @ 22:26

    Hi Kev. Quite simply, you are a legend. I’ve just wiped clean and re-installed my entire system, which has been made a whole lot easier by following your step-by-step guide. Nice one :)

  51. Elim said on October 14, 2011 @ 18:17

    I don’t think localhost and 127.0.0.1 will make any differences. The problem of connection is mainly the location of mysql.sock if mysql shell is working and port number has not been changed.

    Once I corrected the mysql.sock path in php.ini, restart apache should make apache+php+mysql working

  52. Ellen said on October 17, 2011 @ 14:27

    Hi Kev,

    Your info has been really helpful. HOWEVER, I am having issues! I set everything up as you described for my Sites folder. In my sites folder I have /sites-name/ and then inside that a WordPress site. The homepage works great, but all other pages and posts give me a 500 Internal Server Error. I’ve noticed when I turn AllowOverride back to “None” I then get a 404 error instead.

    I also have another site in progress in another folder under Sites – haven’t found this problem so far.

    Any ideas what is causing this?! Is it WordPress? There is no .htaccess file in my WordPress folders, not sure whether that makes a difference.

    Thanks

  53. Scott said on October 17, 2011 @ 16:26

    Thank you very much for putting this together. I’ve been working on getting rid of MAMP and moving everything over to the native OSX. It was confusing because with MAMP I had two PHP’s and two MySQL’s installed. It was tricky to get everything straight. Your article was the most helpful, accurate and clearly-written of anything I’ve seen on the web in the last couple of days.

  54. Steve said on February 27, 2012 @ 19:48

    After installing phpMyAdmin and configuring as instructed I go to open by typing “http://localhost/~yourusername/phpMyAdmin” in my browser and get a “Welcome” page with an error:

    MySQL said: #2002 – The server is not responding (or the local MySQL server’s socket is not currently configured)

    I followed these instructions to the letter and am running OS 10.6.8. I uncommented the “LoadModule” for PHP5 in httpd.conf, I installed phpMyAdmin 3.4.10.1 with mySQL 5.1.61.

    I have restarted Apache.

    Can anyone help!?

  55. srini said on March 17, 2012 @ 20:54

    change the permission to the appache2 folder as read and write access.. then u can save it dude

  56. Michelle said on March 30, 2012 @ 09:09

    Thanks for the detailed instructions! Was following another tutorial on someone else’s website and I couldn’t get past 403 error. But thank goodness I found your article and it solved my problem immediately! :)

  57. Jim said on August 15, 2012 @ 05:03

    Hi Kev,
    This is the best *looking* tutorial i’ve been able to find after hours of searching, but I wonder if you might know the answer to a question. I upgraded my web server from OSX 10.5x to 10.6x, and of course this broke my previously-functioning PHP/MySQL site. Now PHP is “running,” but not “functioning”– Apache is crashed (though Apple’s “Server Admin” app says “Web Service” (what they call Apache) is running (a falsely reassuring ‘green light’ appears next to it) and is outputting these “Segmentation Fault” errors into its log file. I also noticed my “mysql.sock” file is still sitting in /Var/mysql, and no such file exists in /tmp. I changed the php.ini file to point it to /tmp but thinking maybe I should change it back?

    I am such a newbie with this stuff. I am certain that whatever is causing the breakage has more to do with MySql than anything else, but I can’t even run PhpMyAdmin because NO pages will show up in a browser. At best I can see the raw code when I browse to a page such as index.php.

    If you have any ideas about this I would be so grateful for your help. This tutorial really is one of the best ones out there so I thought it’s worth a shot to ask!

  58. Tony Coppola said on February 7, 2013 @ 18:45

    Hi,

    For enabling PHP, you mention to to to /etc/apache2 and edit httpd.conf and:

    Go to line #115 and uncomment (remove the starting #) “LoadModule php5_module”

    On line #115 of my /private/etc/apache2/httpd.conf is:

    LoadModule headers_module libexec/apache2/mod_headers.so

    If I look a little farther down to Line 144 I see something similar:

    LoadModule php5_module libexec/apache2/libphp5.so

    It was already uncommented….has something changed in Apache or PHP since your instructions were written? I’m running a Mac mini server, Mac OS X 10.6.8.

    I also have issues with editing php.ini and instructions not matching the correct line numbers, but I figured I’d deal with the httpd.conf for now.

    thanks in advance!

Leave a comment