Centos7 安装 Tengine

Tengine介绍

Tengine是淘宝开发的基于nginx的应用服务器,反向代理服务器。它兼容nginx的所有配置,并且在其上增加了很多实用的功能,例如动态模块加载,集成lua语言进行扩展等,在性能方面有了比较大的改进。下面是Tengine独有的部分功能,查看全部等前往官网,地址是 http://tengine.taobao.org/ 。

一、Tengine部分特性

1.nginx -m 查看所有已经加载的模块,static说明已经安装的,shared说明是动态安装的;

2.nginx架构是模块化的,除了主模块,其他模块都需在安装时进行编译安装,也可在安装后进行编译安装;

3.tengine完全兼容nginx的配置;

4.tengine实现动态加载模块,不需重新编译,就可以实现安装新的模块。动态模块功能会被默认安装。而nginx不支持模块动态加载,需要编译重启;

5.lua语言扩展功能,通过编程可以有效为应用服务器分流请求;

6.强大的负载均衡功能,可以检测应用服务器的健康情况,动态的上下线。

二、安装Tengine,具有LUA模块

               cd /usr/local/tengine/sbin

                ./nginx -t -c /usr/local/nginx/conf/nginx.conf   测试nginx配置文件是否正确

               ./nginx  启动 (./nginx -c /usr/local/nginx/conf/nginx.conf 指定配置文件启动)

               ./nginx -s reload   就是来重启nginx

               ./nginx -s stop   快速停止nginx

               ./nginx -s quit  完整有序的停止nginx

  1. 安装必要的编译环境好

    
    # yum update
    
    # yum install gcc gcc-c++ autoconf automak
    
  2. 安装需要的组件

    1. PCRE

      PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx rewrite依赖于PCRE库,所以在安装Tengine前一定要先安装PCRE,最新版本的PCRE可在官网(http://www.pcre.org/)获取。具体安装流程为:

      cd /usr/src
      wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz
      tar zxvf pcre-8.41.tar.gz
      cd pcre-8.41
      ./configure --prefix=/usr/local/pcre
      make && make install

      附加信息:

      源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install)。 
      Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./configure –help输出详细的选项列表。其中–prefix选项是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share,比较凌乱。 
      如果配置–prefix,如:./configure –prefix=/usr/local/test,可以把所有资源文件放在/usr/local/test的路径中,不会杂乱。 
      用了—prefix选项的另一个好处是卸载软件或移植软件。当某个安装的软件不再需要时,只须简单的删除该安装目录,就可以把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统)。当然要卸载程序,也可以在原来的make目录下用一次make uninstall,但前提是make文件指定过uninstall。

    2. OpenSSL

      OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。,安装OpenSSL(http://www.openssl.org/source/)主要是为了让tengine支持Https的访问请求。具体是否安装看需求。 
      复制代码 代码如下:

      cd /usr/src
      wget http://www.openssl.org/source/openssl-1.0.2.tar.gz
      tar zxvf openssl-1.0.2.tar.gz
      cd openssl-1.0.2.tar.gz
      ./configure --prefix=/usr/local/openssl
      make && make install
    3. Zlib

      Zlib是提供资料压缩之用的函式库,当Tengine想启用GZIP压缩的时候就需要使用到Zlib(http://www.zlib.net/)。

      cd /usr/src
      wget http://zlib.net/zlib-1.2.11.tar.gz
      tar zxvf zlib-1.2.11.tar.gz
      cd zlib-1.2.11.tar.gz
      ./configure --prefix=/usr/local/zlib
      make && make install
    4. jemalloc

      jemalloc(http://www.canonware.com/jemalloc/)是一个更好的内存管理工具,使用jemalloc可以更好的优化Tengine的内存管理。

      cd /usr/src
      wget http://www.canonware.com/download/jemalloc/jemalloc-5.0.1.tar.bz2
      tar jxvf jemalloc-5.0.1.tar.bz2
      cd jemalloc-5.0.1.tar.bz2
      ./configure --prefix=/usr/local/jemalloc
      make && make install
  3. 安装Tengine

    在主要核心的组件安装完毕以后就可以安装Tegine了,最新版本的Tegine可从官网(http://tengine.taobao.org/)获取。 
    在编译安装前还需要做的一件事是添加一个专门的用户来执行Tengine。当然你也可以用root(不建议)。 
    复制代码 代码如下:

    groupadd tengine
    useradd -s /sbin/nologin -g tengine tengine

    注意: 如果要实现tcp代理需:

     下载nginx_tcp_proxy_module(下载地址:https://codeload.github.com/yaoweibin/nginx_tcp_proxy_module/zip/master):

     cd /usr/src/

     wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master.zip下载并解压到nginx_tcp_proxy_module-master目录下

    (如果patch未安装可以  yum -y install patch 安装)

    cd tengine-2.1.2

    patch -p1 < /usr/src/nginx_tcp_proxy_module-master/tcp.patch

    -----------------------------------------------------------------------

    patching file src/core/ngx_log.c

    Hunk #1 succeeded at 69 (offset 3 lines).

    patching file src/core/ngx_log.h

    Hunk #1 succeeded at 30 (offset 1 line).

    Hunk #2 succeeded at 38 (offset 1 line).

    patching file src/event/ngx_event_connect.h

    Hunk #1 succeeded at 33 (offset 1 line).

    Hunk #2 succeeded at 45 (offset 2 lines).

    -------------------------------------------------------------------------

    接下来才是进行安装:

    cd /usr/src
    wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
    tar -zxvf tengine-2.1.2.tar.gz
    cd tengine-2.1.2
    ./configure --prefix=/usr/local/tengine \
    --user=tengine \
    --group=tengine \
    --with-pcre=/usr/src/pcre-8.41 \
    --with-openssl=/usr/src/openssl-1.0.2 \
    --with-jemalloc=/usr/src/jemalloc-5.0.1 \
    --with-http_gzip_static_module \
    --with-http_realip_module \
    --with-http_stub_status_module \
    --with-http_concat_module \
    --with-zlib=/usr/src/zlib-1.2.11
    (如果需要tcp 需加上:  --add-module=/usr/src/nginx_tcp_proxy_module-master 
    即: ./configure --prefix=/usr/local/tengine/ --user=tengine --group=tengine --with-pcre=/usr/src/pcre-8.41 --with-openssl=/usr/src/openssl-1.0.2 --with-jemalloc=/usr/src/jemalloc-5.0.1 --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_concat_module --with-zlib=/usr/src/zlib-1.2.11 --with-http_ssl_module --add-module=/usr/src/nginx_tcp_proxy_module-master )
    
    make && make install

     

    注意配置的时候 –with-pcre 、–with-openssl、–with-jemalloc、–with-zlib的路径为源文件的路径。

  4. tengine 启停

  5. Centos7配置Tengine,设置tengine开机自启

    1. 系统用户登录系统后启动的服务的目录

      /usr/lib/systemd/system

    2. 如需要开机没有登陆情况下就能运行的程序在系统目录内

      /usr/lib/systemd/system

    3. 我希望系统开机就启动目录,所以我把文件放在系统目录内。

      vim /lib/systemd/system/nginx.service 创建文件

      [Unit]
      Description=The nginx HTTP and reverse proxy server
      After=syslog.target network.target remote-fs.target nss-lookup.target
      
      [Service]
      Type=forking
      PIDFile=/usr/local/nginx/logs/nginx.pid
      ExecStartPre=/usr/local/nginx/sbin/nginx -t
      ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
      ExecReload=/bin/kill -s HUP $MAINPID
      ExecStop=/bin/kill -s QUIT $MAINPID
      PrivateTmp=true
      
      [Install]
      WantedBy=multi-user.target
    4. 修改文件权限

      chmod 745 nginx.service
    5. 设置为开机启动

      systemctl enable nginx.service
    6. 其它命令

      启动nginx服务

      systemctl start nginx.service

      设置开机自启动

      systemctl enable nginx.service

      停止开机自启动

      systemctl disable nginx.service

      查看服务当前状态

      systemctl status nginx.service

      重新启动服务

      systemctl restart nginx.service

      查看所有已启动的服务

      systemctl list-units --type=service

 

三、配置文件nginx.conf常见配置项优化:

# cd /usr/local/tengine/conf

# cp nginx.conf nginx.conf.bak

# vim nginx.conf

======================================================================================

user tengine;                    # 运行worker进程的用户

worker_processes auto;           # worker进程的个数,命令ps aux | grep nginx可查看启动的worker进程数量

worker_cpu_affinity auto;         # worker进程绑定在哪些CPU上,减少由于上下文切换导致的CPU消耗

worker_rlimit_nofile 65535;       # worker进程能够打开的最大文件数限制

error_log logs/error.log error;     # 指定Tengine错误日志的存放路径和级别

pid logs/nginx.pid;              # 指定Tengine进程的pid文件路径

google_perftools_profiles /tmp/tcmalloc;   # tcmalloc保存路径,命令lsof -n | grep tcmalloc验证运行状态

 

events {

accept_mutex on;           # 让多个worker进程轮流地、序列化地响应新请求

multi_accept on;            # Tengine接收到一个新连接通知后,调用accept()来接收尽可能多的连接

worker_connections 65535;   # 单个worker进程所能响应的最大并发连接数

此处没有指定use epoll;指令,让Tengine自动选择合适的事件处理模型

}

 

http {

server_tokens off;           # 隐藏Tengine版本号

include mime.types;         # MIME是网络资源的媒体类型,使用include指令导入mime.types文件

default_type application/octet-stream;   # 指定默认类型

 

自定义访问日志格式,名称为main,指定要保存的日志内容

# ====================================================

# $remote_addr$http_x_forwarded_for含义相同:客户端的IP地址

# $remote_user:记录客户端用户名称

# $time_local:通用日志格式下的本地服务器时间

# $request:记录请求的URLHTTP协议

# $status:响应状态码,成功是200

# $body_bytes_sent:发送给客户端HTTP响应的主体内容的字节数大小,不包括响应首部的大小

# $http_referer:记录从哪个页面链接访问过来的

# $http_user_agent:记录客户端浏览器相关信息

# ====================================================

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 off;       # 关闭访问日志,提高磁盘的I/O性能

sendfile on;          开启高效文件传输模式,调用sendfile()进行数据复制,sendfile()复制数据是在内核级别完成的,所以会比一般的read()write()更高效,sendfile()可以在磁盘和TCP socket之间互相拷贝数据

tcp_nopush on;      # 开启后服务器的响应头部信息产生独立的数据包发送,即一个响应头信息一个包。在一个数据包里发送所有头文件,而不是一个接一个的发送,用于防止网络阻塞,必须先开启sendfile模式

keepalive_timeout 65; # 客户端keep-alive连接超时时长,服务器将在这个超时时长过后关闭连接,单位秒

charset UTF-8;      # 定义头文件默认字符集

 

gzip on;                      # 采用gzip压缩形式发送数据,将页面压缩后传输更节省带宽

gzip_disable "msie6";           # IE6的浏览器禁用gzip功能

gzip_proxied any;              # 指定对客户端请求的所有资源启用压缩功能

gzip_min_length 1k;            # 指定对数据启用压缩的最少字节数,如果请求小于1K的文件,不要压缩,压缩小数据会降低处理此请求的所有进程速度

gzip_comp_level 6;             # 指定数据的压缩等级,这个等级可以是1~9之间的任意数值,1压缩比最小但处理速度最快,9处理最慢但压缩比最大,CPU消耗也越大

gzip_buffers 16 8k;             # 指定压缩响应的缓冲区的数量和大小

gzip_vary on;                 # 允许把"Vary: Accept-Encoding"插入响应首部

gzip_http_version 1.1;          # 指定识别HTTP协议版本,默认是1.1

gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript application/xml application/xml+rss;               # 指定需要压缩的资源类型

 

reset_timedout_connection on;       # 关闭不响应的客户端连接,释放客户端所占的内存空间

client_body_buffer_size 8k;         # 指定用于读取客户端请求主体的缓冲区大小

client_header_buffer_size 1k;       # 指定用于读取客户端请求首部的缓冲区大小

large_client_header_buffers 4 8k;    # 指定用于读取客户端请求中较大首部的缓冲区的最大数量和大小

client_max_body_size 1m;          # 指定用于客户端请求主体的最大大小

client_header_timeout 30s;         # 指定用于读取客户端请求报文首部的超时时长,单位秒

client_body_timeout 30s;           # 指定用于读取客户端请求报文主体的超时时长,单位秒

send_timeout 30s;                 # 指定用于向客户端发送响应报文的超时时长,单位秒

}

====================================================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值