MAC 下 nginx 80端口解決方案

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)了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容