爲了加快網站的大流量訪問速度,公司要求把Nginx更換爲Tengine,下面記錄下整個安裝配置過程:javascript
#安裝必要依賴css
yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel glibc-headers gcc-c++java
#安裝google-perftools支持依賴node
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.98.tar.gznginx
tar xf libunwind-0.98.tar.gzc++
cd libunwind-0.98git
./configure && make && make installgithub
#安裝google-perftoolsweb
wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.4/gperftools-2.4.tar.gzapache
tar xf gperftools-2.4.tar.gz
cd gperftools-2.4
./configure --enable-frame-pointers
make && make install
#爲google-perftools添加線程目錄
mkdir /tmp/tcmalloc
chmod 0777 /tmp/tcmalloc
#下載Tengine最新穩定版本
wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
#解壓
tar xf tengine-2.1.2.tar.gz
cd tengine-2.1.2
#修改nginx.h,以隱藏版本信息
sed -i "12 s/1006002/1000001/g" src/core/nginx.h
sed -i "13 s/1.6.2/1.0.0/g" src/core/nginx.h
sed -i "14 s/nginx/webserver/g" src/core/nginx.h
sed -i "16 s/\"Tengine\"/\"X\"/g" src/core/nginx.h
sed -i "17 s/2001002/2000000/g" src/core/nginx.h
sed -i "18 s/2.1.2/2.0.0/g" src/core/nginx.h
sed -i "21 s/\"NGINX\"/\"X\"/g" src/core/nginx.h
sed -i "s/\/usr\/local/\/usr\/local\/gacp\/gperftools/g" auto/lib/google-perftools/conf
#添加運行用戶
useradd -s /sbin/nologin apache
#建立相應目錄
mkdir -p /usr/local/gacp/nginx
mkdir -p /data/logs/nginx/{access,error}
#編譯安裝
./configure \
--user=apache \
--group=users \
--prefix=/usr/local/gacp/nginx \
--error-log-path=/data/logs/nginx/error/error.log \
--http-log-path=/data/logs/nginx/access/access.log \
--pid-path=/usr/local/gacp/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-google_perftools_module \
--with-file-aio
make
make install
#配置文件nginx.conf
user apache;
worker_processes 12;
worker_rlimit_nofile 65535;
pid conf/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
error_log /data/logs/nginx/error/error.log crit;
events {
use epoll;
worker_connections 20960;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$upstream_addr $upstream_response_time $upstream_status';
access_log off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
keepalive_timeout 60;
include gzip.types;
include server.types;
include upstream.conf;
include vhosts.d/*.conf;
}
#配置文件gzip.types
zip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/css text/xml image/jpeg image/gif image/png;
gzip_vary on;
#配置文件server.types
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
server_names_hash_bucket_size 128;
client_max_body_size 8m;
open_file_cache max=10240 inactive=60s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
proxy_headers_hash_max_size 512;
proxy_headers_hash_bucket_size 64;
#配置文件flb_params
#配置文件upstream.conf
#負載均衡
#一、輪詢(默認)
#每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。
#二、weight
#指定輪詢概率,weight和訪問比率成正比,用於後端服務器性能不均的狀況。
#例如:
#upstream bakend {
#server 192.168.0.1 weight=10;
#server 192.168.0.2 weight=10;
#}
#三、ip_hash
#每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器。
#例如:
#upstream bakend {
#ip_hash;
#server 192.168.0.1:88;
#server 192.168.0.2:80;
#}
#每一個設備的狀態設置爲:
#1.down 表示單前的server暫時不參與負載
#2.weight 默認爲1.weight越大,負載的權重就越大。
#3.max_fails :容許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
#4.fail_timeout:max_fails次失敗後,暫停的時間。
#5.backup: 其它全部的非backup機器down或者忙的時候,請求backup機器。因此這臺機器壓力會最輕。
#6.check interval=3000 rise=2 fall=2 timeout=1000 type=http;用於自動判斷後端是否可用。
#####
upstream web_server
{
check interval=3000 rise=2 fall=2 timeout=1000 type=http;
server 1.1.1.1 max_fails=2 fail_timeout=30s;
server 2.2.2.2 max_fails=2 fail_timeout=30s;
ip_hash;
}
#配置文件proxy.vipbet.com.conf
server {
listen 58080;
server_name proxy.vipbet.com;
access_log /data/logs/nginx/access/proxy.vipbet.com.access.log main;
error_log /data/logs/nginx/error/proxy.vipbet.com.error.log;
location /
{
proxy_pass http://web_server;
include flb_params;
}
}
#更改目錄用戶組
chown -R apache.apache /usr/local/gacp/nginx
#啓動腳本:cat /etc/init.d/nginx
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemon
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
TENGINE_HOME="/usr/local/gacp/nginx/"
#這裏修改nginx的路徑
nginx=$TENGINE_HOME"sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE=$TENGINE_HOME"conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
killall -9 nginx
}
restart() {
test || return $?
stop
sleep 1
start
}
reload() {
test || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
test() {
$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|test)
$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|test}"
exit 2
esac
#chmod +x /etc/init.d/nginx
#service nginx start
#報錯/lib/lsb/init-functions: No such file or directory
yum install redhat-lsb
#報錯error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory
查看libprofiler.so.0是否存在:whereis libprofiler.so.0
libprofiler.so: /usr/local/lib/libprofiler.so /usr/local/lib/libprofiler.so.0
添加動態庫路徑: vi /etc/ld.so.conf.d/extend.conf
/usr/local/lib
裝載動態庫:ldconfig
#service nginx start
#####查看nginx進程
ps aux|grep nginx
#####查看google_perftools_profiles
lsof -n | grep tcmalloc