在Mac OS X 10.9上编译和安装nginx的教程

2014-04-09 09:50:02来源:开源中国作者:

很早就听闻nginx的大名,轻量级高并发的特性一直吸引着我,但一直没有时间顾及,最近由于各种原因,准备学习一下nginx这个神器。好的,让我们进入正题。今天先来安装一下。

很早就听闻nginx的大名,轻量级高并发的特性一直吸引着我,但一直没有时间顾及,最近由于各种原因,准备学习一下nginx这个神器。好的,让我们进入正题。今天先来安装一下。

首先我用的是苹果最新的 Mac OS X Mavericks 10.9的系统,所以准备采用源码编译的方法来安装。

1.先安装PCRE库

可以在这里下载最新版,我这里使用的是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

tar -zvxf nginx-1.5.2.tar.gz cd nginx-1.5.2 ./configure

默认编译概要:

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

编译参数参考 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

    Labelnginx Program/usr/local/sbin/nginx KeepAlive  NetworkState  StandardErrorPath/var/log/system.log LaunchOnlyOnce    

载入自启动文件

launchctl load -F /System/Library/LaunchDaemons/nginx.plist 
关键词:Macnginx

赞助商链接: