下载nginx
http://nginx.org/en/download.html

nginx编译记录
解压源码之后,在源码目录执行./configure --help查看编译选项,这里指定--prefix=/root/nginx,将安装路径指定为root目录nginx文件夹。

执行./configure --prefix=/root,遇到以下错误提示:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
意思很明显,缺少PCRE库,导致HTTP rewrite模块无法编译,要么使用--without-http_rewrite_module禁用HTTP rewrite模块,要么安装PCRE库或是手动指定PCRE库的路径。这里参考网上教程,把最基本的库一次性安装上,命令行执行以下命令:

sudo apt-get install libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev
解决configure错误后直接make && make install就可以了,nginx执行程序及其配置文件位于/root/nginx路径下。
./configure --prefix=/home/ubuntu/nginx --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module

以下是nginx执行configure之后的输出:

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library


  nginx path prefix: "~/nginx"
  nginx binary file: "~/nginx/sbin/nginx"
  nginx modules path: "~/nginx/modules"
  nginx configuration prefix: "~/nginx/conf"
  nginx configuration file: "~/nginx/conf/nginx.conf"
  nginx pid file: "~/nginx/logs/nginx.pid"
  nginx error log file: "~/nginx/logs/error.log"
  nginx http access log file: "~/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"
~/nginx# tree
.
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── html
│   ├── 50x.html
│   └── index.html
├── logs
└── sbin
    └── nginx
接下来使用nginx搭建一个简单的静态Web服务器。修改/root/nginx/conf/nginx.conf文件,如下:

    user root; # 启用root权限,避免Permission denied导致403 Forbidden问题
    ...
    # 打开gzip压缩
    gzip  on;

    server {
        listen       8080;        # 监听端口改成8080
        server_name  localhost;
        ...
        location / {
            root /root/www;       # 根路径改成/root/www
            index  index.html index.htm;
        }
执行/root/nginx/sbin/nginx即可启动nginx,通过ps -ef命令可查看后台的nginx master进程和worker进程,如果重新修改了nginx.conf文件,则需要执行/root/nginx/sbin/nginx -s reload重新载入配置。