美文网首页程序员
Nginx介绍及安装

Nginx介绍及安装

作者: ifcoder | 来源:发表于2017-07-11 19:48 被阅读48次

    什么是Nginx?

    Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器。

    Nginx特点

    1. 占有内存少
    2. 并发能力强

    Nginx相对于apache的优缺点

    1.Nginx相对于apache的优点
    • 轻量级,同样起web服务,比apache占用更少的内存及资源
    • 抗并发,nginx处理请求是异步非阻塞的,而apache则是阻塞型的,在高并发下nginx能保持低资源低消耗高性能
    • 高度模块化的设计,编写模块相对简单
    • 社区活跃,各种高性能模块出品迅速啊
    2.apache相对于Nginx的优点
    • rewrite ,比nginx 的rewrite 强大
    • 模块超多,基本想到的都可以找到
    • 少bug ,nginx 的bug 相对较多
    3.Nginx 配置简洁, Apache 复杂
    4.最核心的区别在于apache是同步多进程模型,一个连接对应一个进程;nginx是异步的,多个连接(万级别)可以对应一个进程

    Tengine 是nginx的加强版,封装版,淘宝开源

    Tengine安装流程

    安装之前准备

    1、依赖 gcc openssl-devel pcre-devel zlib-deve

    安装:yum install gcc openssl-devel pcre-devel zlib-devel

    2.创建用户和用户组。为了方便nginx运行而不影响linux安全

    创建组:groupadd -r nginx

    创建用户:useradd -r -g nginx -M nginx

    -M 表示不创建用户的家目录。

    3.安装

    Tengine压缩包自行下载,解压后在Tengine目录执行后续步骤

    以下代码为实际安装时所用代码

        ./configure \
      --prefix=/opt/to/soft/tengine-2.1.0/ \
      --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 \
      --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/ \
      --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
      --http-scgi-temp-path=/var/tmp/nginx/scgi \
      --with-pcre 
    

    以下代码为示意代码

      ./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 \        //http请求日志路径
    --pid-path=/var/run/nginx/nginx.pid \              //pid进程号路径
    --lock-path=/var/lock/nginx.lock \                 //锁文件路径
    --user=nginx \                                     //若加上 --user和--group 则只有指定用户可以启动,若不加则没有限制
    --group=nginx \
    --with-http_ssl_module \                           //可插拔模块 |
    --with-http_flv_module \                           //用哪个模块 |
    --with-http_stub_status_module \                   //就with    |
    --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/ \        //文件
    --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \          //路
    --http-scgi-temp-path=/var/tmp/nginx/scgi \            //径
    --with-pcre
    

    4、make && make install

    5、添加安装的tengine到注册表,具体内容见nginx

    • 注意修改路径,nginx文件必须是在/etc/init.d 下面touch或者vi来新建,不可以用rz sz或者其他文件上传工具进行上传,因为会出现编码问题
    • 修改nginx文件的执行权限
      • chomod a+x nginx
    • 添加该文件到系统服务中去
      • chkconfig --add nginx
    • 查看是否添加成功
      • chkconfig --list nginx
    • 启动,停止,重新装载
      • service nginx start|stop|reload

    以下代码为nginx

    #!/bin/bash
    #
    # chkconfig: - 85 15
    # description: nginx is a World Wide Web server. It is used to serve
    # 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="/opt/to/soft/tengine-2.1.0/sbin/nginx"
    prog=$(basename $nginx)
     
    NGINX_CONF_FILE="/opt/to/soft/tengine-2.1.0/conf/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: "
    #  -HUP是nginx平滑重启参数  
        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
    

    相关文章

      网友评论

        本文标题:Nginx介绍及安装

        本文链接:https://www.haomeiwen.com/subject/tjnmhxtx.html