How do I start apache in OSX Mountain Lion / Mavericks?

71

46

OSX Mountain Lion removed the GUI for web sharing, but apache is still installed.

How do I start and stop the service? How do I get the service to automatically launch on startup?

slifty

Posted 2012-07-30T19:46:37.970

Reputation: 1 569

Its starts automatically, at least for me. Just type 0.0.0.0 it should be there. – Wojciech Bednarski – 2012-08-02T21:51:22.177

@Lri No. I did upgrade from 10.7 – Wojciech Bednarski – 2012-08-03T16:29:16.763

Answers

79

The LaunchDaemon for Apache is still there as well, so all you have to do it load it:

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

The -w makes it permanent, i.e. it'll be reloaded when you reboot.

Gordon Davisson

Posted 2012-07-30T19:46:37.970

Reputation: 28 538

How do you turn this off once it's started this way? – James McMahon – 2012-09-05T14:13:17.727

9@JamesMcMahon: sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist – Gordon Davisson – 2012-09-05T14:14:17.130

Hmm, something is still running on localhost, I had assumed it was Apache, maybe I have something else running. – James McMahon – 2012-09-05T14:19:09.073

@JamesMcMahon: You can use e.g. sudo lsof -i:80 to see what process is listening on a given port. From there, this previous answer might help you track it down...

– Gordon Davisson – 2012-09-05T14:58:19.927

35

Terminal Commands

Starting: "sudo apachectl start"

Stopping: "sudo apachectl stop"

Restarting: "sudo apachectl restart"

Launching on Startup

(See the marked answer in this thread)

slifty

Posted 2012-07-30T19:46:37.970

Reputation: 1 569

Is there an equivalent ShutdownItems dir? Don't have OSX in front of me at the moment... – Rich Homolka – 2012-07-30T20:33:33.887

There used to be /etc/rc.shutdown.local -- not sure if that is still around... – slifty – 2012-07-30T20:37:08.873

12

sudo apachectl start also enables the launchd daemon. It overrides the Disabled key in /System/Library/LaunchDaemons/org.apache.httpd.plist by modifying /private/var/db/launchd.db/com.apple.launchd/overrides.plist, just like launchctl load -w.

For http://localhost/~username/ to work, you have to create /etc/apache2/users/username.conf and add a <Directory> directive like this:

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

Lri

Posted 2012-07-30T19:46:37.970

Reputation: 34 501

1

I found a really great writeup of this, here. Also covers php module enablation. http://coolestguyplanettech.com/downtown/install-and-configure-apache-mysql-php-and-phpmyadmin-osx-108-mountain-lion

– Peter Ehrlich – 2012-10-13T19:35:37.743

8

Slifty and Lauri hint at an important concept without clearly saying it.

"sudo apachectl start" will not only start apache but will also modify /private/var/db/launchd.db/com.apple.launchd/overrides.plist so that apache will automatically restart each time the system is rebooted.

Similarly, "sudo apachectl stop" will stop apache and modify the above file so that apache won't start when the system is rebooted.

This seems to be the simplest way to control whether the service starts automatically.

aeropapa17

Posted 2012-07-30T19:46:37.970

Reputation: 181

3

For those in search of a GUI, Click On Tyler has developed a control panel app, http://clickontyler.com/blog/2012/02/web-sharing-mountain-lion/

GUI sample

James McMahon

Posted 2012-07-30T19:46:37.970

Reputation: 3 010

This is cool.. But outdated, doesn't work on Yosemite. How do we remove this from our settings pane? – emotality – 2015-02-21T23:37:50.780

To Delete: ~/Library/PreferencePanes – emotality – 2015-02-21T23:38:43.943

Sorry, this is no longer available, or became part of a commercial product that you can only download complete - not just the preference panel. Any alternatives? – Motti Shneor – 2016-11-16T08:48:37.287

Honestly better to use ngnix at this point. – James McMahon – 2016-11-20T01:57:56.080

Nice widget.. however in my system something went wrong and apache is not starting..:( – Dilip Rajkumar – 2012-10-14T05:20:31.903

Same here. Seems to be a little bit sloppy coded - good old confusion about the .pid-file I guess ...zzz – conny – 2012-11-02T12:14:36.130

1

This preference pane has the links for computer site folder and to open a browser : http://stowlake.com/WebServer

Danny Swarzman

Posted 2012-07-30T19:46:37.970

Reputation: 19

This rather ugly UI's preference panel crashes immediately as you try to start the web-server on both OS-X 10.11 and 10.12. Crash-logs available at your request ;) – Motti Shneor – 2016-11-16T08:53:22.370