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

Sep 2, 2012 - 41 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 Mountain Lion and Mavericks can simply turn on “Web Sharing”, but from 10.8 and 10.9 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 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

You can now also visit http://127.0.0.1/~USERNAME/ to see the contents of whatever is stored in the user ~/Sites/ directory, 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

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

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

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

41 Comments

» Comments RSS Feed

  1. 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.

  2. 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

  3. 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.

  4. 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.

  5. Bruce says:

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

  6. 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.

  7. 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.

  8. 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

  9. 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.

  10. 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?

  11. John Valean Baily says:

    Doesn’t work.

  12. 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.

    • Mike says:

      You may have to restart apache. Type sudo apachectl stop then sudo apachectl start. Also, be sure you have a Sites folder in your home directory.

  13. 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.

  14. 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.

  15. 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.

  16. tim says:

    and what about http sharing not only localhosting ??

  17. 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.

  18. 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?

  19. 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?

  20. Rana says:

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

  21. 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.

  22. 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.

  23. chia says:

    thumbs up! it works perfectly. easy tutorial

  24. 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.

  25. 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…

  26. Hello my family member! I want to say that this post
    is awesome, nice written and include almost all vital infos.
    I would like to peer more posts like this .

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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