Mac OS X 10.9.3上安装配置nginx-1.7.2

时间:2022-09-19 17:49:53

2014-06-27 wcdj


先从官方的wiki开始。http://wiki.nginx.org/GettingStarted,了解下配置nginx编译选项的各个含义。

./configure --help 查看具体编译选项使用说明,然后写一个简单的安装脚本。

Nginx modules must be selected during compile, run-time selection of modules is not currently supported.

A full summary of the compile-time options, including optional modules, can be found in the provided configure script by running./configure --help


#!/bin/bash

echo "begin to install nginx-1.7.2"

echo "configure..."
./configure --prefix=/Users/gerryyang/LAMP/nginx/install/nginx-1.7.2 \
                 --with-debug \
                 --with-pcre=/Users/gerryyang/LAMP/pcre/pcre-8.35 \
                 --with-openssl=/Users/gerryyang/LAMP/OpenSSL/openssl-1.0.1g

echo "make and make install..."
make && make install

指定后的配置和安装选项结果值如下:

Configuration summary
  + using PCRE library: /Users/gerryyang/LAMP/pcre/pcre-8.35
  + using OpenSSL library: /Users/gerryyang/LAMP/OpenSSL/openssl-1.0.1g
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/Users/gerryyang/LAMP/nginx/install/nginx-1.7.2"
  nginx binary file: "/Users/gerryyang/LAMP/nginx/install/nginx-1.7.2/sbin/nginx"
  nginx configuration prefix: "/Users/gerryyang/LAMP/nginx/install/nginx-1.7.2/conf"
  nginx configuration file: "/Users/gerryyang/LAMP/nginx/install/nginx-1.7.2/conf/nginx.conf"
  nginx pid file: "/Users/gerryyang/LAMP/nginx/install/nginx-1.7.2/logs/nginx.pid"
  nginx error log file: "/Users/gerryyang/LAMP/nginx/install/nginx-1.7.2/logs/error.log"
  nginx http access log file: "/Users/gerryyang/LAMP/nginx/install/nginx-1.7.2/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"

安装后的目录结构:
Mac OS X 10.9.3上安装配置nginx-1.7.2

查看安装好的nginx版本信息和编译选项:

root@mba:sbin#./nginx -v
nginx version: nginx/1.7.2
root@mba:sbin#./nginx -V
nginx version: nginx/1.7.2
built by clang 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
configure arguments: --prefix=/Users/gerryyang/LAMP/nginx/install/nginx-1.7.2 --with-debug --with-pcre=/Users/gerryyang/LAMP/pcre/pcre-8.35 --with-openssl=/Users/gerryyang/LAMP/OpenSSL/openssl-1.0.1g

然后运行nginx服务,并在browser上测试下。

Mac OS X 10.9.3上安装配置nginx-1.7.2

Mac OS X 10.9.3上安装配置nginx-1.7.2


测试后可以看到nginx记录的请求日志:

Mac OS X 10.9.3上安装配置nginx-1.7.2


以下是access.log的内容:

127.0.0.1 - - [27/Jun/2014:16:08:09 +0800] "GET /index HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0"
127.0.0.1 - - [27/Jun/2014:16:08:10 +0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0"
127.0.0.1 - - [27/Jun/2014:16:08:10 +0800] "GET /favicon.ico HTTP/1.1" 404 168 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0"
127.0.0.1 - - [27/Jun/2014:16:08:44 +0800] "GET /index.html HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0"


关于nginx的启动选项:

http://wiki.nginx.org/NginxCommandLine

Mac OS X 10.9.3上安装配置nginx-1.7.2


后续可选操作:
(1) 配置nginx的vim插件
cp -r /Users/gerryyang/LAMP/nginx/nginx-1.7.2/contrib/vim/* ~/.vim

修改后的效果:

Mac OS X 10.9.3上安装配置nginx-1.7.2


下面是安装后默认的nginx运行配置:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


nginx的启动和关闭,一些信号选项可参考http://wiki.nginx.org/NginxCommandLine

root@mba:sbin#./nginx 
root@mba:sbin#ps aux|grep nginx
root              703   0.0  0.0  2432784    620 s000  S+    9:54下午   0:00.00 grep nginx
nobody            701   0.0  0.0  2452336    892   ??  S     9:54下午   0:00.00 nginx: worker process
root              700   0.0  0.0  2442908    344   ??  Ss    9:54下午   0:00.00 nginx: master process ./nginx
root@mba:sbin#./nginx -s stop
root@mba:sbin#ps aux|grep nginx
root              706   0.0  0.0  2442000    636 s000  S+    9:54下午   0:00.00 grep nginx
root@mba:sbin#./nginx 
root@mba:sbin#ps aux|grep nginx
root              714   0.7  0.0  2442000    632 s000  S+    9:56下午   0:00.00 grep nginx
nobody            712   0.0  0.0  2452336    896   ??  S     9:56下午   0:00.00 nginx: worker process
root              711   0.0  0.0  2443932    348   ??  Ss    9:56下午   0:00.00 nginx: master process ./nginx
root@mba:sbin#./nginx -s stop
root@mba:sbin#ps aux|grep nginx
root              717   0.0  0.0  2432784    612 s000  S+    9:56下午   0:00.00 grep nginx
root@mba:sbin#./nginx 
root@mba:sbin#ps aux|grep nginx
root              722   0.0  0.0  2432784    612 s000  S+    9:56下午   0:00.00 grep nginx
nobody            720   0.0  0.0  2444144    908   ??  S     9:56下午   0:00.00 nginx: worker process
root              719   0.0  0.0  2443932    348   ??  Ss    9:56下午   0:00.00 nginx: master process ./nginx
root@mba:sbin#kill -QUIT $(cat ../logs/nginx.pid)
root@mba:sbin#ps aux|grep nginx
root              725   0.0  0.0  2432784    612 s000  S+    9:57下午   0:00.00 grep nginx
root@mba:sbin#

The master process can handle the following signals:

TERM, INT Quick shutdown
QUIT Graceful shutdown
KILL Halts a stubborn process
HUP Configuration reload
Start the new worker processes with a new configuration
Gracefully shutdown the old worker processes
USR1 Reopen the log files
USR2 Upgrade Executable on the fly
WINCH Gracefully shutdown the worker processes


There's no need to control the worker processes yourself. However, they support some signals, too:

TERM, INT Quick shutdown
QUIT Graceful shutdown
USR1 Reopen the log files




参考

[1] https://github.com/nginx/nginx
[2] http://nginx.org/download/
[3] http://lenky.info/ngx_book
[4] http://book.51cto.com/art/201305/395368.htm
[5] https://github.com/taobao/nginx-book