nginx服务器部署

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

 部署环境:

                 环境系统:red hat 5

                 内核版本:2.6.18-371.el5

                nginx版本:nginx-1.0.15

                nginx升级版本:nginx-1.2.9

部署过程:

          环境准备1:安装gcc  pcre等依赖软件包

             #yum -y install gcc gcc-c++  openssl-devel pcre-devel zlib-devel

             #yum -y install  “开发工具”“开发库”

环境准备好后开始安装:

             #tar zxf nginx-1.0.15.tar.gz                     //解压软件包

             #cd nginx-1.0.15                            

             #ls                                                            //查看解压目录文件

             auto     CHANGES.ru  configure  html     Makefile  objs    src
             CHANGES  conf        contrib    LICENSE  man       README

             #useradd nginx                                       //创建nginx后台进程管理用户

             #./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module

                                                                              //编译

  1.  --user=nginx –group=nginx 设置允许nginx允许的用户和组为nginx 

  2. // --prefix=/usr/local/nginx/  设置nginx安装路径 

  3. // --with-http_stub_status_module 安装允许状态模块 

  4. // --with-http_ssl_module 安装ssl模块

             #make

             #make install

             # /usr/local/nginx/sbin/nginx  –V       //安装完成后查看Nginx的相关环境配置信息是否正确 

             nginx version: nginx/1.0.15

             #/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

                                                                              //启动nginx服务

            #netstat -anput|grep nginx                      //查看服务端口80

            tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      14815/nginx

            #ls /usr/local/nginx/                                //查看nginx安装目录文件

            client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
            conf              html          proxy_temp  scgi_temp

            conf:为配置文件目录 nginx.conf 主配置文件

            sbin:为启动脚本目录

            html:为默认主页面目录

            logs:为日志文件目录包括access.log访问日志  error.log 错误日志 nginx.pid pid文件 

            停止nginx服务:

            #ps -ef |grep nginx                                   //查看进程id,红色表示进程id

            root     14815     1  0 15:40 ?        00:00:00 nginx: master  process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
            nginx    14817 14815  0 15:40 ?        00:00:00 nginx: worker process                                         
           root     16139  4816  0 16:45 pts/1    00:00:00 grep nginx                                                 

            #kill -9  14815
            #kill -9  14817                                           //杀掉进程,停止服务

            验证服务是否启动

             浏览器输入:http://10.10.0.221                //回车

             页面看到        Welcome to nginx!             //服务启动成功

            为nginx服务写启动脚本/重启脚本:要先停止nginx服务,其中nginx的路径需要按照具体的安装路径修改

            #vi /etc/init.d/nginx

#!/bin/sh
# nginx - this script starts and stops the nginx daemin
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
    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

           #cd /etc/init.d/                                         //切换到init.d目录

           #chmod +x /etc/init.d/nginx                   //赋予执行权限

           # chkconfig --add nginx                          //将脚本文件加入chkconfig中

           这下就能使用server命令启动停止服务

           #server nginx start |stop |restart 

           设置nginx开机启动:

           # echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local

           #cat /etc/rc.local | grep nginx

           /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

           

           ok,部署完成

接下来进行平滑升级:

           #tar zxf nginx-1.2.9.tar.gz                       //解压

           #cd nginx-1.2.9                                       //进入目录

           #./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module

                                                                           //编译,要和未升级前一样

           #make

           #mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

                                                                          //改名就启动脚本

           #cd /root/Desktop/nginx-1.2.9/objs/

           #cp nginx /usr/local/nginx/sbin/             //拷贝高版本启动脚本

           #make upgrade                                     //升级软件

           # ./nginx -v                                            //查看版本

           nginx version: nginx/1.2.9                      //升级成功

           # netstat -anput |grep 80                      //查看是否影响nginx运行,服务在运行中
           tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      14815/nginx

           到此为止,nginx安装完成,来看一下主配置文件的内容:根据编译的不同主配置可能也会有所不同,我的只是nginx刚搭建起来时的配置文件,还是很干净的配置文件。

          


#user  nobody;                                                       #运行用户,如果是注释的表示用户是我们自己创建的用户,不是默认的
worker_processes  1;                                               #启动进程,通常设置成和cpu的数量相等

#error_log  logs/error.log;                                       #全局错误日志
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;                                         #PID文件


events {
    worker_connections  1024;                                  #单个后台worker process进程的最大并发链接数
}


http {                                                                      #设定http服务器,利用它的反向代理功能提供负载均衡支持
    include       mime.types;                                      #设定mime类型,类型由mime.type文件定义
    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;                                                               #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.


    #tcp_nopush     on;                                                          #nginx在一个数据包里发送所有头文件,而不一个接一个的发送
    #tcp_nodelay                                                                   #nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值

    #keepalive_timeout  0;
    keepalive_timeout  65;                                                     #连接超时时间

    #gzip  on;                                                                        #开启gzip压缩

    server {                                                                            #定义虚拟主机
        listen       80;                                                               #侦听80端口
        server_name  localhost;                                               #定义访问路径

        #charset koi8-r;           

        #access_log  logs/host.access.log  main;                      #设定本虚拟主机的访问日志
       
        #默认请求
        location / {                                                                  #location允许对不同的URL使用不同的配置,即可以使用字符串也可以使用与正则表达式,实现整合本地路径和URL路径的访问控制          
            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;                      #如果出现指定的http错误状态码,则返回给客户端指定的url地址
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {           #模式匹配URI,在此路径下的文件都生效
        #    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;
    #    server_name  localhost;

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

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

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

}

好了,先到这吧,剩下的配置以后再补,还得要继续学习呢