我们的项目基于SSI技术实现前后端完全分离,同事都用Apache,我比较喜欢nginx。
安装方法源自:http://www.codingcool.com/2013/07/18/在mac-os-x-10-9上编译安装nginx/
接着往下转:
1.先安装PCRE库(转注:nginx rewrite依赖该库)
可以在这里下载最新版,我这里使用的是8.33的版本然后在终端执行下面的命令。
- cd ~/Download
- tar xvzf pcre-8.33.tar.gz
- cd pcre-8.33
- sudo ./configure --prefix=/usr/local
- sudo make
- sudo make install
2.下载安装nginx
首先在nginx官网下载最新的源码,我这里用的是nginx-1.5.2
- tar -zvxf nginx-1.5.2.tar.gz
- cd nginx-1.5.2
- ./configure --prefix=/usr/local/nginx --with-cc-opt="-Wno-deprecated-declarations"
- make
- make install
2015年03月19日注:在最新版Mac系统 10.10+中,nginx编译过程需要用到的MD5相关API已经是deprecated的了,会报错,类似于
- cc1: warnings being treated as errors
- src/core/ngx_crypt.c: In function ‘ngx_crypt_apr1’:
- src/core/ngx_crypt.c:76: warning: ‘MD5_Init’ is deprecated (declared
- at /usr/include/openssl/md5.h:113)
- src/core/ngx_crypt.c:77: warning: ‘MD5_Update’ is deprecated
- (declared at /usr/include/openssl/md5.h:114)
- ....
解决方法就是在配置时候增加参数如下:
- ./configure --with-cc-opt="-Wno-deprecated-declarations"
默认编译概要:
- Configuration summary
- + using system PCRE library
- + OpenSSL library is not used
- + md5: using system crypto library
- + sha1: using system crypto library
- + using system zlib library
- # 默认编译参数对应的安装路径(*_temp 为目录)
- nginx path prefix: "/usr/local/nginx"
- nginx binary file: "/usr/local/nginx/sbin/nginx"
- nginx configuration prefix: "/usr/local/nginx/conf"
- nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
- nginx pid file: "/usr/local/nginx/logs/nginx.pid"
- nginx error log file: "/usr/local/nginx/logs/error.log"
- nginx http access log file: "/usr/local/nginx/logs/access.log"
- nginx http client request body temporary files: "client_body_temp"
- nginx http proxy temporary files: "proxy_temp"
- nginx http fastcgi temporary files: "fastcgi_temp"
- nginx http uwsgi temporary files: "uwsgi_temp"
- nginx http scgi temporary files: "scgi_temp"
为了方便:(转注:我选择这个方式)
- sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
- sudo ln -s /usr/local/nginx/conf /etc/nginx
- sudo ln -s /usr/local/nginx/logs/nginx.pid /var/run/nginx.pid
- sudo ln -s /usr/local/nginx/logs /var/log/nginx
或者直接在编译时设定
- ./configure \
- --sbin-path=/usr/local/bin/nginx \
- --conf-path=/etc/nginx \
- --pid-path=/var/run \
- --error-log-path=/var/log/nginx \
- --http-log-path=/var/log/nginx
编译参数参考 Nginx InstallOption
3.启动Nginx
检查PATH环境变量
- # ~/.bash_profile export PATH=/usr/local/bin:/usr/local/sbin:$PATH
启动Nginx
- sudo nginx
需要停止Nginx的时候运行
- sudo nginx -s stop
4.配置自启动
创建文件 /System/Library/LaunchDaemons/nginx.plist
- <?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>nginx</string>
- <key>KeepAlive</key>
- <true/>
- <key>Program</key>
- <string>/usr/local/bin/nginx</string>
- <key>RunAtLoad</key>
- <true/>
- </dict>
- </plist>
载入自启动文件
- 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也是无法解决的。