系统环境

标题 版本 备注
系统版本 ubuntu 22.04.3 LTS (Jammy Jellyfish) -
内核版本 5.15.0-91-generic #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux -
Tengine v3.1.0 -

Tengine 简介

Tengine是一个基于Nginx的高性能Web服务器,由淘宝网发起并开发,旨在为大型网站提供稳定、高性能的服务。它在Nginx的基础上进行了优化和扩展,提供了更多的功能和性能改进。

以下是Tengine的一些关键特性和优势:

  1. 高性能:Tengine在Nginx的基础上进行了性能优化,包括请求处理、负载均衡、反向代理等方面,提高了网站的响应速度和并发处理能力。

  2. 稳定性:Tengine经过了大规模的线上实践和测试,具有良好的稳定性和可靠性,适用于高并发、大流量的网站环境。

  3. 内置模块:Tengine内置了丰富的模块,包括HTTP模块、SSL模块、负载均衡模块、缓存模块等,提供了丰富的功能和灵活性。

  4. 动态扩展:Tengine支持动态加载模块,用户可以根据需要自定义扩展功能,满足不同的业务需求。

  5. 安全性:Tengine实现了一些安全功能,如防止DDoS攻击、防止SQL注入攻击等,保障了网站的安全性和可靠性。

  6. 易于配置:Tengine的配置文件语法与Nginx类似,易于理解和使用,同时提供了丰富的配置选项,满足不同场景的需求。

  7. 活跃的社区:Tengine拥有一个活跃的开发和用户社区,提供了丰富的文档、教程和支持资源,为用户提供了便利。

总的来说,Tengine是一种功能强大、性能优越、稳定可靠的Web服务器,适用于各种规模和类型的网站和应用程序。

Tengine 安装

注意:以下操作都是在 root 用户下执行。且该文章中的安装方式不支持 Lua 脚本

安装准备

1.安装依赖

1
apt install -y libmaxminddb-dev libxml2-dev libxslt1-dev libgd-dev libgeoip-dev libgoogle-perftools-dev autoconf automake build-essential libtool

2.创建运行 nginx 的用户

1
useradd -s /sbin/nologin nginx

下载解压 openssl

点击这里下载 openssl 到服务器的 /usr/local/src/ 目录并解压(注意:不用安装):

1
2
3
4
5
6
7
8
9
10
11
# 下载 OPENSSL:
wget -O /usr/local/src/openssl-1.1.1w.tar.gz https://www.openssl.org/source/openssl-1.1.1w.tar.gz

# 解压 OPENSSL:
tar -zxf /usr/local/src/openssl-1.1.1w.tar.gz -C /usr/local/src/

# CONFIGURE OPENSSL:
# ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl -fPIC -shared linux-x86_64

# MAKE 及 MAKE INSTALL:
# make && make install

下载解压 pcre(非 pcre2)

pcre 官方站点 下载源码包到 /usr/local/src/ 并解压到下载目录 /usr/local/src/

1
2
3
4
5
# 下载 PCRE:
wget -O /usr/local/src/pcre-8.45.tar.gz https://udomain.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz

# 解压 PCRE:
tar -zxf /usr/local/src/pcre-8.45.tar.gz -C /usr/local/src/

下载解压 zlib

点击这里下载 zlib 到服务器的 /usr/local/src/ 目录并解压(注意:不用安装):

1
2
3
4
5
# 下载 ZLIB
wget -O /usr/local/src/zlib-1.3.tar.gz https://www.zlib.net/zlib-1.3.tar.gz

# 解压 ZLIB:
tar -zxf /usr/local/src/zlib-1.3.tar.gz -C /usr/local/src/

下载安装 jemalloc:

从 github 上同步 jemalloc 源码到 /usr/local/src/ 目录下并执行 ./autogen.sh 生成 configure 文件

1
2
3
4
5
6
7
8
# 同步的代码
git clone https://github.com/jemalloc/jemalloc.git /usr/local/src/jemalloc

# 进入同步目录
cd !$

# 生成 configure 文件
./autogen.sh

下载安装 libatomic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 同步的代码
git clone https://github.com/ivmai/libatomic_ops.git /usr/local/src/libatomic_ops

# 进入同步目录
cd /usr/local/src/libatomic_ops

# 生成 configure 文件
./autogen.sh

# 配置 configure 参数:
./configure --prefix=/usr/local/libatomic_ops

# 执行 make (主要目的是生成 .libs/libatomic_ops.a 文件,后面不需要再执行 make install) ,在解压目录生成 .libs/libatomic_ops.a 文件:
make

