为Mac配置服务器nginx

来源:互联网 发布:photoshop mac版下载 编辑:程序博客网 时间:2024/05/23 16:32

我们的项目基于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

0 0
原创粉丝点击