tengine编译安装,tengine编译


-------------------------------------------------------------------------------------------------------
1、官方源码包
wget http://tengine.taobao.org/download/tengine-2.3.0.tar.gz

 

2、基础依赖库
http://luajit.org/download/LuaJIT-2.0.5.tar.gz
https://www.openssl.org/source/openssl-1.0.2q.tar.gz
https://github.com/maxmind/geoip-api-c/releases/download/v1.6.12/GeoIP-1.6.12.tar.gz
https://sourceforge.net/projects/pcre/files/pcre/8.42/pcre-8.42.tar.gz
https://github.com/openresty/lua-cjson/archive/2.1.0.6.tar.gz
https://github.com/libgd/libgd/releases/download/gd-2.2.5/libgd-2.2.5.tar.gz
https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/v0.4.5.zip

3、第三方模块
1)需静态编译的C模块
https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
https://github.com/openresty/array-var-nginx-module/archive/v0.05.tar.gz
https://github.com/calio/form-input-nginx-module/archive/v0.12.tar.gz
https://github.com/openresty/encrypted-session-nginx-module/archive/v0.08.tar.gz
https://github.com/calio/iconv-nginx-module/archive/v0.14.tar.gz
https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
https://github.com/openresty/set-misc-nginx-module/archive/v0.32.tar.gz

2)通过DSO动态加载的C模块
https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz
https://github.com/openresty/lua-upstream-nginx-module/archive/v0.07.tar.gz
https://github.com/openresty/memc-nginx-module/archive/v0.19.tar.gz
https://github.com/vozlt/nginx-module-vts/archive/v0.1.18.tar.gz
https://github.com/weibocom/nginx-upsync-module/archive/v1.0.0.tar.gz
https://github.com/FRiCKLE/ngx_coolkit/archive/0.2.tar.gz
https://github.com/openresty/rds-csv-nginx-module/archive/v0.09.tar.gz
https://github.com/openresty/rds-json-nginx-module/archive/v0.15.tar.gz
https://github.com/openresty/redis2-nginx-module/archive/v0.15.tar.gz
https://github.com/openresty/srcache-nginx-module/archive/v0.31.tar.gz

3)第三方lua模块
https://github.com/hamishforbes/lua-resty-consul/archive/v0.3.1.tar.gz
https://github.com/cloudflare/lua-resty-cookie/archive/v0.1.0.tar.gz
https://github.com/openresty/lua-resty-core/archive/v0.1.15.tar.gz
https://github.com/openresty/lua-resty-dns/archive/v0.21.tar.gz
https://github.com/ledgetech/lua-resty-http/archive/v0.12.tar.gz
https://github.com/hamishforbes/lua-resty-iputils/archive/v0.3.0.tar.gz
https://github.com/doujiang24/lua-resty-kafka/archive/v0.06.tar.gz
https://github.com/upyun/lua-resty-limit-rate/archive/v0.1.tar.gz
https://github.com/openresty/lua-resty-limit-traffic/archive/v0.05.tar.gz
https://github.com/openresty/lua-resty-lock/archive/v0.07.tar.gz
https://github.com/cloudflare/lua-resty-logger-socket/archive/v0.1.tar.gz
https://github.com/openresty/lua-resty-lrucache/archive/v0.08.tar.gz
https://github.com/openresty/lua-resty-memcached/archive/v0.14.tar.gz
https://github.com/openresty/lua-resty-mysql/archive/v0.21.tar.gz
https://github.com/openresty/lua-resty-redis/archive/v0.26.tar.gz
https://github.com/bungle/lua-resty-session/archive/v2.23.tar.gz
https://github.com/openresty/lua-resty-string/archive/v0.11.tar.gz
https://github.com/bungle/lua-resty-template/archive/v1.9.tar.gz
https://github.com/openresty/lua-resty-upload/archive/v0.10.tar.gz
https://github.com/hamishforbes/lua-resty-upstream/archive/v0.09.tar.gz
https://github.com/openresty/lua-resty-upstream-healthcheck/archive/v0.05.tar.gz
https://github.com/openresty/lua-resty-websocket/archive/v0.06.tar.gz

-------------------------------------------------------------------------------------------------------
4、提取基础依赖库
1)基础目录创建
mkdir -p /data/rpm_packages
mkdir -p /data/binarys/tengine
mkdir -p /data/logs/nginx
mkdir -p /var/nginx_tmp/{client_body,proxy,fastcgi,uwsgi,scgi,nginx_temp}

2)基础库提取
cd LuaJIT-2.0.5
make PREFIX=/usr/local
make install PREFIX=/usr/local DESTDIR=/data/binarys/tengine
make install

cd pcre-8.42
./configure --prefix=/usr/local --enable-utf
make
make install DESTDIR=/data/binarys/tengine
make install

cd GeoIP-1.6.12
./configure --prefix=/usr/local
make
make install DESTDIR=/data/binarys/tengine
make install

cd lua-cjson-2.1.0.6
修改 Makefile:LUA_INCLUDE_DIR ?= /usr/local/include/luajit-2.0
make
make install DESTDIR=/data/binarys/tengine
make install

cd libgd-2.2.5
./configure --prefix=/usr/local
make
make install DESTDIR=/data/binarys/tengine
make install

3)加载库路径
cat > /etc/ld.so.conf.d/tengine_ld.so.conf <<EOF
/usr/local/lib
/usr/local/lib64
EOF

cat > /data/binarys/tengine/etc/ld.so.conf.d/tengine_ld.so.conf <<EOF
/usr/local/lib
/usr/local/lib64
EOF

/sbin/ldconfig

-------------------------------------------------------------------------------------------------------
5、tengine相关
1)tengine编译与提取
cd tengine-2.3.0
patch -p1 < /usr/local/packages/nginx_tcp_proxy_module-0.4.4/tcp.patch

./configure --prefix=/usr/local/nginx \
--lock-path=/var/lock/nginx.lock \
--pid-path=/var/run/nginx.pid \
--error-log-path=/data/logs/nginx/error.log \
--http-log-path=/data/logs/nginx/access.log \
--user=admin \
--group=admin \
--with-pcre=../pcre-8.42 \
--with-pcre-opt=-fPIC \
--with-openssl=../openssl-1.0.2q \
--with-openssl-opt=-fPIC \
--with-luajit-inc=/usr/local/include/luajit-2.0 \
--with-luajit-lib=/usr/local \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_concat_module=shared \
--with-http_sysguard_module=shared \
--with-http_geoip_module=shared \
--with-http_addition_module \
--with-http_image_filter_module \
--with-http_sub_module \
--with-http_random_index_module=shared \
--with-http_v2_module \
--with-http_dyups_module \
--with-http_fastcgi_module=shared \
--with-http_uwsgi_module=shared \
--with-http_scgi_module=shared \
--with-file-aio \
--with-ipv6 \
--with-cc-opt='-O2' \
--without-select_module \
--without-poll_module \
--add-module=../ngx_devel_kit-0.3.0 \
--add-module=../array-var-nginx-module-0.05 \
--add-module=../form-input-nginx-module-0.12 \
--add-module=../encrypted-session-nginx-module-0.08 \
--add-module=../iconv-nginx-module-0.14 \
--add-module=../lua-nginx-module-0.10.13 \
--add-module=../set-misc-nginx-module-0.32 \
--add-module=/usr/local/packages/nginx_tcp_proxy_module-0.4.5 \
--http-client-body-temp-path=/var/nginx_tmp/client_body \
--http-proxy-temp-path=/var/nginx_tmp/proxy \
--http-fastcgi-temp-path=/var/nginx_tmp/fastcgi \
--http-uwsgi-temp-path=/var/nginx_tmp/uwsgi \
--http-scgi-temp-path=/var/nginx_tmp/scgi

make
make install DESTDIR=/data/binarys/tengine
make install

