warem
warem

Reputation: 1683

mac apache start in a terminal failed

I want to start Apache in Mac. If I make web sharing enable by checking in system prefs -> web sharing, Apache works. But if I input command in terminal: sudo apachectl start, Apache doesn't start. In this case, when run http://localhost in Safari, it always said The requested URL / was not found on this server. In both cases, I used the default httpd.conf.

Therefore, How can I start Apache and make it work in Mac by inputting command in a terminal instead of by the graphical config window of mac itself? I prefer to command in a terminal.

Thank you.

Upvotes: 0

Views: 2614

Answers (1)

warem
warem

Reputation: 1683

Figure it out. The point is from the words of /etc/apache2/httpd.conf:

The <IfDefine> blocks segregate server-specific directives and also directives that only apply when Web Sharing or server Web Service (as opposed to other services that need Apache) is on. The launchd plist sets appropriate Define parameters.

The action of Web Sharing in mac (system prefs -> web sharing) in nature first launches plist to set appropriate Define parameters, then calls apachectl. If we read httpd.conf, we can find the file is structured in several sections -- <IfDefine xxxx>yyyyy</IfDefine> based on the parameters from plist.

If we call the command sudo apachectl start in terminal like what we usually do in linux, the Define parameters from plist can't be set. Although httpd is up, it is not configured appropriately, e.g., in my case, DocumentRoot is not set at all because the parameter is surrounded by

<IfDefine !MACOSXSERVER>
<IfDefine WEBSHARING_ON>
    DocumentRoot "/Library/WebServer/Documents"
</IfDefine>
</IfDefine>

This is why the error log said [error] [client 192.168.1.2] File does not exist: /usr/htdocs. As to /usr/htdocs, it is not set in any configure file but is hardcoded in Apache. It is the "last resort" place to look for.

If we comment #<IfDefine !MACOSXSERVER> and #<IfDefine WEBSHARING_ON> and the two #</IfDefine>, call sudo apachectl start in terminal. Then everything is ok.

It took me a full day of work to finally straighten this out. I hope it is useful to those guys who are used to be Linux and especially prefer to command line in terminal like me and just move to Mac.

Upvotes: 1

Related Questions