Nginx安装

Nginx 安装

简介

Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。
从2011年12月开始,Tengine成为一个开源项目,Tengine团队在积极地开发和维护着它。Tengine团队的核心成员来自于淘宝、搜狗等互联网企业。Tengine是社区合作的成果,我们欢迎大家参与其中,贡献自己的力量。

特性

  • 继承Nginx-1.6.2的所有特性,兼容Nginx的配置;
  • 动态模块加载(DSO)支持。加入一个模块不再需要重新编译整个Tengine;
  • 支持SO_REUSEPORT选项,建连性能提升为官方nginx的三倍;
  • 同时支持HTTP v2协议和SPDY v3协议,可同时使用两种协议;
    流式上传到HTTP后端服务器或FastCGI服务器,大量减少机器的I/O压力;
  • 更加强大的负载均衡能力,包括一致性hash模块、会话保持模块,还可以对后端的服务器进行主动健康检查,根据服务器状态自动上线下线,以及动态解析upstream中出现的域名;
  • 输入过滤器机制支持。通过使用这种机制Web应用防火墙的编写更为方便;
  • 支持设置proxy、memcached、fastcgi、scgi、uwsgi在后端失败时的重试次数
  • 动态脚本语言Lua支持。扩展功能非常高效简单;
  • 支持管道(pipe)和syslog(本地和远端)形式的日志以及日志抽样;
  • 支持按指定关键字(域名,url等)收集Tengine运行状态;
  • 组合多个CSS、JavaScript文件的访问请求变成一个请求;
  • 自动去除空白字符和注释从而减小页面的体积
  • 自动根据CPU数目设置进程个数和绑定CPU亲缘性;
  • 监控系统的负载和资源占用从而对系统进行保护;
  • 显示对运维人员更友好的出错信息,便于定位出错机器;
  • 更强大的防攻击(访问速度限制)模块;
  • 更方便的命令行参数,如列出编译的模块列表、支持的指令等;
    可以根据访问文件类型设置过期时间;
    ……

Tengine官网

依赖安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#创建nginx用户和组
groupadd www
useradd -g www -s/sbin/nologin -M www
#安装依赖的库文件和包
yum groupinstall "Development tools" "Development Libraries" -y
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel pcre gd-devel
# 安装jemalloc 优化nginx内存管理
cd /opt
wget http://www.canonware.com/download/jemalloc/jemalloc-4.2.1.tar.bz2
tar xjf jemalloc-4.2.1.tar.bz2
cd jemalloc-4.2.1
./configure
make && make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig

下载安装包

这里使用最新的2.1.2稳定版

1
wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz

编译安装

1
2
3
4
5
6
7
8
9
10
./configure --prefix=/usr/local/nginx \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_concat_module \
--with-pcre \
--with-http_sysguard_module \
--with-http_realip_module \
--with-jemalloc
make
make install
  • –prefix=/usr/local/nginx nginx安装目录
  • –with-http_gzip_static_module 支持gzip压缩
  • –with-http_stub_status_module 支持nginx状态检查
  • –with-http_concat_module 该模块类似于apache中的mod_concat模块,用于合并多个文件在一个响应报文中
  • –with-pcre 支持正则
  • –with-http_sysguard_module 当swap的剩余百分比,剩下的内存,load值或平均响应时间到设定的值时,就会跳转到action所指定的url
  • –with-http_realip_module 获取真实的用户IP
  • –with-jemalloc 使用jeamlloc优化内存管理

环境变量设置

1
2
echo "PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile
source /etc/profile

验证版本

1
2
nginx -v
Tengine version: Tengine/2.1.2 (nginx/1.6.2)

启动

1
nginx

访问

tengine

设置开机自启动

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
tee /etc/init.d/nginx<<EOF
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
EOF
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx --level 345 on
坚持原创技术分享,您的支持将鼓励我继续创作!