# 做库文件的软链接
ln -s /usr/local/src/libatomic_ops/src/.libs/libatomic_ops.a /usr/local/src/libatomic_ops/src/

下载目录索引模块:ngx-fancyindex

从 github 上同步 ngx-fancyindex 源码到 /usr/local/src/ 目录下

1
git clone https://github.com/aperezdc/ngx-fancyindex.git /usr/local/src/ngx-fancyindex

下载IP定位模块:ngx_http_geoip2_module

从 github 上同步 ngx_http_geoip2_module 源码到 /usr/local/src/ 目录下

1
git clone https://github.com/leev/ngx_http_geoip2_module.git /usr/local/src/ngx_http_geoip2_module

下载安装 tengine

1.点击这里下载 tengine-3.1.0 源码文件到服务器的 /usr/local/src/ 目录下:

1
wget -O /usr/local/src/tengine-3.1.0.tar.gz https://tengine.taobao.org/download/tengine-3.1.0.tar.gz

2.将下载好的源码文件解压至下载目录:

1
tar -zxf /usr/local/src/tengine-3.1.0.tar.gz -C /usr/local/src/

3.进入 tengine 解压目录:

1
cd !$

4.配置编译参数如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
./configure   \
--prefix=/usr/local/nginx \
--modules-path=/usr/local/nginx/modules \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--error-log-path=/usr/local/nginx/logs/error.log \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--http-log-path=/usr/local/nginx/logs/access.log \
--http-client-body-temp-path=/usr/local/nginx/body_tmp/ \
--http-proxy-temp-path=/usr/local/nginx/proxy_tmp/ \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
--with-stream_sni \
--with-google_perftools_module \
--with-cpp_test_module \
--with-pcre=/usr/local/src/pcre-8.45/ \
--with-zlib=/usr/local/src/zlib-1.3/ \
--with-openssl=/usr/local/src/openssl-1.1.1w \
--with-jemalloc=/usr/local/src/jemalloc \
--with-libatomic=/usr/local/src/libatomic_ops \
--with-debug \
--without-http_upstream_keepalive_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--add-module=./modules/ngx_backtrace_module \
--add-module=./modules/ngx_http_concat_module \
--add-module=./modules/ngx_http_footer_filter_module \
--add-module=./modules/ngx_http_proxy_connect_module \
--add-module=./modules/ngx_http_reqstat_module \
--add-module=./modules/ngx_http_slice_module \
--add-module=./modules/ngx_http_sysguard_module \
--add-module=./modules/ngx_http_trim_filter_module \
--add-module=./modules/ngx_http_upstream_check_module \
--add-module=./modules/ngx_http_upstream_consistent_hash_module \
--add-module=./modules/ngx_http_upstream_keepalive_module \
--add-module=./modules/ngx_http_upstream_session_sticky_module \
--add-module=./modules/ngx_http_upstream_vnswrr_module \
--add-module=./modules/ngx_http_user_agent_module \
--add-module=./modules/ngx_multi_upstream_module \
--add-dynamic-module=/usr/local/src/ngx_http_geoip2_module \
--add-module=/usr/local/src/ngx-fancyindex

5.执行 make 命令开始编译:

1
make

6.执行 make install 命令进行安装:

1
make install

7.配置 tengine 系统环境变量

7.1.在 /etc/profile.d 目录下创建 nignx.sh 文件,并添加如下内容:

1
export PATH=$PATH:/usr/local/nginx/sbin

7.2.执行 source /etc/profile 命令使配置生效:

1
source /etc/profile

8.在 /lib/systemd/system 目录下创建 tengine 的启动脚本文件 nginx.service, 内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Nginx
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

9.执行命令 systemctl daemon-reload 加载 tengine 的启动脚本文件:

1
systemctl daemon-reload

10.执行命令 systemctl enable --now nginx.service 启动 tengine 并设置 tengine 开机自启动:

1
systemctl enable nginx.service

11.执行命令 systemctl status nginx.service 查看 tengine 的运行状态(如果 Active 状态是 running 的,则代表 tengine 启动成功):

1
systemctl status nginx.service

报错及解决方法

configure 报错

错误一:error: the geoip2 module requires the maxminddb library.

报错信息:

1
2
3
adding module in /usr/local/src/ngx_http_geoip2_module
checking for MaxmindDB library ... not found
./configure: error: the geoip2 module requires the maxminddb library.

解决方法:

安装 libmaxminddb-dev

1
apt install -y libmaxminddb-dev 

注意:如果这里使用 apt 安装 libmaxminddb-dev 时无法找到包,则更换清华大学的 apt 源再次安装即可!

错误二:error: the HTTP XSLT module requires the libxml2/libxslt