rm -f /data/binarys/tengine/usr/local/nginx/html/*.html
rm -f /data/binarys/tengine/usr/local/nginx/conf/*.default
mkdir -p /data/binarys/tengine/usr/local/nginx/conf/{conf.d,lua,ssl,upstream,vhostes.d}

说明:
conf.d 存放一些功能子配置
lua lua模块存放目录
ssl https证书存放目录
upstream nginx upsream配置目录
vhosts.d nginx vhost配置目录

2)第三方C模块安装
mkdir /usr/local/src/nginx_build
cp -rf /tmp/packages/{echo-nginx-module-0.61,headers-more-nginx-module-0.33,lua-upstream-nginx-module-0.07,memc-nginx-module-0.19,nginx-module-vts-0.1.18,nginx-upsync-module-1.0.0,ngx_coolkit-0.2,rds-csv-nginx-module-0.09,rds-json-nginx-module-0.15,redis2-nginx-module-0.15,srcache-nginx-module-0.31} /usr/local/src/nginx_build
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/echo-nginx-module-0.61
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/headers-more-nginx-module-0.33
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/lua-upstream-nginx-module-0.07
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/memc-nginx-module-0.19
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/nginx-module-vts-0.1.18
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/nginx-upsync-module-1.0.0
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/ngx_coolkit-0.2
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/rds-csv-nginx-module-0.09
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/rds-json-nginx-module-0.15
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/redis2-nginx-module-0.15
/usr/local/nginx/sbin/dso_tool --add-module=/usr/local/src/nginx_build/srcache-nginx-module-0.31

cp -a /usr/local/nginx/modules/*.so /data/binarys/tengine/usr/local/nginx/modules/

3)nginx主配置(nginx.conf)
user admin admin;

worker_processes auto;
worker_cpu_affinity auto;

error_log /data/logs/nginx/error.log crit;
pid /var/run/nginx.pid;

worker_rlimit_nofile 65535;

include conf.d/ngx_dso_modules.conf;

events {
use epoll;
worker_connections 10240;
}

http {
server_tokens off;
server_tag off;
server_info off;
autoindex off;
include mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 128k;
large_client_header_buffers 4 32k;
client_max_body_size 30m;
client_body_buffer_size 256k;

sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css text/xml application/xml application/xml+rss application/javascript application/json text/javascript image/jpeg image/gif image/png;
gzip_vary on;

proxy_connect_timeout 60s;
proxy_read_timeout 120s;
proxy_send_timeout 120s;
proxy_buffer_size 128k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_headers_hash_max_size 1024;
proxy_headers_hash_bucket_size 128;

client_header_timeout 120s;
client_body_timeout 120s;
proxy_intercept_errors on;

proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header remote-port $remote_port;

proxy_temp_path /var/nginx_tmp/nginx_temp;

log_format main '{"@timestamp":"$time_iso8601",'
'"@source":"$server_addr",'
'"hostname":"$hostname",'
'"ip":"$http_x_forwarded_for",'
'"client":"$remote_addr",'
'"request_method":"$request_method",'
'"scheme":"$scheme",'
'"domain":"$server_name",'
'"referer":"$http_referer",'
'"request":"$request_uri",'
'"args":"$args",'
'"size":$body_bytes_sent,'
'"status": $status,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamaddr":"$upstream_addr",'
'"http_user_agent":"$http_user_agent",'
'"https":"$https"'
'}';

access_log /data/logs/nginx/access.log main;

map $remote_addr $my_real_ip {
default $remote_addr;
}

map $remote_addr $my_peer_ip {
default $remote_addr;
}

map $http_x_forwarded_proto $https_status {
default off;
https on;
}

req_status_zone server_reqstat_monitor "$host" 100M;

lua_package_path '/usr/local/nginx/conf/lua/lib/?.lua;;';
lua_package_cpath '/usr/local/nginx/conf/lua/lib/?.so;;';

include conf.d/ngx_func_*.conf;
include upstream/*.conf;
include vhosts.d/*.conf;
}

4)dso加载配置(conf.d/ngx_dso_modules.conf)
dso {
load ngx_http_sysguard_module.so;
load ngx_http_upsync_module.so;
}

5)附加功能配置(conf.d/ngx_func_sysguard.conf)
server {
sysguard on;
sysguard_mode or;

sysguard_load load=10.5 action=/loadlimit;
sysguard_cpu usage=20 period=3s action=/cpulimit;
sysguard_mem swapratio=20% action=/swaplimit;
sysguard_mem free=100M action=/freelimit;
sysguard_rt rt=0.01 period=5s action=/rtlimit;

location /loadlimit {
return 503;
}

location /swaplimit {
return 503;
}

location /freelimit {
return 503;
}

location /rtlimit {
return 503;
}

location /cpulimit {
return 503;
}
}

6)默认vhost配置,主要用于监控(vhosts.d/default.my.com.conf)
server {
listen 80;
server_name 127.0.0.1;

charset utf-8;

location = /upstream_status {
check_status;
access_log off;
}

location = /req_status {
req_status_show;
access_log off;
}

location = /nginxstatus {
stub_status on;
access_log off;
}

location = /luatest {
content_by_lua_block {
ngx.say('hello world ^_^')
}
}
}

7)日志切割
cat > /data/binarys/tengine/etc/logrotate.d/nginx <<EOF
/data/logs/nginx/*.log
{
rotate 7
daily
dateext
dateformat .%Y%m%d
compress
missingok
notifempty
create 0644 admin admin
sharedscripts
postrotate
/usr/bin/killall -HUP rsyslogd >/dev/null 2>&1 || true
[ -f /var/run/nginx.pid ] && kill -USR1 \`cat /var/run/nginx.pid\`
endscript
}
EOF

8)nginx启动脚本(采用systemd管理)
cat > /data/binarys/tengine/usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /var/run/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
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

说明:
建议基础服务,都统一采用标准的systemd管理。
启动命令(nginx安装后会自动启动):
systemctl start nginx.service

-------------------------------------------------------------------------------------------------------
6、tengine打包
1)打包工具安装
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
gem install fpm

2)一些辅助脚本
--安装前要执行的脚本:
cat > /data/binarys/tengine/var/scripts/install_tengine_before.sh <<EOF
#!/bin/bash

mkdir -p /data/logs/nginx
mkdir -p /var/nginx_tmp/{client_body,proxy,fastcgi,uwsgi,scgi,nginx_temp}
/sbin/ldconfig

exit 0
EOF

--安装后要执行的脚本:
cat > /data/binarys/tengine/var/scripts/install_tengine_after.sh <<EOF
#!/bin/bash

NGX_VERSION='2.2.3'

cd /usr/local
mv nginx tengine-\${NGX_VERSION}
ln -s tengine-\${NGX_VERSION} nginx
[[ -e /usr/bin/nginx ]] || ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
/sbin/ldconfig
systemctl enable nginx.service >/dev/null 2>&1
systemctl start nginx.service

exit 0
EOF

--卸载前要执行的脚本:
cat > /data/binarys/tengine/var/scripts/uninstall_tengine_before.sh <<EOF
#!/bin/bash

[[ -n \`ps aux|grep sbin/nginx|grep -v grep\` ]] && killall -9 nginx
rm -f /usr/bin/nginx
systemctl disable nginx.service >/dev/null 2>&1

exit 0
EOF

--卸载后要执行的脚本:
cat > /data/binarys/tengine/var/scripts/uninstall_tengine_after.sh <<EOF
#!/bin/bash

rm -rf /usr/local/tengine* /usr/local/nginx
rm -f /etc/logrotate.d/nginx
rm -rf /var/nginx_tmp /data/logs/nginx

exit 0
EOF

3)rpm打包

yum install rpm-build -y
yum install ruby-devel

fpm -s dir -t rpm -n tengine -v 2.2.3 \
--iteration 1.my.el7 \
-C /data/binarys/tengine/ \
-p /data/rpm_packages \
--description 'my tengine rpm package' \
--url 'http://tengine.taobao.org' \
-d 'libpng,libpng-devel,fontconfig,fontconfig-devel,libjpeg,libjpeg-devel,libXpm,libXpm-devel,libxslt,libxslt-devel,zlib,zlib-devel,lynx,gcc-c++,gd' \
--pre-install /data/binarys/tengine/var/scripts/install_tengine_before.sh \
--post-install /data/binarys/tengine/var/scripts/install_tengine_after.sh \
--pre-uninstall /data/binarys/tengine/var/scripts/uninstall_tengine_before.sh \
--post-uninstall /data/binarys/tengine/var/scripts/uninstall_tengine_after.sh

rpm包信息查看:
rpm -qp --scripts tengine-2.2.3-1.my.el7.x86_64.rpm

rpm包安装:
yum -y localinstall tengine-2.2.3-1.my.el7.x86_64.rpm

rpm包卸载:
rpm -e tengine

说明:
建议构建自己的yum仓库,将自定制的基础服务rpm包统一放到仓库中,然后直接yum安装,如:yum -y install tengine

yum -y localinstall tengine-2.2.3-1.my.el7.x86_64.rpm

这样可以解决依赖问题。

 

相关内容

    暂无相关文章