nginx 安裝 通過(guò)brew
$:brew install nginx
launchd
關(guān)于launchd的描述,這里引用一下manpage,如下:
launchd manages processes, both for the system as a whole and for individual users.
The primary and preferred interface to launchd is via the launchctl(1) tool which (among other options) allows the user or administrator to load and unload jobs.
Where possible, it is preferable for jobs to launch on demand based on criteria specified in their respective configuration files.
launchd also manages XPC services that are bundled within applications and frameworks on the system.
During boot launchd is invoked by the kernel to run as the first process on the system and to further bootstrap the rest of the system.
You cannot invoke launchd directly.
launchd用于為系統(tǒng)和用戶管理進(jìn)程,主要通過(guò)launchctl對(duì)其進(jìn)行操作,用戶無(wú)法直接調(diào)用launchd。
啟動(dòng)時(shí)由內(nèi)核調(diào)用launchd,運(yùn)行第一個(gè)進(jìn)程。
plist
另外我們需要了解的就是plist文件。
plist就是property list format的意思,是蘋(píng)果用來(lái)保存應(yīng)用數(shù)據(jù)的格式,其實(shí)就是個(gè)xml。
可以在 /usr/local/opt/nginx 下找到nginx對(duì)應(yīng)的plist文件,比如在我的電腦上是 homebrew.mxcl.nginx.plist 。
它的內(nèi)容大概是這樣:
<p><code>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>homebrew.mxcl.nginx</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/usr/local/opt/nginx/bin/nginx</string>
<string>-g</string>
<string>daemon off;</string>
</array>
<key>WorkingDirectory</key>
<string>/usr/local</string>
</dict>
</plist>
</code></p>
我們需要把這個(gè)文件復(fù)制到 /Library/LaunchDaemons 下,如果是 ~/Library/LaunchAgents 也可以,但兩者有區(qū)別。
前者是系統(tǒng)啟動(dòng)時(shí)啟動(dòng),后者則是在用戶登錄時(shí)啟動(dòng)。 接著執(zhí)行l(wèi)aunchctl load -w,如下:
sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
最后,重啟你的機(jī)器,你會(huì)發(fā)現(xiàn)nginx在80端口啟動(dòng)了。