我是用root用户装的
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
tar xvzf pcre-8.33.tar.gz
cd pcre-8.12
sudo ./configure --prefix=/usr/local
sudo make
sudo make install
或者更简单的方式:
yum install gcc-c++
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
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
cd nginx-1.5.2
./configure --prefix=/usr/local/nginx
注: nginx默认是安装到/opt目录下,在mac系统中是没有/opt目录的,所以用--prefix指定目标目录
默认编译概要:
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"
+ 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
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
--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
查看是否启动成功 :在浏览器中输入http://localhost 回车
需要停止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>Program</key><string>/usr/local/sbin/nginx</string>
<key>KeepAlive</key><true/>
<key>NetworkState</key><true/>
<key>StandardErrorPath</key><string>/var/log/system.log</string>
<key>LaunchOnlyOnce</key><true/>
</dict>
</plist>
<!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>Program</key><string>/usr/local/sbin/nginx</string>
<key>KeepAlive</key><true/>
<key>NetworkState</key><true/>
<key>StandardErrorPath</key><string>/var/log/system.log</string>
<key>LaunchOnlyOnce</key><true/>
</dict>
</plist>
载入自启动文件
launchctl load -F /System/Library/LaunchDaemons/nginx.plist