报错及解决方法错信息:

1
2
3
4
5
6
7
8
checking for OpenSSL DTLS support ... not found
checking for libxslt ... not found
checking for libxslt in /usr/local/ ... not found
checking for libxslt in /usr/pkg/ ... not found
checking for libxslt in /opt/local/ ... not found

./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.

解决方法:

安装 libxml2-dev

1
apt install -y libxml2-dev libxslt1-dev 

错误三:error: the HTTP image filter module requires the GD library.

报错信息:

1
2
3
4
5
6
7
8
9
10
hecking for OpenSSL DTLS support ... not found
checking for libxslt ... found
checking for libexslt ... found
checking for GD library ... not found
checking for GD library in /usr/local/ ... not found
checking for GD library in /usr/pkg/ ... not found
checking for GD library in /opt/local/ ... not found

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

解决方法:

安装 libgd-dev

1
apt install -y libgd-dev 

错误四:error: the GeoIP module requires the GeoIP library.

报错信息:

1
2
3
4
5
6
7
checking for GeoIP library ... not found
checking for GeoIP library in /usr/local/ ... not found
checking for GeoIP library in /usr/pkg/ ... not found
checking for GeoIP library in /opt/local/ ... not found

./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.

解决方法:

安装 libgeoip-dev

1
apt install -y libgeoip-dev

报错五:error: the Google perftools module requires the Google perftools

报错信息:

1
2
3
4
5
6
checking for Google perftools ... not found
checking for Google perftools in /usr/local/ ... not found
checking for Google perftools in /opt/local/ ... not found

./configure: error: the Google perftools module requires the Google perftools
library. You can either do not enable the module or install the library.

解决方法:

安装 libgoogle-perftools-dev

1
apt install -y libgoogle-perftools-dev

报错六: error: Libtool library used but ‘LIBTOOL’ is undefined

报错信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 安装 libatomic_ops 执行 autogen.sh 时报:
veazhi@vm002-ubuntu2204:/usr/local/src/libatomic_ops$ sudo ./autogen.sh
src/Makefile.am:14: error: Libtool library used but 'LIBTOOL' is undefined
src/Makefile.am:14: The usual way to define 'LIBTOOL' is to add 'LT_INIT'
src/Makefile.am:14: to 'configure.ac' and run 'aclocal' and 'autoconf' again.
src/Makefile.am:14: If 'LT_INIT' is in 'configure.ac', make sure
src/Makefile.am:14: its definition is in aclocal's search path.
autoreconf: error: automake failed with exit status: 1

# 安装 libatomic_ops 执行 ./configure --prefix=/usr/local/libatomic_ops 时报:
veazhi@vm002-ubuntu2204:/usr/local/src/libatomic_ops$ sudo ./configure --prefix=/usr/local/libatomic_ops
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a race-free mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking whether make supports the include directive... yes (GNU style)
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
checking for gcc option to enable C11 features... none needed
checking whether gcc understands -c and -o together... yes
checking dependency style of gcc... gcc3
checking dependency style of gcc... gcc3
./configure: line 4688: syntax error near unexpected token `disable-shared'
./configure: line 4688: `LT_INIT(disable-shared)'

解决方法:

安装 libtool 即可.

1
apt install -y libtool

make 报错:

错误一: error: Invalid C++ compiler or C++ compiler flags

报错信息:

1
2
3
4
5
checking for windows.h... no
configure: error: Invalid C++ compiler or C++ compiler flags
make[1]: *** [objs/Makefile:2230: /usr/local/src/pcre-8.45//Makefile] Error 1
make[1]: Leaving directory '/usr/local/src/tengine-3.1.0'
make: *** [Makefile:10: build] Error 2

解决方法:

安装 build-essential 即可!

1
apt install -y build-essential

错误二:cannot find /usr/local/src/libatomic_ops/src/libatomic_ops.a: No such file or directory

报错信息:

1
2
3
4
5
/usr/bin/ld: cannot find /usr/local/src/libatomic_ops/src/libatomic_ops.a: No such file or directory
collect2: error: ld returned 1 exit status
make[1]: *** [objs/Makefile:381: objs/nginx] Error 1
make[1]: Leaving directory '/usr/local/src/tengine-3.1.0'
make: *** [Makefile:10: build] Error 2

解决方法:

/usr/local/src/libatomic_ops/src/.libs/libatomic_ops.a 软链接到 /usr/local/src/libatomic_ops/src/ 即可!

1
ln -s /usr/local/src/libatomic_ops/src/.libs/libatomic_ops.a /usr/local/src/libatomic_ops/src/