Start an Apache Web Server in Mac OS X El Capitan, Mavericks, & Mountain Lion

Sep 2, 2012 - 65 Comments

Apache web server running in Mac OS X Mountain Lion

The Sharing preference panel options were changed a bit in OS X Mountain Lion and again in Mavericks, and while things like Internet Sharing remain, the Web Sharing preference panel was removed. The Apache web server remains bundled with Mac OS X though, but you’ll need to turn to the command line to enable the web server. Additionally, you’ll want edit a user configuration file for each user account on the Mac to have the personal web sharing feature active. If any of this sounds intimidating or complex, it’s really not, just follow along and you’ll have a simple web server running on your Mac in no time.

Setting Up and Starting the Apache Web Server in OS X

Versions of OS X prior to El Capitan, Yosemite, Mavericks, Mountain Lion and Mavericks can simply turn on “Web Sharing”, but from 10.8, 10.9, 10.10, and 10.11 onward you’ll need to do the following to use a local web server:

  • Launch Terminal, located in /Applications/Utilities/
  • Type the following command, replacing USERNAME with the user account short name:
  • nano /etc/apache2/users/USERNAME.conf

  • Enter the admin password when requested, then paste the following into the nano text editor:
  • <Directory "/Users/USERNAME/Sites/">
    Options Indexes Multiviews
    AllowOverride AuthConfig Limit
    Order allow,deny
    Allow from all
    </Directory>

    In the .conf file it will look like this:
    Apache web server in Mac OS X user configuration file

  • Edit the Directory path USERNAME to the appropriate username
  • Now hit Control+O to save the changes to USERNAME.conf, then hit Control+X to quit out of nano
  • Next, you will start the Apache web server with the following command:
  • sudo apachectl start

  • Launch Safari, Chrome, or Firefox and navigate to “http://127.0.0.1” to verify the server is running, you will see an “It Works!” message

Now that you’ve started a successful Apache server in OS X, you can either modify the core ‘localhost’ files, or go further with the user files.

Apache Web Server Documents Location & User Sites Folders

Note, if you just want to use and modify ‘localhost’ root and not the user level Sites at localhost/~user, you can find the apache webserver files and ‘It Works!’ html in the following location:

/Library/WebServer/Documents/

You can now also visit http://127.0.0.1/~USERNAME/ to see the contents of whatever is stored in the user ~/Sites/ directory – if anything is there per user – and you can add an index.html file or whatever else you’d like to the directory to serve it to the outside world or even just your LAN.

Using http://localhost/ is also fine, and by editing the hosts file you can set a local domain to whatever you want to create a local test environment with an otherwise live domain.

This whole process is quite fast, and can be completed in under a minute as demonstrated in the video walkthrough below:

Shutting Down Apache & Restarting Apache Server

To shut down the web server, go back to the command line and type the following:

sudo apachectl stop

If you make changes to the server and just want to restart it, that can be accomplished with the following command instead:

sudo apachectl restart

The default Apache server is barebones and does not have PHP, MySQL, or anything particularly fancy enabled. You can either install and configure those manually, or you can go the pre-configured route through an all-in-one server app like MAMP, which includes Apache, MySQL, and PHP in a simple to control app-based web server package. You can get MAMP free from here.

Thanks to Ben for the tip idea

.

Related articles:

Posted by: William Pearson in Command Line, Mac OS, Tips & Tricks

65 Comments

