Space Cat, Prince Among Thieves

Configure Apache for PHP in OS X / macOS

Every time I would upgrade OS X it took probably 45 minutes of fiddling with my Apache configuration to get it working properly. I'm writing this article in the hopes of saving myself and others some time.

The Setup

For the following examples, {username} represents your username.

Firstly, if you don't already have a Sites folder, create one in your home directory.

$ mkdir ~/Sites

Create or update /etc/apache2/users/{username}.conf to look as follows:

<Directory "/Users/{username}/Sites/">
    Options Indexes MultiViews FollowSymLinks
    Require all granted
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

Add your virtual hosts if you don't already have them to /etc/apache2/extra/httpd-vhosts.conf

ala:

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /Users/{username}/Sites/
</VirtualHost>

Configuration

/etc/apache2/httpd.conf

Search for and uncomment the following lines:

LoadModule deflate_module libexec/apache2/mod_deflate.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php5_module libexec/apache2/libphp5.so

Include /private/etc/apache2/extra/httpd-userdir.conf
Include /private/etc/apache2/extra/httpd-vhosts.conf

Then around line 271 add index.php to the DirectoryIndex

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

/etc/apache2/extra/httpd-userdir.conf

Uncomment the following line:

Include /private/etc/apache2/users/*.conf

Almost Done

Restart apache

$ sudo apachectl restart

open a browser and test it out.


Comment by: Simon on

Thank you for taking time to share this with everyone. Saved me a lot of time :-)

Comment by: Dan on

Nice work - Thanks!

Comment by: Nu on

Thanks for posting these instructions, it's really helpful.

Comment by: Reyescult on

This is awesome, thanks! Can you offer any insight as to getting SSI (.shtml) working? Previously to Yosemite, I was using the following guide:

http://www.456bereastreet.com/archive/201104/apache_with_virtual_hosts_php_and_ssi_on_mac_os_x_106/

Comment by: Jesse G. Donat on

There are two lines commented out as follows in /etc/apache2/httpd.conf, uncommenting them should do the trick!

    #AddType text/html .shtml
    #AddOutputFilter INCLUDES .shtml

Comment by: Dmas on

Hi, why mi apache directory is this: /usr/local/Cellar/apache24/2.4.4/ ? i have osx yosemite and i can't add php module.

Regards

Comment by: John on

Hi, great work. Thank you. I had issues having upgraded to Yosemite and this is the only guide I found that actually solved the issue.

Comment by: Daniel on

You are an angel, thank you!

Comment by: Radu on

Thank you man! Works like a charm.

Comment by: Devlix on

Man, after 2 hours of rage and fury, this post extended my mac's lifetime :D Thanks for sharing this!

Comment by: Freya on

I keep getting this error: You don't have permission to access / on this server. Did everything what you told ...

Comment by: alBz69 on

Thank you very much! sweety guide, dude ^_^ All works for me.

Comment by: Roy on

Great job, saved my a lot of time! Best tutorial I've seen. I'd like to add the following for people who are not super handy with the Terminal app (like me). Use "sudo nano" before every path to open and edit the files.

Comment by: Jeff on

Thanks! I wish your post came up first on Google. It would have saved me even more time.

Comment by: Fortyhands on

This is probably the fifth article I've looked at regarding setting up Apache in Yosemite - and the only one that actually worked flawlessly. Thanks for your awesomeness.

Comment by: Drew on

I figured it would be some work after upgrading to Yosemite but couldn't figure out what had happened to my previous set up. I followed two other tutorials and yours was the only one that worked. Totally saved my sanity.

Comment by: Denis Karanja on

Thank you so much man!! I have been struggling with this for two weeks now. Thanks man!!

Comment by: Mark on

Great tutorial. Worked perfectly. Only thing I noticed in your instructions you have a typo /Users/{userbane}/Sites/ I thought that was some special system setting until I realized it was {username}

Comment by: KM on

Am able to configure Apache on yosemite successfully. Thank you

Comment by: Mieke on

Thanks for the quick and easy tutorial. Great

Comment by: Jeff on

thanks, this is really helpful specially for newbies to apache like me...

Comment by: Peter on

Thanks for the tutorial. One quick question. I have a java war file that I'd like to deploy to apache. I'm not sure how to go about doing this on OS X. On windows, I had the Manager GUI and would upload the war file and click deploy. It was pretty easy (I'm a little disappointed as a new Mac convert to find out how much easier it is to deploy a war file to Tomcat).

I've been battling this for hours. If you have any suggestions, I'd really appreciate it. Thanks

Comment by: barfo on

Remember to comment out Require all denied in httpd.conf

Comment by: Anhduc.bkhn on

Thanks you!

Nice post

Comment by: mark on

Thanks man! You saved me a lot of work!

Comment by: Kees de Kooter on

Please note that the Apache version on El Capitan is 2.4 and note these configuration changes: https://httpd.apache.org/docs/2.4/upgrading.html

Comment by: Derek on

You are a legend

Comment by: Abubakar Saddique on

I have followed the every step but still having following error in browser while access localhost.


Forbidden

You don't have permission to access / on this server.

Comment by: Tibo on

Thanks a lot – worked like a charm!!

Comment by: dd on

I've tried everything still can't get Permalinks in Wordpress to work properly...

Comment by: Nahid on

Thank you. You just save my lots of time :)

Comment by: Huiyu Lee on

Hey, have you also encountered this problem: http://stackoverflow.com/questions/33532949/el-capitan-unable-to-connect-to-localhost-server-via-external-device-in-lan
this is the one that I cannot figure out till now.
I do the above things as you did. and I use my mobile phone( which is connected to the same local area network as my web server) and open the browser as the link http://ip but nothing returned.

Comment by: Jesse G. Donat on

Have you tried turning your firewall off to see if that's the issue? Otherwise you may need to change something in your apache config to accept outgoing connections.

Comment by: Ben on

Thanks!! Got my virtual host working!

Comment by: A on

Thank you!

Comment by: Tom on

Thanks. Great help.

Comment by: Philbert on

Thank you! :-)

Comment by: Tony on

Was dreading updating to 10.11
This made it so easy.

Thanks!

Comment by: Joshua Pinter on

Awesome, thanks man! Clear and concise. And most importantly, worked like a charm! :)

Comment by: Kam on

For some reason I can't access plain localhost when I use this. I get a "Forbidden" message. But now it's serving the file from my Sites directory so I'm happy.

Comment by: Rufus on

You are awesome :)
Very helpful article
it solved my problem
Thank you.

Comment by: zhoulujun on

wo copy it ^^
http://www.zhoulujun.cn/zhoulujun/index.php?m=content&c=index&a=show&catid=186&id=7855

Comment by: Ondrej on

You're the man. Cheers.

Comment by: Alla on

Hello! I am very thankful to you for this article! It saved me. I am just learning, and after more than a week of struggling with setting up AMP on my Mac OS 10.7.5, I have finally decided to upgrade to El Capitan in hope that AMP will not cause any trouble. I did so yesterday, and it took me hours before I have finally found your article that has helped me, and apache finally runs. Thank you!

Comment by: seung-kil on

Thank you! It solved my problem !

Comment by: Jose on

Thanks!!! It works perfect :)

Comment by: Jenoah on

Thanks!
It worked for me.
I just couldn't find a tutorial on Google on how to fix the permission issue, but now I have found one!
Thanks :)

Comment by: Jakob on

Thank you Man i searched for this for about 2 Days!! Good Tutorial! Recommended!

Comment by: Mekaeil on

Hi Jesse
my OS is MAC OS Sierra version 10.12.4

I did step by step your article... but when i want to create the folder in Sites folder it 's redirected to XAMPP dashboard !

and when Uncomment the line :
( /Applications/XAMPP/xamppfiles/etc/httpd.conf )

#Include etc/extra/httpd-vhosts.conf

when Uncommented , the Sites folder worked but the localhost Access forbiden 403!

Comment by: ALEXEI ZOUBOV on

Went through everything, still when trying http://localhost/~alexeiz I get:
Forbidden

You don't have permission to access /~alexeiz on this server.

I've searched and tried all recommender solutions for hours - the result is still the same.

I have a fresh install of El Capitan on a separate bootable disk

Comment by: tim on

Thanks for the share, I spent 3 hours researching for an answer.

One issue I was having was syntax issues. Somewhere things got broken.

Tip: "Check Apaches syntax", in my case, I had a few issues in some files.

sudo apachectl configtest

It's best to start over with a fresh default httpd.conf or httpd-vhosts.conf file. Get a clean copy from the originals directory... /etc/apache2/original

Comment by: Chris on

You are the best!!!!!!!!

Comment by: John Clarke Au on

Thanks for sharing your knowledge.
This worked fine for Mojave, except I have to use PHP7 instead of PHP5.

Comment by: Juan Andrade on

I just really want to thank you . Im working with Mojave PHP 7 Apache 2.7 OS X 2019 you really help me a lot. Thanks Genius.

Comment by: Francisco on

I did exactly as you described in all steps but nothing seems to happen.

Have no idea what's wrong, I have MacOS Mojave 10.14.4.

When I go to http://localhost I keep seeing "Apache!! It works!"

Comment by: Antoine on

Thank you very much in 2020 !
You did such an amazing tutorial. You're a star :D

Everything worked properly for me on Mojave.

Comment by: Stevik on

I initially had everything going just using a localhost PHP server out of the box. THEN. The localhost just quit on me. I followed this entire manual and I am still getting an error: unknown order in private/etc/apache2/users/{username}.conf

I will continue looking at documentation to figure this out as I am building a site and this is quite frustrating lol...I am from the electrical engineering degree and have not been coding for a while. So be patient if you choose to answer me.

Cheers! Good Tutorial.

Email address will never be publicly visible.

Basic HTML allowed.