Mac Pro 開機自啟動 PHP-FPM,Nginx,MySql 等軟件


在Mac下安裝好了PHP開發環境(PHP-FPM,Nginx,MySql), 想設置成開機自啟動,原來以為和一般的Linux系統一樣,也是在rc.d這樣目錄放置啟動腳本。在網上查了一些資料,發現蘋果應該是把它完全封閉了,只能利用Mac系統里的Launchctl來做這個事。

Launchctl 其實就是寫一個 *.plist 的文件,它的作用和 Linux 里的 Crontab 的作用是一樣的。下面以自啟動 MySql 為例

1、新建文件 com.mysql.plist,但這個文件需要放到 /Library/LaunchDaemons 目錄下

sudo vim /Library/LaunchDaemons/com.mysql.plist

其內容如下:

<!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>com.mysql</string>  
        <key>ProgramArguments</key>  
        <array>  
                <string>/usr/local/mysql/bin/mysqld_safe</string>  
        </array>  
        <key>RunAtLoad</key>  
        <true/>  
        <key>KeepAlive</key>  
        <true/>
</dict>  
</plist>

2、注冊這個 plist 文件到系統服務。

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.plist  

注意:卸載命令為

sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.plist 

3、修改執行權限

sudo chown root:wheel /Library/LaunchDaemons/com.mysql.plist
sudo chmod +x /Library/LaunchDaemons/com.mysql.plist

 

4、其他開發軟件(nginx、php-fpm)同理配置

/Library/LaunchDaemons/com.nginx.plist

<!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>com.nginx</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/nginx/sbin/nginx</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
</dict>
</plist>

/Library/LaunchDaemons/com.php-fpm.plist

<!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>com.php-fpm</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/local/php/sbin/php-fpm -D</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
</dict>
</plist>

 

 

可能的【報錯

1、執行 launchctl 的時候,出現 Dubious ownership on file (skipping) 這個錯誤

原因是:這個plist文件必須是屬於 root 用戶,wheel 組,用 chown 修改即可。

sudo chown root:wheel /Library/LaunchDaemons/com.mysql.plist

 

2、執行 launchctl 的時候,出現 launchctl: no plist was returned 這個錯誤

原因是:plist文件內容可能有格式錯誤。

用下面這個命令可以查看文件格式是否有問題,我就是用這個查看到 有一個 <true> 標記沒有閉合。

sudo plutil -lint /Library/LaunchDaemons/com.mysql.plist

 

注意:php-fpm.confnginx.conf 等配置文件中的 用戶名 和 用戶組 設置為當前登錄用戶,即 jianbaostaff

 

延伸閱讀:

Mac 用戶組:staff、 wheel、admin 的區別

Mac Pro 解壓安裝MySQL二進制分發版 mysql-5.6.30-osx10.11-x86_64.tar.gz(不是dmg的)

Mac Pro 編譯安裝 Nginx 1.8.1

Mac Pro 編譯安裝 PHP 5.6.21 及 問題匯總

 

 

參考:

https://www.nginx.com/resources/wiki/start/topics/examples/osxlaunchd/

https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

https://sexywp.com/use-launchd-to-start-shadowsocks.htm

 


  

注意:

個人用戶 開機自啟動 數據保持在 /Library/LaunchDaemons/ 目錄,而系統級別的 保存在 /system/Library/LaunchDaemons/ 目錄。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM