系统环境
标题
版本
备注
系统版本
ubuntu 22.10
-
内核版本
5.15.0-45-generic #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
-
Tengine
v3.0.0
-
Tengine 简介 Tengine是一个基于Nginx的高性能Web服务器,由淘宝网发起并开发,旨在为大型网站提供稳定、高性能的服务。它在Nginx的基础上进行了优化和扩展,提供了更多的功能和性能改进。
以下是Tengine的一些关键特性和优势:
高性能 :Tengine在Nginx的基础上进行了性能优化,包括请求处理、负载均衡、反向代理等方面,提高了网站的响应速度和并发处理能力。
稳定性 :Tengine经过了大规模的线上实践和测试,具有良好的稳定性和可靠性,适用于高并发、大流量的网站环境。
内置模块 :Tengine内置了丰富的模块,包括HTTP模块、SSL模块、负载均衡模块、缓存模块等,提供了丰富的功能和灵活性。
动态扩展 :Tengine支持动态加载模块,用户可以根据需要自定义扩展功能,满足不同的业务需求。
安全性 :Tengine实现了一些安全功能,如防止DDoS攻击、防止SQL注入攻击等,保障了网站的安全性和可靠性。
易于配置 :Tengine的配置文件语法与Nginx类似,易于理解和使用,同时提供了丰富的配置选项,满足不同场景的需求。
活跃的社区 :Tengine拥有一个活跃的开发和用户社区,提供了丰富的文档、教程和支持资源,为用户提供了便利。
总的来说,Tengine是一种功能强大、性能优越、稳定可靠的Web服务器,适用于各种规模和类型的网站和应用程序。
Tengine 安装 注意:以下操作都是在 root 用户下执行。且该文章中的安装方式支持 Lua 脚本
一键安装 在 /usr/local/sbin/
目录下创建安装脚本文件 tengine.sh
,内容为:
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 #!/usr/bin/env bash set -euCOLOR_R='\e[0;31m' COLOR_G='\e[0;32m' COLOR_Y='\e[0;33m' COLOR_P='\e[0m' SRC_DIR="/usr/local/src" OPENSSL_DIR="$SRC_DIR /openssl-1.1.1u" PCRE_DIR="$SRC_DIR /pcre-8.45" ZLIB_DIR="$SRC_DIR /zlib-1.3.1" LUA_DIR="/usr/local/lua" LUAJIT_DIR="/usr/local/LuaJIT2" JEMALLOC_DIR="$SRC_DIR /jemalloc" LIBATOMIC_DIR="$SRC_DIR /libatomic_ops" TENGINE_DIR="$SRC_DIR /tengine-3.0.0" NGINX_DIR="/usr/local/nginx" install_packages () { apt install -y gcc g++ git wget make autoconf automake libtool libmaxminddb-dev libxslt1-dev libgd-dev libgeoip-dev libgoogle-perftools-dev libunwind-dev } download_and_extract () { local url=$1 local dir =$2 if [ ! -d "$dir " ]; then wget -O "${dir} .tar.gz" "$url " tar -zxf "${dir} .tar.gz" -C "$SRC_DIR " fi } install_lua () { if [ ! -d "$LUA_DIR " ]; then download_and_extract "http://www.lua.org/ftp/lua-5.4.6.tar.gz" "$SRC_DIR /lua-5.4.6" cd "$SRC_DIR /lua-5.4.6" make all test make install INSTALL_TOP="$LUA_DIR " fi if [ ! -d "$LUAJIT_DIR " ]; then git clone https://github.com/openresty/luajit2.git "$SRC_DIR /luajit2" cd "$SRC_DIR /luajit2" make install PREFIX="$LUAJIT_DIR " git clone https://github.com/openresty/lua-resty-core.git "$SRC_DIR /lua-resty-core" cd "$SRC_DIR /lua-resty-core" make install PREFIX="$LUAJIT_DIR " git clone https://github.com/openresty/lua-resty-lrucache.git "$SRC_DIR /lua-resty-lrucache" cd "$SRC_DIR /lua-resty-lrucache" make install PREFIX="$LUAJIT_DIR " ln -s "$LUAJIT_DIR /lib/libluajit-5.1.so.2" /usr/lib/libluajit-5.1.so.2 fi } install_jemalloc () { if [ ! -d "$JEMALLOC_DIR " ]; then git clone https://github.com/jemalloc/jemalloc.git "$JEMALLOC_DIR " if [ ! -f "$JEMALLOC_DIR /configure" ]; then cd "$JEMALLOC_DIR " ./autogen.sh fi fi } install_libatomic () { if [ ! -d "$LIBATOMIC_DIR " ]; then git clone https://github.com/ivmai/libatomic_ops.git "$LIBATOMIC_DIR " cd "$LIBATOMIC_DIR " ./autogen.sh ln -s "$LIBATOMIC_DIR /src/.libs/libatomic_ops.a" "$LIBATOMIC_DIR /src/" fi } download_nginx_extend () { if [ ! -d "$SRC_DIR /ngx-fancyindex" -a ! -d "$SRC_DIR /ngx_http_geoip2_module" ]; then git clone https://github.com/aperezdc/ngx-fancyindex.git "$SRC_DIR /ngx-fancyindex" git clone https://github.com/leev/ngx_http_geoip2_module.git "$SRC_DIR /ngx_http_geoip2_module" fi } install_tengine () { if [ ! -d "$TENGINE_DIR " ]; then download_and_extract "http://tengine.taobao.org/download/tengine-3.0.0.tar.gz" "$TENGINE_DIR " fi if [ ! -d "$NGINX_DIR " ]; then cd "$TENGINE_DIR " ./configure --prefix="$NGINX_DIR " \ --modules-path="$NGINX_DIR /modules" \ --pid-path="$NGINX_DIR /logs/nginx.pid" \ --error-log-path="$NGINX_DIR /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 \ --with-http_lua_module \ --http-log-path="$NGINX_DIR /logs/access.log" \ --http-client-body-temp-path="$NGINX_DIR /body_tmp/" \ --http-proxy-temp-path="$NGINX_DIR /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-luajit-lib="$LUAJIT_DIR /lib" \ --with-luajit-inc="$LUAJIT_DIR /include/luajit-2.1" \ --with-lua-lib="$LUA_DIR /lib" \ --with-lua-inc="$LUA_DIR /include" \ --with-pcre="$PCRE_DIR " \ --with-zlib="$ZLIB_DIR " \ --with-openssl="$OPENSSL_DIR " \ --with-jemalloc="$JEMALLOC_DIR " \ --with-libatomic="$LIBATOMIC_DIR " \ --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="$SRC_DIR /ngx_http_geoip2_module" \ --add-module="$SRC_DIR /ngx-fancyindex" make -j "$(nproc) " make install ln -s "$NGINX_DIR /sbin/nginx" /usr/sbin/ else echo -e "${COLOR_G} Tengine was installed...${COLOR_P} " fi } main () { if [ "$(id -u) " -ne 0 ]; then echo -e "$COLOR_R 请以root用户或使用sudo权限执行此脚本.$COLOR_P " exit 1 fi [ -z "$(grep nginx /etc/passwd) " ] && useradd -s /sbin/nologin nginx install_packages read -p "$(echo -e "${COLOR_Y} 是否安装OpenSSL? [y/n]${COLOR_P} " ) " -n 1 -r echo if [[ "$REPLY " =~ ^[Yy]$ ]]; then download_and_extract "https://www.openssl.org/source/openssl-1.1.1u.tar.gz" "$OPENSSL_DIR " fi read -p "$(echo -e "${COLOR_Y} 是否安装PCRE? [y/n]${COLOR_P} " ) " -n 1 -r echo if [[ "$REPLY " =~ ^[Yy]$ ]]; then download_and_extract "https://udomain.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz" "$PCRE_DIR " fi read -p "$(echo -e "${COLOR_Y} 是否安装zlib? [y/n]${COLOR_P} " ) " -n 1 -r echo if [[ "$REPLY " =~ ^[Yy]$ ]]; then download_and_extract "https://zlib.net/zlib-1.3.1.tar.gz" "$ZLIB_DIR " fi read -p "$(echo -e "${COLOR_Y} 是否安装Lua? [y/n]${COLOR_P} " ) " -n 1 -r echo if [[ "$REPLY " =~ ^[Yy]$ ]]; then install_lua fi read -p "$(echo -e "${COLOR_Y} 是否安装Jemalloc? [y/n]${COLOR_P} " ) " -n 1 -r echo if [[ "$REPLY " =~ ^[Yy]$ ]]; then install_jemalloc fi read -p "$(echo -e "${COLOR_Y} 是否安装libatomic? [y/n]${COLOR_P} " ) " -n 1 -r echo if [[ "$REPLY " =~ ^[Yy]$ ]]; then install_libatomic fi download_nginx_extend install_tengine } main
手动安装 安装必要依赖的包: 1 apt install -y gcc g++ make git autoconf automake libtool libmaxminddb-dev libxslt1-dev libgd-dev libgeoip-dev libgoogle-perftools-dev libunwind-dev
下载解压:openssl-1.1.1u 从 openssl 官方站点 下载源码包到 /usr/local/src/ 并解压到下载目录 /usr/local/src/
1 2 3 4 5 6 7 8 9 10 11 wget -O /usr/local/src/openssl-1.1.1u.tar.gz https://www.openssl.org/source/openssl-1.1.1u.tar.gz tar -zxf /usr/local/src/openssl-1.1.1u.tar.gz -C /usr/local/src/ cd openssl-1.1.1u
下载解压:pcre(非 pcre2) 从 pcre 官方站点 下载源码包到 /usr/local/src/ 并解压到下载目录 /usr/local/src/
1 2 3 4 5 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 tar -zxf /usr/local/src/pcre-8.45.tar.gz -C /usr/local/src/
下载解压:zlib 从 zlib 官方站点 下载源码包到 /usr/local/src/ 并解压到下载目录 /usr/local/src/
1 2 3 4 5 wget -O /usr/local/src/zlib-1.3.1.tar.gz https://zlib.net/zlib-1.3.1.tar.gz tar -zxf /usr/local/src/zlib-1.3.1.tar.gz -C /usr/local/src/
下载安装:lua-5.4.6 从 lua 官方站点 下载 lua 的源码包到 /usr/local/src/ 目录下并安装到 /usr/local/lua 目录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 wget -O /usr/local/src/lua-5.4.6.tar.gz http://www.lua.org/ftp/lua-5.4.6.tar.gz tar -zxf /usr/local/src/lua-5.4.6.tar.gz -C /usr/local/src/ cd /usr/local/src/lua-5.4.6/make all test make install INSTALL_TOP=/usr/local/lua
安装 luajit2 及其它 lua 组件 这里的 luajit2、lua-resty-core 以及 lua-resty-lrucache 都安装到同一个目录
下载安装:luajit2 1 2 3 4 5 6 7 8 git clone https://github.com/openresty/luajit2.git /usr/local/src/luajit2 cd /usr/local/src/luajit2make install PREFIX=/usr/local/LuaJIT2
下载安装: 1 2 3 4 5 6 7 8 9 git clone https://github.com/openresty/lua-resty-core.git /usr/local/src/lua-resty-core cd /usr/local/src/lua-resty-coremake install LUA_LIB_DIR=/usr/local/share/lua/5.1
下载安装: 1 2 3 4 5 6 7 8 9 git clone https://github.com/openresty/lua-resty-lrucache.git /usr/local/src/lua-resty-lrucache cd /usr/local/src/lua-resty-lrucachemake install LUA_LIB_DIR=/usr/local/share/lua/5.1
最后,将 luajit2 模块软链接到 /usr/lib/ 及 /usr/lib64/ 目录:
1 2 ln -s /usr/local/LuaJIT2/lib/libluajit-5.1.so.2.1.0 /usr/lib/libluajit-5.1.so.2ln -s /usr/local/LuaJIT2/lib/libluajit-5.1.so.2.1.0 /usr/lib64/libluajit-5.1.so.2
下载安装: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 !$./autogen.sh
下载安装:libatomic: 从 github 上同步 libatomic_ops 源码到 /usr/local/src/ 目录下并执行 ./autogen.sh 生成 configure 文件
1 2 3 4 5 6 7 8 9 10 11 git clone https://github.com/ivmai/libatomic_ops.git /usr/local/src/libatomic_ops cd /usr/local/src/libatomic_ops./autogen.sh 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 官方站点 下载 tengine 源码包到 /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.进入解压目录:
1 cd /usr/local/src/tengine-3.1.0
4.添加运行 tengine 的用户:
1 useradd -s /sbin/nologin nginx
5.配置编译参数:
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 65 66 67 68 69 ./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 \ --with-http_lua_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-luajit-lib=/usr/local/LuaJIT2/lib \ --with-luajit-inc=/usr/local/LuaJIT2/include/luajit-2.1 \ --with-lua-lib=/usr/local/lua/lib \ --with-lua-inc=/usr/local/lua/include \ --with-pcre=/usr/local/src/pcre-8.45/ \ --with-zlib=/usr/local/src/zlib-1.3.1/ \ --with-openssl=/usr/local/src/openssl-1.1.1u \ --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
6.编译:
7.安装:
8.将 nginx 可执行文件软链接到 /usr/sbin/ 目录下:
1 ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
9.创建 tengine 启动脚本文件 /lib/systemd/system/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
10.启动 nginx 并设置为开机启动
1 systemctl enable --now nginx.service
报错及解决方法: 启动报错: 错误一:error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 详细信息:
1 2 3 4 Apr 12 09:43:53 vm001-ubuntu2204 nginx[37160]: /usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory Apr 12 09:43:53 vm001-ubuntu2204 systemd[1]: nginx.service: Control process exited, code=exited, status=127/n/a Apr 12 09:43:53 vm001-ubuntu2204 systemd[1]: nginx.service: Failed with result 'exit-code' . Apr 12 09:43:53 vm001-ubuntu2204 systemd[1]: Failed to start Nginx.
解决方法:
将 LuaJIT 安装目录下的 libluajit-5.1.so.2 文件复制到如下目录:
1 2 cp /usr/local/LuaJIT2/lib/libluajit-5.1.so.2 /usr/local/lib/cp /usr/local/LuaJIT2/lib/libluajit-5.1.so.2 /usr/lib/
错误二:nginx -t 报:[warn] could not build optimal variables_hash 相信信息:
1 2 3 4 root@vultr:/usr/local/src/tengine-3.0.0 nginx: [warn] could not build optimal variables_hash, you should increase either variables_hash_max_size: 1024 or variables_hash_bucket_size: 64; nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
解决方法:
编辑 nginx 主配置文件,在 http{…} 中添加(注意:不能添加到 server{…}):
1 2 variables_hash_max_size 4096; variables_hash_bucket_size 2048;
错误三:报:failed to load the ‘resty.core’ module 相信信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 2023/07/26 10:25:01 [alert] 345323 no field package.preload['resty.core' ] no file '../lua-resty-core/lib/resty/core.lua' no file '../lua-resty-lrucache/lib/resty/core.lua' no file './resty/core.lua' no file '/usr/local/share/luajit-2.1.0-beta3/resty/core.lua' no file '/usr/local/share/lua/5.1/resty/core.lua' no file '/usr/local/share/lua/5.1/resty/core/init.lua' no file './resty/core.so' no file '/usr/local/lib/lua/5.1/resty/core.so' no file '/usr/local/lib/lua/5.1/loadall.so' no file './resty.so' no file '/usr/local/lib/lua/5.1/resty.so' no file '/usr/local/lib/lua/5.1/loadall.so' ) in /usr/local/nginx/conf/nginx.conf:137
解决方法:
根据提示,
1 cp -r /usr/local/LuaJIT2/lib/lua/resty /usr/local/share/luajit-2.1.0-beta3/