为Mac配置服务器nginx

1.先安装PCRE库(转注:nginx rewrite依赖该库)

可以在这里下载最新版,我这里使用的是8.33的版本然后在终端执行下面的命令。

cd ~/Download
tar xvzf pcre-8.33.tar.gz
cd pcre-8.12
sudo ./configure --prefix=/usr/local
sudo make
sudo make install

 

2.下载安装nginx

首先在nginx官网下载最新的源码,我这里用的是nginx-1.5.2

--with-cc-opt="-Wno-deprecated-declarations"解决编译MD5出错
tar -zvxf nginx-1.5.2.tar.gz
cd nginx-1.5.2
./configure  --with-cc-opt="-Wno-deprecated-declarations"
make make install

 默认编译概要:

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 --prefix=/usr/local --sbin-path=/usr/local/sbin --conf-path=/etc/nginx --pid-path=/var/run --error-log-path=/var/log/nginx --http-log-path=/var/log/nginx

 

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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。