» Comments RSS Feed

  1. Jim says:

    After upgrade from Yosemite to High Sierra, I got 404 not found on a localhost user directory, although plain localhost gave “It Works!” as it should. Fix for me was the answer by Hesham (first of six as of 1/19/2018) at StackOverflow

    What worked for me after upgrading from El Capitan to Sierra as well:

    1) Uncommenting from /etc/apache2/httpd.conf

    LoadModule userdir_module libexec/apache2/mod_userdir.so
    LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
    Include /private/etc/apache2/extra/httpd-vhosts.conf
    Include /private/etc/apache2/extra/httpd-userdir.conf
    2) Uncomment from /etc/apache2/extra/httpd-userdir.conf

    Include /private/etc/apache2/users/*.conf
    3) In my /etc/apache2/users/$USERNAME.conf file I had ProxyPass configured as well, and to get it working again I had to re-uncomment the following from my httpd.conf

    LoadModule proxy_module libexec/apache2/mod_proxy.so
    LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
    4) Optional for PHP: You need to re-uncomment the following from the httpd.conf as well:

    LoadModule php5_module libexec/apache2/libphp5.so
    5) Of course, restart apache

    sudo apachectl -k restart
    Things worked for me afterwards

    https://stackoverflow.com/questions/39700357/apache-giving-404-error-after-mac-os-sierra-update/39702677#39702677

  2. garda.ir says:

    What i do not understood is if truth be told how you’re no longer actually much more smartly-liked than you might
    be right now. You’re very intelligent. You know thus considerably in the case of this topic, made me in my opinion imagine it from
    so many varied angles. Its like women and men are not involved unless it is something to do with Lady
    gaga! Your individual stuffs nice. Always maintain it up!

  3. Jane says:

    OK I’m all fixed. Saw my errors and deleted the files I created accidentally manually. Thank God you can still get to the etc directory that way.

    I used this text for 10.8.3 instead of what I saw here and in https://www.youtube.com/watch?v=aEE6ZIonBoM where he’s doing it for 10.11 but had the clearest explanation which I followed to success.

    …and then I fixed the user.conf file and the Sites folder permissions with command-i, because before that it would not let me edit a file in the Sites folder. Now my php sites show up, and it does allow me to edit .php files. I just have to get that control panel back to turn on and off the Sharing without having to use Terminal

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

    But if you cannot be allowed into the /etc/ you are screwed. Don’t know how I got into it on 10.6.8 because it won’t let me there. However, Sites folder and Web Sharing were GUI back then.

    Basically what you are doing is—

    1) You can turn the server on and off in the terminal even if you are a newb at using it and old lady like me (later you put the control panel add-on but mine wasn’t working so I took it off) That’s the first step.

    2) You can get to the /etc/ file manually, but you won’t be able to save changes after erasing the #s, so you need to use the terminal to uncomment your PHP, PERL or whatever other modules you need.

    3) You need to create a SITES folder in your HOME(House) directory because after 10.6.8 they don’t include one, but the tricky part was writing that stuff above into the terminal to make it connect with your localhost/~username and bring up that page with your sites in it.

    4) I then fixed permissions manually using Get Info

    Hit or miss but I did it.

    :)

  4. Apache Captain says:

    For OS X El Capitan and OS X Yosemite, the above works as now to start apache server, but it will enable the root localhost directory only. To get the USER LEVEL “Sites” folders working, so that you can visit ‘localhost/~username’ in the browser for each user account on the Mac, you need to modify the apache configuration in OS X 10.10+ and 10.11+ further as so:

    /etc/apache2/httpd.conf

    Uncomment the following:

    LoadModule userdir_module libexec/apache2/mod_userdir.so

    and

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

    Then in http-userdir.conf you need to uncomment:

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

    Lastly you would need to create /private/etc/apache2/users/NAME.conf if it doesn’t exist.

    I think it should look something like this:

    <Directory "/Users/NAME/Sites/">
    Options Indexes MultiViews
    AllowOverride None
    Require all granted
    </Directory>

    Then restart Apache again. Boom, browser point to localhost/~USERNAME and it will load

    sudo apachectl restart

    Boom, Apache running at the user “Sites” folder

    • Brian Franklin says:

      Yes! Thank you. I had gotten through everything except the “Require all granted” line that needed to replace the previous “Order / Allow” lines.

  5. Krisp1 says:

    Thanks mate! This sorted out Internal Server error.

  6. Hello,
    I am planning to run Apache WebServer on my Macbook OS X ver 10.10.2.
    However, when I try to edit the file /etc/apache2/users/USERNAME.conf
    I get error message
    I have logged in as admin user and the USERNAME substituted is also the same as the admin user

  7. Johan says:

    For Yosemite these instructions works, https://discussions.apple.com/docs/DOC-3083

    else you will have “404 not found”.

    Thanks
    Johan

  8. D says:

    Everything was fine until I got to the sentence “to see the contents of whatever is stored in the user ~/Sites/ directory” which makes no sense since there is no directory with that name anywhere on my computer. Did I miss something? This page also neglects to mention where the “It Works!” text comes from. I searched my entire hard drive, I was unable to find any such text. perhaps I’m having the same problem as David, who posted one day before me.

    • Eric M says:

      The “It works!” text is located in /Library/WebServer/Documents for me (running Yosemite). The path is defined in /etc/apache2/http.conf.

  9. David says:

    Could someone explain how the apache web server is allowing me to access files in my sites directory or take a look at how I am trying to access them now? I have the server running, but am having difficulty displaying test web pages in my sites directory.

    For instance, if I am trying to display index.html, I receive a “Not_Found” error when I try to access it with any of these path names.

    http://127.0.0.1/~My_Name/
    http://127.0.0.1/~My_Name/index.html
    http://127.0.0.1/~My_Name/Sites/index.html

  10. John says:

    The tutorial should mention that you need to enable (uncomment) the following lines:

    1) Open/Edit file β€œ/etc/apache2/httpd.conf”
    2) Uncomment line β€œLoadModule userdir_module libexec/apache2/mod_userdir.so”
    3) Uncomment line “Include /private/etc/apache2/extra/httpd-userdir.conf”

    4) Open/Edit β€œ/etc/apache2/extra/httpd-userdir.conf”
    5) Uncomment line β€œInclude /private/etc/apache2/users/*.conf”

    Good Luck

    • Tomas says:

      Right, that is to set up and start the Apache server in OS X Yosemite, did you try it?

    • Ron says:

      Just got this working for my wife on Yosemite. I had to do what John commented above. Furthermore, due to the version of Apache being greater than 2.4.3, I had to add the following directive to my Directory block:

      Require all granted

      I also had to change to: AllowOverride None
      I was getting an internal server error when AllowOverride was:
      AllowOverride AuthConfig Limit
      I’m guessing this is due to NOT having a .htaccess file in the directory. There was an error message in the apache log file that stated, “…Sites/.htaccess: Options not allowed here”.

      So, the directory block now looks like:
      <Directory "/Users//Sites/”>
      Options Indexes Multiviews
      AllowOverride None
      Order allow,deny
      Allow from all
      Require all granted

      I’m still not certain this is all correct. It works for, but I’d do a lot more investigation if this wasn’t just for home use.

  11. James Burgess says:

    I tried this on 10.8.5 and it didn’t work. I got the following page:

    Forbidden
    You don’t have permission to access /~jrb/ on this server.

    Ok, why is this I wondered. Where is apache writing its log file, that might tell me. So I did this from a Terminal:

    lily:~ jrb$ cd /etc/apache2/
    lily:apache2 jrb$ grep ErrorLog httpd.conf
    # ErrorLog: The location of the error log file.
    ErrorLog “/private/var/log/apache2/error_log”

    Ok so now I know where the log file is, lets have a look at the last few lines:

    lily:apache2 jrb$ tail -5 /private/var/log/apache2/error_log

    [Sat Sep 13 19:04:31 2014] [error] [client 127.0.0.1] (13)Permission denied: access to /~jrb/Sites denied (filesystem path ‘/Users/jrb/Sites’)

    From this I can see I wrote my jrb.conf file correctly because apache is saying it can’t access ‘/Users/jrb/Sites’ which it got from the ~jrb url. Maybe this is just a file system problem? Turns out it was.

    The web server runs as the user and group set in httpd.conf. What is that? Again from the Terminal

    lily:apache2 jrb$ grep User httpd.conf
    # User/Group: The name (or #number) of the user/group to run httpd as.
    User _www

    So there is a user id that OSX has been set up with called _www for the sole purpose of running Apache. Maybe that user id can’t see my Sites folder? Lets find out, the sudo command takes a -u option that’ll let to specify any user (not just the super user):

    lily:apache2 jrb$ sudo -u _www ls /Users/jrb/Sites
    Password:
    ls: /Users/jrb/Sites: Permission denied

    Aha! This is probably why I’m getting that error, at any rate apache will not be able to serve files in my Sites directory before this works.

    Lets see what the permission are on my home folder:

    lily:apache2 jrb$ ls -l /Users
    total 0
    drwxrwxrwt 13 root wheel 442 Apr 11 15:35 Shared
    drwx—— 131 jrb staff 4522 Sep 13 19:00 jrb

    This will be very familiar to UNIX/Linux users but not mean much to anyone else but the 6 ‘-‘ chars after rwx mean that anyone other than me on this machine can not look inside the ‘jrb’ folder. I’m not going to try to explain that or why the next command will fix it but what I’m going to do is add read and execute permission to both the staff group and anyone else (other)

    lily:apache2 jrb$ chmod go+rx /Users/jrb

    Let’s check that did the right thing:

    lily:apache2 jrb$ ls -l /Users
    total 0
    drwxrwxrwt 13 root wheel 442 Apr 11 15:35 Shared
    drwxr-xr-x@ 131 jrb staff 4522 Sep 13 19:00 jrb

    Yes, the last three – changed to r-x, that’s for “others” and the 3 before that did also, that’s for the “staff” group.

    Now let’s see if apache can not read my Sites folder:

    lily:apache2 jrb$ sudo -u _www ls /Users/jrb/Sites
    Password:
    images index.html iwebtest

    Great, that worked.

    OK, well now does http://localhost/~jrb/ work? Yes, yes it does. Phew. OSX put that index.html file in there (and images and iwebtest) to let you know apache is working. You can delete them and start putting your own stuff.

  12. SRJSyd says:

    Nope. Like everything in the $%^* open source world it just DOESN’T work. I have tried a dozen times, created a Sites directory, changed Administrator permissions to allow read/write access to the normal user directory – still just get access forbidden…

    I run a user account WITHOUT admin permissions – for obvious reasons. This means logging in and out of the user and Administrator account every time I try this.

    I had similar problems a few years ago trying to get Ruby On Rails working – I did, eventually, but it took an age.

    And, of course, in the meantime you are working in the most user-unfriendly environment imaginable – deep under the hood of the operating system which turns Unix into something a sane person would buy.

    I cannot imagine why Apple do not take this in hand and put a proper front end on all this sh*t from the open source world so you can just press a button and install it, like everything else in the Mac environment.

    I note that many of the users on here who got this to work had to make changes – and not the same ones.

    Le plus ca change…

  13. ugomena says:

    Great reference!

    Upgraded (Mac OS 10.4) Apache1.3 site to Apache2 (10.8). Uhhhhgggg! What a pain.

    @Torbin thank you for the tip: httpd -t saved me so much time. Really did not feel like digging into my conf files to figure out what the syntax errors were.

  14. chia says:

    thumbs up! it works perfectly. easy tutorial

  15. Peter L says:

    I hit some confusion regarding the “users”-directory, isn’t the same reference used to the apache folder and later to the home folder? I went mamp and noticed the reference to the home folder afterwards.

  16. Peter L says:

    I hit some confusion regarding the “users”-directory, isn’t the same reference used to the apache folder and later to the home folder? I went mamp and noticed the reference to the home folder afterwards.

  17. Rana says:

    I got 403 Forbidden, You don’t have permission to access this server. Please any solution to fix thing problem

  18. Darrin says:

    I’m a bit new to this so please excuse my ignorance. I was wondering how I would go about adding my index.html page to the server. Do I add it through nano?

  19. William says:

    This worked perfectly except that i needed to make the “Sites” folder. I just wondered if it is possible to show the index.html file without entering a user?

  20. Pod says:

    Well looks like 10.83 broke Apache again. I normally do everything in this webpage with each update and now I get a “Forbidden: You don’t have permission to access / on this server”. The nearest I can figure is that the 10.83 update has munged permissions somewhere. Apple really needs to get on the ball with this – apache on OS X used to be the most painless thing I can imagine and now it’s some of the most painful. Ideas?

    • Paul says:

      I was able to confirm that Apache still works in OS X 10.8.3, make sure you have everything configured properly and use sudo to launch apache:

      sudo apachectl start

      If you’re encountering permissions errors, you can also try to point apache at a different directory with the -d flag:

      sudo apachectl start -d /Path/To/Directory/

    • Shiran says:

      For someone who is having this problem today, usually this is because you don’t have “Sites” directory under the current user.

      Go to finder, Commmand+Shift+G and you get the full file system access. Navigate to /users/currentuser/ and create a “sites” directory.

      Jeez, this is the first time I’m doing mac development and I almost it up to this silly issue. I’m only hopeful there won’t be many stupid ones like this.

  21. tim says:

    and what about http sharing not only localhosting ??

  22. David Bagdasarian says:

    Hi Guys,

    For everyone that’s having trouble with accessing localhost/~user, make sure that you have a folder called Sites under your user directory. I had to create one and drop in an index.html file for it to show up.

    Hope this helps.

  23. Berik Visschers says:

    As most of the mac computers are used by only one user, the per-user configuration is a bit overkill. I would suggest using these changes in the http.conf file:

    Switch off the userdir_module, by inserting a # in front of this line:
    #LoadModule userdir_module libexec/apache2/mod_userdir.so

    Set the documentRoot to the Sitesin your home directory:
    DocumentRoot “/Users//Sites/”

    Give permission to acces your Sites directory
    #
    # This should be changed to whatever you set DocumentRoot to.
    #
    <Directory "/Users//Sites/”>

    Everything in ~/Sites will be accessible from http://localhost and http://.

    Cheers!

    • Taylor Marks says:

      I tried making the changes you suggested but when I try accessing the site I get:

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

      This is what I have for the Directory permissions:

      Options Indexes FollowSymLinks Multiviews
      AllowOverride AuthConfig Limit
      Order allow,deny
      Allow from all

      So it looks to me like / should definitely be allowed.

  24. Torben Brams says:

    For what it is worth, I was struggling with this for a while and found a nice way to debug and fix my problem.

    From the terminal, run “httpd -t” to get a syntax check of the config files (I guess a lot of people before me has been typing wrong). I got this message, that was very easy to understand and fix:

    tbrams$ httpd -t
    httpd: Syntax error on line 471 of /private/etc/apache2/httpd.conf: Syntax error on line 15 of /private/etc/apache2/extra/httpd-userdir.conf: Syntax error on line 3 of /private/etc/apache2/users/tbrams.conf: </Directory without matching <Directory section…

    And then, my Site directory was working as expected.

  25. Paul says:

    I’ve done all this stated above but I still get a 404 error page not found when I go to localhost/~paul
    localhost gives me the welcome to server page.
    I’m running it on a server mac mini.

  26. John Valean Baily says:

    Doesn’t work.

  27. Mottel says:

    I keep getting stuck at the
    nano /etc/apache2/users/Mottel.conf
    Terminal doesn’t ask for my password but when I go to save the changes it says
    [ Error writing /etc/apache2/users/Mottel.conf: Permission denied ]

    • ReasonOverFaith says:

      Yeah got that too, instead I just used vi and saved, but you can also use a normal text editor and copy the file using Finder too

      • Dave says:

        terminal doesn’t ask for a password when I open the nano editor and I also get: Error writing /etc/apache2/users/davidleslie.conf: Permission denied ]

        I have tried to locate the file using finder but i cant even find the directory… any help?

  28. Shanimal says:

    http://127.0.0.1/MyUser resolves like its supposed to but I can’t get http://127.0.0.1/ to show up. I get a 404

    Not Found
    The requested URL / was not found on this server.

  29. Sam says:

    I can get the it works! message but when trying to get to localhost/~sam or localhost/~Sam or 127.0.0.1/~Sam i get
    You don’t have permission to access /~Sam on this server.

    any ideas please?

    here is my conf file looked it over a hundred times

    Options Indexes Multiviews
    AllowOverride AuthConfig Limit
    Order allow,deny
    Allow from all

  30. Mate I am having a hard time. I want to use it for my IP of my computer, like Web Sharing used to do it, not via local host.

    I don’t understand why OSX has done these little stupid changes. Web Sharing affected NOBODY yet they removed it.

  31. Jason B says:

    Here is a simple preference panel that can toggle the web server on and off

    http://clickontyler.com/blog/2012/02/web-sharing-mountain-lion/

    More fool proof for newbs or anyone afraid of terminal.

    • Jane says:

      That screwed me up too will not work now either, It says off when the server is on.

      Also how to fix my errors? I used the command to find the errors and it said:

      Closing command was missing it’s /
      Don’t know how to fix it.

      I also accidentally created a username.com file instead of a username.conf file which shows up when I do ls

      This sucks. In 10.6 there was a Sites folder, and WebSharing button, and you only had to finagle with the .conf file for PHP5 to turn that on by diddling with permissions in the get info and removing the # from the PHP5 string.

      I am about ready to scream….

  32. Bruce says:

    Definitely use MAMP instead of this…much easier. http://www.mamp.info/en/index.html

  33. ianf says:

    Any particular reason why this wouldn’t
    work in Mac OS X Lion (10.6.x) that also
    comes equipped with Apache Web Server?

    • DPM says:

      Previous versions of OSX just needed you to turn on Web Sharing in the control panel for the same thing.

      This hint is mostly about restoring the same functionality in Mountain Lion that was taken away, including configuration of the webserver the same way as before.

  34. Bill H says:

    Thanks for sharing this. I’d love to see an article like this describing the steps for enabling VPN without installing OSX Server.

    • Matt says:

      Easy inbound VPN is just a matter of turning on Screen Sharing and then opening the port on a router if you’re behind one.

  35. ddr says:

    can’t remember the name, but there’s a free preference panel that has a big on/off switch to do this in 10.8 too

  36. John Scott says:

    Hi William,

    Following your instructions I was able to get the “It works!” page. However the second part (accessing /~USERNAME folder) is giving me this response
    Forbidden
    You don’t have permission to access /~USERNAME/ on this server.

    Any insights?
    Thanks

    • John Scott says:

      **–> My apologies! <–**

      I missed due diligence copying and pasting into the .conf file. I forgot to change USERNAME to my name.

      Thanks again.

      • David Mapleton says:

        I made the same mistake by copying and pasting, just remember to literally replace USERNAME with your account user name like this using my name David

        nano /etc/apache2/users/David.conf

        And for the top line it’s

        Directory “/Users/David/Sites/”

        Then put up an index.html page and away you go. Great place to store a selfmade bookmark start page BTW.

    • Chaitanya Rahalkar says:

      use the command sudo su – to have root access

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site