Wednesday, October 27

在MacOSX Server上编译,安装使用NGNIX

MacOSX自带Apache服务器,如果是MacOSX Server的话,所带的Server Admin图形界面的管理器很好用,可是他的Proxy管理功能比较弱,必须修改直接去修改conf文件,如果有virtual host的需求不好对应。所以经多简单的调查,决定使用ngnix。ngnix的好处网上有很多介绍,下面主要考虑如何安装到macosx上,找到一篇很好的介绍文章Compiling Nginx on OS X Leopard in 5 minutes
按照这篇文章所诉的步骤,可以很简单地编译安装最新版的ngnix
  • 首先编译PCRE 到官方网站上下载最新版的代码,现在是8.10,解压缩以后,通过terminal进入代码的目录编译

$ cd pcre-8.10
$ ./configure --prefix=/usr/local
$ make
$ sudo make install

  • 下载最新的nginx源代码,下载是nginx-0.8.53,解压缩以后,通过terminal计入代码目录,编译安装
$ cd nginx-0.8.53
$ ./configure --prefix=/usr/local --with-http_ssl_module
$ make
$ sudo make install

  • 设置PATH。一般缺省的mac安装里面nginx所在的目录/usr/local/sbin不会被包含在PATH里面的,一般which ngnix也是没有结果的,可以这样先export PATH就可以找到了。如果希望机器一启动PATH就设置好了,可以将下面这行加入~/.profile文件里。
$ export PATH=$PATH:/usr/local/sbin
  • 尝试启动ngnix,使用safari访问一下http://localhost, 看看是否可以看到
Welcome to nginx!

  • 启动和关闭的命令
$ which nginx
/usr/local/sbin/nginx
$ sudo nginx
$ sudo nginx -h
nginx version: nginx/0.8.53
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file

$ sudo nginx -s stop
  • 设置conf文件。nginx的配置文件位于/usr/local/conf/nginx.conf, 用编辑器打开,编辑之。我需要一个静态文件服务,一个redirect和一个proxy pass,那就类似下面的写法
server {
listen 80;
server_name cn.bbshare.com;
access_log logs/cn.bbshare.com.access.log;
index index.html;
root /Users/myname/CNWebserver;
}
server {
listen 80;
server_name ja.bbshare.com;
access_log logs/ja.bbshare.com.access.log;
rewrite ^(.*) http://www.yahoo.com:80$1 permanent;
}
server {
listen 80;
server_name redmine.bbshare.com;
access_log logs/redmine.bbshare.com.access.log;
location / {
proxy_pass http://127.0.0.1:8080/;
}
}