Tengine介紹
Tengine是由淘寶網發起的Web服務器項目。它在Nginx 的基礎上,針對大訪問量網站的需求,添加了很多高級功能和特性。Tengine的性能和穩定性已經在大型的網站如淘寶網 ,天貓商城 等得到了很好的檢驗。它的最終目標是打造一個高效、穩定、安全、易用的Web平臺。
官網地址:http://tengine.taobao.org/
Tengine特性
- 繼承Nginx-1.8.1的所有特性,兼容Nginx的配置;
- 動態模塊加載(DSO) 支持。加入一個模塊不再需要重新編譯整個Tengine;
- 支持HTTP/2協議 ,HTTP/2模塊替代SPDY模塊;
- 流式上傳 到HTTP後端服務器或FastCGI服務器,大量減少機器的I/O壓力;
- 更加強大的負載均衡能力,包括一致性hash模塊 、會話保持模塊 ,還可以對後端的服務器進行主動健康檢查 ,根據服務器狀態自動上線下線,以及動態解析upstream中出現的域名 ;
- 輸入過濾器機制 支持。通過使用這種機制Web應用防火牆的編寫更爲方便;
- 支持設置proxy、memcached、fastcgi、scgi、uwsgi在後端失敗時的重試次數
- 動態腳本語言Lua 支持。擴展功能非常高效簡單;
安裝 Tengine,具有LUA模塊
1. 安裝必要的編譯環境
yum update
yum install gcc gcc-c++ autoconf automake
2. 安裝需要的組件
PCRE
PCRE(Perl Compatible Regular Expressions) http://www.pcre.org 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx rewrite依賴於PCRE庫,所以在安裝Tengine前一定要先安裝PCRE,最新版本的PCRE可在官網獲取。具體安裝流程爲:
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
tar zxvf pcre-8.40.tar.gz
cd pcre-8.40
./configure --prefix=/usr/local/pcre
make && make install
OpenSSL
OpenSSL http://www.openssl.org/source 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。安裝OpenSSL 主要是爲了讓tengine支持Https的訪問請求。具體是否安裝看需求。安裝流程爲:
cd /usr/local/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
./config --prefix=/usr/local/openssl
make && make install
Zlib
Zlib http://www.zlib.net 是提供資料壓縮之用的函式庫,當Tengine想啓用GZIP壓縮的時候就需要使用到Zli。安裝流程爲:
cd /usr/local/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
jemalloc
jemalloc http://www.canonware.com/jemalloc 是一個更好的內存管理工具,使用jemalloc可以更好的優化Tengine的內存管理。安裝流程爲:
cd /usr/local/src
wget https://src.fedoraproject.org/lookaside/pkgs/jemalloc/jemalloc-3.6.0.tar.bz2/e76665b63a8fddf4c9f26d2fa67afdf2/jemalloc-3.6.0.tar.bz2
tar jxvf jemalloc-3.6.0.tar.bz2
cd jemalloc-3.6.0.tar.bz2
./configure --prefix=/usr/local/jemalloc
make && make install
3. 安裝Tengine
在主要核心的組件安裝完畢以後就可以安裝Tegine了,最新版本的Tegine可從官網 http://tengine.taobao.org 獲取。在編譯安裝前還需要做的一件事是添加一個專門的用戶來執行Tengine。當然你也可以用root(不建議)。
添加用戶及用戶組:
# 添加www組
groupadd -r www
# 創建www運行賬戶nginx並加入到www組,不允許www用戶直接登錄系統
useradd -s /sbin/nologin -g www -r www
編譯安裝Tengine
cd /usr/local/src
wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
tar -zxvf tengine-2.2.0.tar.gz
cd tengine-2.2.0
./configure --prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-pcre=/usr/local/src/pcre-8.40 \
--with-openssl=/usr/local/src/openssl-1.0.2 \
--with-jemalloc=/usr/local/src/jemalloc-3.6.0 \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_concat_module \
--with-zlib=/usr/local/src/zlib-1.2.11
make && make install
注意配置的時候 –with-pcre 、–with-openssl、–with-jemalloc、–with-zlib的路徑爲源文件的路徑。
4. CentOS 7 配置Tengine,設置tengine開機自啓
# 系統用戶登錄系統後啓動的服務的目錄
/usr/lib/systemd/system
# 如需要開機沒有登陸情況下就能運行的程序在系統目錄內
/usr/lib/systemd/system
# 我希望系統開機就啓動目錄,所以我把文件放在系統目錄內。
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
# 修改文件權限
chmod 745 nginx.service
# 設置爲開機啓動
systemctl enable nginx.service
# 其它命令
# 啓動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
編輯Tengine操作腳本
vi /etc/rc.d/init.d/nginx #編輯啓動文件添加下面內容
############################################################
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# 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="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
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
fi
}
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: "
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
############################################################
配置權限及開機啓動
chmod 745 /etc/rc.d/init.d/nginx # 設置權限
chkconfig nginx on # 開機啓動
操作指令
# 開啓服務
/etc/init.d/nginx start
# 重啓服務
/etc/init.d/nginx restart
# 停止服務
/etc/init.d/nginx stop
# 查看服務狀態
/etc/init.d/nginx status