nginx编译安装

  Nginx 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版。目前 Nginx 的1.0.0稳定版已发布,开发版本为0.9.x,稳定版为 0.8.x,历史稳定版为 0.7.x,建议使用 0.8系列作为生产版本。 2011年6月1日,nginx 1.0.4发布。

  优点:

  在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

  Nginx作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务器对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。

  作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last. fm 描述了成功并且美妙的使用经验。

  Nginx 是一个安装非常的简单,配置文件非常简洁(还能够支持perl语法),Bugs非常少的服务器:Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。

  一、编译安装nginx(官方文档http://wiki.nginx.org/Main)

    1.模块依赖性:

    gzip 模块需要 zlib 库

  rewrite 模块需要 pcre 库

  ssl 功能需要 openssl 库

    #yum install gcc openssl-devel pcre-devel zlib-devel(gcc编译环境)

    2.nginx编译

      先添加nginx用户和用户组

      # groupadd nginx

      # useradd -g nginx -s /bin/false -M nginx

      解压源码包,这里采用最新的源码包1.0.4

      #tar -zxf nginx-1.0.4.tar.gz

      #cd nginx-1.0.4

      #./configure \

  --prefix=/usr \

  --sbin-path=/usr/sbin/nginx \

  --conf-path=/etc/nginx/nginx.conf \

  --error-log-path=/var/log/nginx/error.log \

  --http-log-path=/var/log/nginx/access.log \

  --pid-path=/var/run/nginx/nginx.pid  \

  --lock-path=/var/lock/nginx.lock \

  --user=nginx \

  --group=nginx \

  --with-http_ssl_module \

  --with-http_flv_module \

  --with-http_stub_status_module \

  --with-http_gzip_static_module \

  --http-client-body-temp-path=/var/tmp/nginx/client/ \

  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ 

      #make && make install

      #makedir -p /var/log/nginx(编译时不会创建日志目录,需手工创建)

      创建启动脚本/etc/init.d/nginx(也可以从官方文档下载

http://wiki.nginx.org/RedHatNginxInitScript)

       亦可自己创建

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ 
 
]*\).*/\1/g' -`
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|
 
reload|force-reload|configtest}"
        exit 2
esac
    #chkconfig --add nginx
    #service ngnix start
    #chkconfig nginx on


   可以访问一下页面测试一下呵呵!

   http://yourIP

二、Nginx虚拟主机配置
      1.基于IP的虚拟主机
        在eth0上添加两块网卡别名,分别为192.168.0.11和192.168.0.12
        #ifconfig eth0:1 192.168.0.11/24
        #ifconfig eth0:2 192.168.0.12/24
        这是检测一下,应该在eth0上有两个别名(ip addr | grep eth0)
        配置nginx主配置文件

 http {
  server {
    listen          192.168.0.11:80;
    server_name     192.168.0.11;
    access_log      logs/server1.access.log main;
 location / {
    index index.html;
    root  /var/www/server1;
   } 
 }
 
  server {
    listen          192.168.0.12:80;
    server_name     192.168.0.12;
    access_log      logs/server2.access.log main;
 location / {
    index index.html;
    root  /var/www/server2;
      }
  }
}


    接下来建立网页文件,在/var/www/server1和/var/www/server2分别编辑一些网页文

件,尽量不同。
    平滑启动nginx服务 
    #service nginx reload
   测试一下
   2.配置基于域名的虚拟主机

 2.配置基于域名的虚拟主机
      基于域名的虚拟主机是最常见的一种虚拟主机。只需配置你的DNS服务器,

经每个主机名映射到正确的IP地址,然后配置Nginx服务器,令其识别不同的主机

名就可以了。这种虚拟主机技术,使很多虚拟主机可以共享同一个IP地址,有效

解决了IP地址不足的问题。所以,如果没有特殊要求是你必须用一个基于IP的虚

拟主机,最好使用基于域名的虚拟主机。
      接下来配置基于域名的虚拟主机。在以下的实例中,配置三个虚拟主机,

第一个虚拟主机对应的域名是aaa.domain.com,第二个虚拟主机对应的虚拟主机

的域名是bbb.other.com,第三个虚拟主机对应域名www.domain.com、domain.com,

以及除了aaa.domain.com之外的所有*.domain.com二级域名的访问都有它来处理

。每个虚拟主机的网页文件分别存放在了不同的目录中,每个虚拟主机使用了不

