Getting Apache & PHP to work with Leopard (OS X 10.5)

I ran into a snag today while trying to get the websites on my local machine running with Leopard. The first thing was that the config file in the new version of Apache is in a different location (/etc/apache2/httpd.conf instead of /etc/httpd/httpd.conf). Once I changed the settings I kept getting 403 Forbidden messages every time I loaded a page (the default in the new httpd.conf is ‘Deny from all’ for all directories). I finally figured things out and here’s what I did:

  1. Open the httpd.conf file for editing (sudo vi /etc/apache2/httpd.conf).
  2. Uncomment the line
    LoadModule php5_module     libexec/apache2/libphp5.so
    by removing the ‘#’ from the front of the line. This will enable PHP 5. The php5.conf file is loaded automatically from /private/etc/apache2/other/.
  3. Create a file called _sites.conf in your Sites folder. I like to keep my site configuration in the Sites folder to make it more easily accessible as I’m doing development.
  4. Add the following line to the very bottom of the httpd.conf file:
    Include /Users/yourusername/Sites/_sites.conf
    That will make Apache load all the configuration settings from your _sites.conf file.
  5. Add the following information to your _sites.conf file.
# Enable named virtual hosts
NameVirtualHost *:80

# Override the default httpd.conf directives.  Make sure to 
# use 'Allow from all' to prevent 403 Forbidden message.
<Directory />
	Options ExecCGI FollowSymLinks
	AllowOverride all
	Allow from all
</Directory>

# A basic virtual host config
<VirtualHost *:80>
	# Add yoursite to your /etc/hosts file so you can 
	# type it directly in your browser
	ServerName yoursite

	DocumentRoot /Users/yourusername/Sites/yoursite
</VirtualHost>

Now you just need to start/restart Apache and the sites should load. PHP should be working and no more 403 Forbidden message.

