爲Mac配置服務器nginx

我們的項目基於SSI技術實現前後端完全分離,同事都用Apache,我比較喜歡nginx。

安裝方法源自:http://www.codingcool.com/2013/07/18/在mac-os-x-10-9上編譯安裝nginx/

接着往下轉:

1.先安裝PCRE庫(轉註:nginx rewrite依賴該庫)

可以在這裏下載最新版,我這裏使用的是8.33的版本然後在終端執行下面的命令。

  1. cd ~/Download
  2. tar xvzf pcre-8.33.tar.gz
  3. cd pcre-8.33
  4. sudo ./configure --prefix=/usr/local
  5. sudo make
  6. sudo make install

2.下載安裝nginx

首先在nginx官網下載最新的源碼,我這裏用的是nginx-1.5.2

  1. tar -zvxf nginx-1.5.2.tar.gz
  2. cd nginx-1.5.2
  3. ./configure --prefix=/usr/local/nginx --with-cc-opt="-Wno-deprecated-declarations"
  4. make
  5. make install

2015年03月19日注:在最新版Mac系統 10.10+中,nginx編譯過程需要用到的MD5相關API已經是deprecated的了,會報錯,類似於

  1. cc1: warnings being treated as errors
  2. src/core/ngx_crypt.c: In function ngx_crypt_apr1’:
  3. src/core/ngx_crypt.c:76: warning: MD5_Init is deprecated (declared
  4. at /usr/include/openssl/md5.h:113)
  5. src/core/ngx_crypt.c:77: warning: MD5_Update is deprecated
  6. (declared at /usr/include/openssl/md5.h:114)
  7. ....

解決方法就是在配置時候增加參數如下:

  1. ./configure --with-cc-opt="-Wno-deprecated-declarations"

默認編譯概要:

  1. Configuration summary
  2. + using system PCRE library
  3. + OpenSSL library is not used
  4. + md5: using system crypto library
  5. + sha1: using system crypto library
  6. + using system zlib library
  7.  
  8. # 默認編譯參數對應的安裝路徑(*_temp 爲目錄)
  9. nginx path prefix: "/usr/local/nginx"
  10. nginx binary file: "/usr/local/nginx/sbin/nginx"
  11. nginx configuration prefix: "/usr/local/nginx/conf"
  12. nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  13. nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  14. nginx error log file: "/usr/local/nginx/logs/error.log"
  15. nginx http access log file: "/usr/local/nginx/logs/access.log"
  16. nginx http client request body temporary files: "client_body_temp"
  17. nginx http proxy temporary files: "proxy_temp"
  18. nginx http fastcgi temporary files: "fastcgi_temp"
  19. nginx http uwsgi temporary files: "uwsgi_temp"
  20. nginx http scgi temporary files: "scgi_temp"

爲了方便:(轉註:我選擇這個方式)

  1. sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
  2. sudo ln -s /usr/local/nginx/conf /etc/nginx
  3. sudo ln -s /usr/local/nginx/logs/nginx.pid /var/run/nginx.pid
  4. sudo ln -s /usr/local/nginx/logs /var/log/nginx

或者直接在編譯時設定

  1. ./configure \
  2. --sbin-path=/usr/local/bin/nginx \
  3. --conf-path=/etc/nginx \
  4. --pid-path=/var/run \
  5. --error-log-path=/var/log/nginx \
  6. --http-log-path=/var/log/nginx

編譯參數參考 Nginx InstallOption

3.啓動Nginx

檢查PATH環境變量

  1. # ~/.bash_profile export PATH=/usr/local/bin:/usr/local/sbin:$PATH

啓動Nginx

  1. sudo nginx

需要停止Nginx的時候運行

  1. sudo nginx -s stop

4.配置自啓動

創建文件 /System/Library/LaunchDaemons/nginx.plist

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <key>Label</key>
  6. <string>nginx</string>
  7. <key>KeepAlive</key>
  8. <true/>
  9. <key>Program</key>
  10. <string>/usr/local/bin/nginx</string>
  11. <key>RunAtLoad</key>
  12. <true/>
  13. </dict>
  14. </plist>

載入自啓動文件

  1. launchctl load -w /System/Library/LaunchDaemons/nginx.plist

2015年03月24日後記:
這次是在RMBP OSX Yosemite系統安裝nginx 1.6.2, 按以上步驟安裝很順利,但是最後修改nginx默認端口爲80後,發現我放在~/Documents目錄下的項目訪問都是403 Forbidden了。一陣折騰,幾次重裝之後,Google到了另一篇brew安裝nginx過程的介紹,出現了同樣的問題,最後是通過修改~/Documents目錄權限爲o+x才得以解決。之前我判斷權限導致該問題,只修改項目所在目錄權限爲777也是無法解決的。

轉帖:http://cssor.com/mac-nginx.html

發佈了28 篇原創文章 · 獲贊 12 · 訪問量 105萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章