同的的日志文件来记录访问日志。
代码:
  http{
          #第一个虚拟主机
           server
         {
                    listen 80;
                    server_name aaa.domain.com;
                    access_log       logs/aaa.domain.com.access.log   

combined;
                    locaton /
                        {
                         index index.html index.htm;
                         root  /data0/htdocs/aaa.domain.com;
                       }
           }
          #第二个虚拟主机
           server
         {
                    listen 80;
                    server_name bbb.domain.com;
                    access_log       logs/bbb.domain.com.access.log   

combined;
                    locaton /
                        {
                         index index.html index.htm;
                         root  /data0/htdocs/bbb.domain.com;
                       }
           }
           #第三个虚拟主机
           server
         {
                    listen 80;
                    server_name www.domain.com domain.com *.domain.com;
                    access_log       logs/www.domain.com.access.log   

combined;
                    locaton /
                        {
                         index index.html index.htm;
                         root  /data0/htdocs/www.domain.com;
                       }
           }  
   三 、什么是CGI

  CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你

的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上

  CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量

。如php,perl,tcl等

  什么是FastCGI

  FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后

,不会每次都要花费时间去fork一次(这是CGI最为人诟病的fork-and-execute 模

式)。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上

执行并且接受来自其它网站服务器来的请求。

  FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释

器进程保持在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是

CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器

调度,则可以提供良好的性能、伸缩性、Fail- Over特性等等。

  FastCGI与CGI特点

  1、如CGI,FastCGI也具有语言无关性.

  2、如CGI, FastCGI在进程中的应用程序,独立于核心web服务器运行,提供了

一个比API更安全的环境。(APIs把应用程序的代码与核心的web服务器链接在一起

,这意味着在一个错误的API的应用程序可能会损坏其他应用程序或核心服务器;

恶意的API的应用程序代码甚至可以窃取另一个应用程序或核心服务器的密钥。)

  3、FastCGI技术目前支持语言有:C/C++、Java、Perl、Tcl、Python、

SmallTalk、Ruby等。相关模块在Apache, ISS, Lighttpd等流行的服务器上也是

可用的。

  4、如CGI,FastCGI的不依赖于任何Web服务器的内部架构,因此即使服务器

技术的变化, FastCGI依然稳定不变。

  FastCGI的工作原理

1、Web Server启动时载入FastCGI进程管理器(IIS ISAPI或Apache Module)

2、FastCGI进程管理器自身初始化,启动多个CGI解释器进程(可见多个php-

cgi)并等待来自Web Server的连接。

3、当客户端请求到达Web Server时,FastCGI进程管理器选择并连接到一个

CGI解释器。Web server将CGI环境变量和标准输入发送到FastCGI子进程php-cgi



4、FastCGI子进程完成处理后将标准输出和错误信息从同一连接返回Web

Server。当FastCGI子进程关闭连接时,请求便告处理完成。FastCGI子进程接着

等待并处理来自FastCGI进程管理器(运行在Web Server中)的下一个连接。 在CGI

模式中,php-cgi在此便退出了。

在上述情况中,你可以想象CGI通常有多慢。每一个Web请求PHP都必须重新解

析php.ini、重新载入全部扩展并重初始化全部数据结构。使用FastCGI,所有这

些都只在进程启动时发生一次。一个额外的好处是,持续数据库连接(Persistent

database connection)可以工作。

  FastCGI的不足

  因为是多进程,所以比CGI多线程消耗更多的服务器内存,PHP-CGI解释器每

进程消耗7至25兆内存,将这个数字乘以50或100就是很大的内存数。

Nginx 0.8.46+PHP 5.2.14(FastCGI)服务器在3万并发连接下,开启的10个

Nginx进程消耗150M内存(15M*10=150M),开启的64个php-cgi进程消耗1280M内

存(20M*64=1280M),加上系统自身消耗的内存,总共消耗不到2GB内存。如果服

务器内存较小,完全可以只开启25个php-cgi进程,这样php-cgi消耗的总内存数

才500M。

补充:编译详细参数如下:

#Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。

--prefix=PATH      

#Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为PATH/sbin/nginx。

--sbin-path=PATH

#在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为

PATH/conf/nginx.conf。

--conf-path=PATH

#在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 PATH/logs/nginx.pid。

--pid-path=PATH

#nginx.lock文件的路径。

--lock-path=PATH

#在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 PATH/logs/error.log。

--error-log-path=PATH

#在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 PATH/logs/access.log。

--http-log-path=PATH

#在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。

--user=USER

#在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默

认为 nobody。

--group=GROUP

#指定编译的目录

--builddir=DIR

#启用 rtsig 模块

--with-rtsig_module

#允许或不允许开启SELECT模式,如果configure没有找到合适的模式,比如,kqueue(sun os)、epoll(linux kenel 2.6+)、rtsig(实时信号)

--with-select_module(--without-select_module) 

#允许或不允许开启POLL模式,如果没有合适的,则开启该模式。

--with-poll_module(--without-poll_module)

#开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在

DEBIAN上是libssl-dev

--with-http_ssl_module

--with-http_realip_module #启用 ngx_http_realip_module

--with-http_addition_module #启用 ngx_http_addition_module

--with-http_sub_module #启用 ngx_http_sub_module

--with-http_dav_module #启用 ngx_http_dav_module

--with-http_flv_module #启用 ngx_http_flv_module

--with-http_stub_status_module #启用 "server status" 页

--without-http_charset_module #禁用 ngx_http_charset_module

--without-http_gzip_module #禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。

--without-http_ssi_module #禁用 ngx_http_ssi_module

--without-http_userid_module #禁用 ngx_http_userid_module

--without-http_access_module #禁用 ngx_http_access_module

--without-http_auth_basic_module #禁用 ngx_http_auth_basic_module

--without-http_autoindex_module #禁用 ngx_http_autoindex_module

--without-http_geo_module #禁用 ngx_http_geo_module

--without-http_map_module #禁用 ngx_http_map_module

--without-http_referer_module #禁用 ngx_http_referer_module

--without-http_rewrite_module #禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。

--without-http_proxy_module #禁用 ngx_http_proxy_module

--without-http_fastcgi_module #禁用 ngx_http_fastcgi_module

--without-http_memcached_module #禁用 ngx_http_memcached_module

--without-http_limit_zone_module #禁用 ngx_http_limit_zone_module

--without-http_empty_gif_module #禁用 ngx_http_empty_gif_module

--without-http_browser_module #禁用 ngx_http_browser_module

--without-http_upstream_ip_hash_module #禁用ngx_http_upstream_ip_hash_module

--with-http_perl_module - #启用 ngx_http_perl_module

--with-perl_modules_path=PATH #指定 perl 模块的路径

--with-perl=PATH #指定 perl 执行文件的路径

--http-log-path=PATH #Set path to the http access log

--http-client-body-temp-path=PATH #Set path to the http client request body 

temporary files

--http-proxy-temp-path=PATH #Set path to the http proxy temporary files

--http-fastcgi-temp-path=PATH #Set path to the http fastcgi temporary files

--without-http #禁用 HTTP server

--with-mail #启用 IMAP4/POP3/SMTP 代理模块

--with-mail_ssl_module #启用 ngx_mail_ssl_module

--with-cc=PATH #指定 C 编译器的路径

--with-cpp=PATH #指定 C 预处理器的路径

--with-cc-opt=OPTIONS #

--with-ld-opt=OPTIONS #Additional parameters passed to the linker. With the use 

of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".

--with-cpu-opt=CPU #为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, 

pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

--without-pcre #禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 

"location" 配置指令中的正则表达式也需要 PCRE 。

--with-pcre=DIR #指定 PCRE 库的源代码的路径。

--with-pcre-opt=OPTIONS #设置PCRE的额外编译选项。

--with-md5=DIR #使用MD5汇编源码。

--with-md5-opt=OPTIONS #Set additional options for md5 building.

--with-md5-asm #Use md5 assembler sources.

--with-sha1=DIR #Set path to sha1 library sources.

--with-sha1-opt=OPTIONS #Set additional options for sha1 building.

--with-sha1-asm #Use sha1 assembler sources.

--with-zlib=DIR #Set path to zlib library sources.

--with-zlib-opt=OPTIONS #Set additional options for zlib building.

--with-zlib-asm=CPU #Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro

--with-openssl=DIR #Set path to OpenSSL library sources

--with-openssl-opt=OPTIONS #Set additional options for OpenSSL building

--with-debug #启用调试日志

--add-module=PATH #Add in a third-party module found in directory PATH

在不同版本间,选项可能会有些许变化,请总是使用 ./configure --help 命令来检查一下当前的选项列表。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值