39 Responses

  1. John, I’ve been pulling my hair out rying to fix this and your approach was the only one that worked, and your explanation the only one that fully explained what was going on – and then some.

    thanks!

  2. Thanks for the example on a Apache named virtual host and /etchosts example. I got PHP5 working earlier but was searching the web for quit a while to get a http://yoursite.local/ like URL to work with Apache (see you’re point 5). Now I can use root relative paths, on my local website development, while my sites are still in my own ~/Sites directory, where they should be.

  3. This isn’t the ‘apple’ way of doing it.

    If you are getting this error, you will need to create the following file by hand:

    /private/etc/apache2/users/username.conf

    With the following contents:

    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all

    That will get you back to defaults with the particular users Sites sharing.

  4. eh, it ate my brackets…

  5. Riley & Dennis, I’m glad this helped you. I really like this setup for my development environment.

    Nathan, it may not be the ‘official’ location to create a .conf file but it’s how I prefer to do things. It’s easier to open and update the .conf file and it also gets backed up when I backup my Sites folder.

  6. Thanks for taking the time to write this! I would have taken me forever… still a bit new to developing on the Mac, then I went and upgraded to Leopard! Thanks again.

  7. Thanks – I had finally worked out the correct configuration directory, but was still having a problem with the “Forbidden” message. The “Allow from all” did the trick.

    I just wish I found this article a couple of weeks ago when I did the upgrade!

  8. Hi John,

    I can only join Riley & Dennis by saying that this solution is the first one that worked after sleepless nights trying to get Apache2 to work.

    Cheers,

    Bob

  9. I’ve done everything on here and more and I still have an issue. For some reason I have one VirtualHost that works just fine and the other I get 403 Forbidden. No matter what I do, it still is this way. Any ideas would be extremely helpful. Thanks in advance

    Jason

  10. Jason, when I’ve run into those type of issues it’s usually that my httpd.conf or _sites.conf aren’t setup properly somewhere, I’ve entered a path (DocumentRoot) where my files don’t actually exist, or the directory permissions are setup so that apache can’t see the files. Also, after each config change, you have to restart apache to get the new change.

    Hope this helps.

  11. Good lord!
    thank you for pointing me to /etc/apache2/httpd.conf
    I wish I had those 8 hrs of my life back

  12. Works beautifully, thanks!

  13. BTW…

    To add to item 5. in the list of instructions above, for people inexperienced with the command line…

    Open a terminal window, type one line at a time, hit enter after each:

    cd /
    cd /Users/yourusername/Sites/
    pico

    Then paste in the _sites.conf code from above and use ctrl + O to save the file and name it _sites.conf – it will be in the correct directory because you navigated there before opening pico (the command line text editor).

  14. Genius!

    I was about 10 mins from restoring my Tiger install until I read this, and hey presto, you solved the issue!

  15. awesome, thanks. new to mac and people like you are making the transition much less painful.

  16. Hey, this just saved me some time too! Thanks so much!

    David

  17. I’m fairly new to the whole Apache scene, but I was able to get it running tonight. I’m using a clean install of Mac OS X on a Powerbook G4.

    My problem is setting the virtual hosting using the sites.conf file as instructed above. I followed the directions to a tea, but every time I try and add a Virtual Directory to the sites.conf file and restart apache, the location of the virtual directory as well as all the apache default and test pages give me a 403, and nothing works at all. If I remove just the ServerName and DocumentRoot properties from the virtual directory, the default pages start working again, which is strange because they should be totally unrelated to what I’m trying to do. This happens whether or not I’ve specified the virtual directory in the hosts file.

    I’m puzzled by this since I’ve set the Allow all files property, but everything breaks with a 403 if I try and add a virtual directory. I’m about stumped as to why this is happening.

  18. Thanks. I like the method of keeping _sites.conf in the Sites folder.

    For developing websites in my own made-up .dev domain, I also add another VirtualHost *:80 block for each site, containing two lines:

    ServerName yoursite.dev
    DocumentRoot /Users/yourusername/Sites/yoursite.com

    And add a line to the hosts file

    127.0.0.1 yoursite.dev

    Then you can go to http://yoursite.dev/ in your web browser, and everything works because the site is in its own top-level directory.

  19. hi
    i don’t have write permission for httpd.conf file. i am the admin and there is no other user account on my macbook. i recently upgraded to leopard. with tiger, apache was working just fine.
    pl help me

  20. Hi tanuj, you’ll need to use “sudo” to get OS X to give you permission to edit the file. So, if you’re familiar with using vi as a text editor then you can type the following in the command line:

    sudo vi httpd.conf

    It will prompt you for your password and allow you to edit the file.

  21. If you’re still getting 403:Forbidden errors and you have FileVault on your machine, try this: http://ricardo.strangevistas.net/apache-unable-to-access-you-sites-on-os-x.html it worked beautifully for me

  22. i tried to install the php5 module from http://www.entropy.ch/software/macosx/php , but it didn’t install successfully on leopard. then i deleted the corresponding files and uncomment the line
    LoadModule php5_module libexec/apache2/libphp5.so, as stated above. i don’t know if the php5 module messed with the apache system, but all i get is a not parsed php file. is there anything i can do to make php work?
    thanks

  23. antonio, I’m not sure what to do on your system. You may need to reinstall apache and/or PHP. Check which version of PHP is being called. There may be one in /usr/bin/php and I think entropy installs php5 to /usr/local/php5.

  24. Nathan’s solution is the only one that worked for me. I didn’t realize I needed to change the settings the users/.conf file.

  25. @Jay: Same here… To much random jarbal garbal on the web, Just do what Nathan says… it’s that easy. Here’s how to create that file:$ sudo touch /private/etc/appache2/users/username.confIf you have textmate:$ mate /private/etc/appache2/users/username.confElse use nano or visudo nano /private/etc/appache2/users/username.conf Then put in what Nathan said. Save. Restart Apache: $ sudo apachectl graceful

  26. Thanks! this worked perfectly, just can’t believe apple didn’t do it properly originally!

  27. Thanks so much. I’ve never configured an apache server before; this was extremely helpful.

  28. Hi John,

    Don’t know if you can help me with this problem I created by defining my Apache setup. I’ve been getting this error below, don’t know if you can see what the problem is. I appreciate the help.

    httpd: Syntax error on line 455 of /private/etc/apache2/httpd.conf: Syntax error on line 15 of /private/etc/apache2/extra/httpd-userdir.conf: Syntax error on line 4 of /private/etc/apache2/users/named.conf: Include takes one argument, Name of the config file to be included

  29. @Carlos, If you’re getting a syntax error then there’s probably something misspelled, invalid structure or a tag that apache doesn’t like in one or more of your config files.

  30. @Nathan

    Thanks for posting ‘the apple way’ that’s exactly what I was looking for and it doesn’t require any of the weird setup and is much more flexible and allows multiple folders at the ‘root’ of the users website.

    thanks for the post!

  31. Simply desire to say your article is as astounding.
    The clearness on your post is just nice and that i can think you’re knowledgeable
    in this subject. Well along with your permission allow me to grab your RSS feed to stay up to date with approaching post.

    Thank you 1,000,000 and please carry on the gratifying work.

  32. Thanks for any other great article. Where else
    may anybody get that kind of info in such a perfect way of writing?

    I have a presentation subsequent week, and
    I’m on the look for such info.

  33. cheap donald hawkins jerseys Ԝheгe To Buy Cheap Nike Nfl Jerseys cheap jeremiaah ratliff jerseys

  34. wholesale nba jerseys Уօu dederve tߋ own the cheap jerseys ѕhow yοur
    modeling cheap jerseys