212dqw 发表于 2015-5-27 09:21:57

LNMP环境之Nginx/Tengine的源代码安装及优化

一.介绍:
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。
二.下载并安装:

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






安装前需要安装依赖的库文件:

1
2
3
4
5
yuminstall pcre-devel -y//pcer是提供正则表达的
# tar xf tengine-2.0.1.tar.gz
[root@kalitengine-2.1.0]# useradd-r nginx
[root@kalitengine-2.1.0]#mkdir /var/tmp/nginx/{proxy,fastcgi,uuwsgi,client}-pv
[root@kalitengine-2.1.0]#./configure --prefix=/usr/local/nginx--user=nginx --group=nginx--error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid--lock-path=/var/lock/nginx.lock --with-http_ssl_module--with-http_stub_status_module --with-http_gzip_static_module--with-http_flv_module --with-http_mp4_module--http-client-body-temp-path=/var/tmp/nginx/client--http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi && make && make install






三.启动nginx
1.为nginx/tengine创建启动脚本
#vim /etc/rc.d/init.d/nginx

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
#!/bin/sh
# chkconfig: - 5545
# description:The nginx daemon is a web service.
# processname:nginx
./etc/rc.d/init.d/functions

NGINX="/usr/local/nginx/sbin/nginx"

start()
{
      echo -n $"Starting nginx: "
      daemon $NGINX
      echo
}

stop()
{
      echo -n $"Shutting down nginx:"
      $NGINX -s stop
      echo
}

quit()
{
      echo -n $"Shutting down nginx:"
      $NGINX -s quit
      echo
}

reload()
{
      echo -n $"reload config:"
      $NGINX -s reload
      echo
}

[ -f $NGINX ] ||exit 1

# See how we werecalled.
case "$1"in
start)
      start
      ;;
stop)
      quit
      ;;
reload)
      reload
      ;;
restart)
      stop
      sleep 3
      start
      ;;
*)
      echo $"Usage: $0{start|stop|restart|reload}"
      exit 1
esac
exit 0




2.给其赋予执行权限并添加系统服务并开机启动

1
2
3
4
#chmod+x /etc/rc.d/init.d/nginx
#chkconfig--add nginx
#chkconfig--level 35 nginx on
#service nginx start






3.若要启用nginx/tengine的虚拟目录,在主配置文件添加以下参数,并在conf/文件内创建vhosts/,并创建配置文件。

1
include vhosts/*.conf;    #nginx虚拟主机包含文件目录






如:在#vim/usr/local/nginx/conf/vhosts/wp.conf

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
server{
      listen 80;

      server_name 192.168.1.1;

      root/home/wp/;    #配置发布目录

      access_log      logs/www_lolfs.log main;

      location / {
            indexindex.php index.html index.htm;
      }

      location ~ \.php$ {
            root         /home/wp/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_indexindex.php;
            fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
            include      fastcgi_params;
      }
         
      location ~ \.(cgi|pl)?$ {
            gzip off;
            root   /usr/local/nagios/sbin;
            rewrite ^/nagios/cgi-bin/(.*)\.cgi/$1.cgi break;
            fastcgi_pass unix:/usr/local/perl-fcgi/logs/perl-fcgi.sock;
            fastcgi_paramQUERY_STRING       $query_string;
            fastcgi_paramREQUEST_METHOD   $request_method;
            fastcgi_param SCRIPT_FILENAME/usr/local/nagios/sbin$fastcgi_script_name;
            fastcgi_index index.cgi;
            fastcgi_read_timeout   60;
            #fastcgi_paramREMOTE_USER      $remote_user;
            #auth_basic "NagiosAccess";
            #auth_basic_user_file/usr/local/nagios/etc/nagiospasswd;
      }


      location ~* ^.+.(jpg|jpeg|gif|png|ico)${
            access_log   off;
            expires      30d;
      }

      location ~* ^.+.(js|css)$ {
            access_log   off;
            expires      1h;
      }

      location ~* ^.+.(html|php)$ {
            access_log   off;
            expires      10m;
      }

}




四.nginx/tengine的优化
为tengine配置一下系统的TCP设置,优化一下
Vi /etc/syscrl.conf

1
net.ipv4.tcp_syncookies= 1




# 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;

1
net.ipv4.tcp_tw_reuse= 1




# 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;

1
net.ipv4.tcp_tw_recycle= 1




# 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。

1
net.ipv4.tcp_fin_timeout




# 修改系統默认的 TIMEOUT 时间

1
2
sapi/fpm/init.d.php-fpm
net.ipv4.tcp_fin_timeout = 1




# 表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间

1
net.ipv4.tcp_keepalive_time = 1200




# 表示当keepalive起用的时候,tcp发送keepalive消息的频度,缺省是2小时,改为20分钟

1
net.ipv4.tcp_mem = 94500000 915000000 927000000




# 当tcp使用低于该值的内存页面数时,tcp不会考虑释放内存

1
net.ipv4.tcp_tw_reuse = 1




# 表示开启重用.允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭

1
net.ipv4.tcp_timestamps = 0




# 时间戳可以避免序列号的卷绕。1个1Gbps的链路肯定会遇到以前用过的序列号。时间戳能让内核接受这种“异常”的数据包,这里需要将其关闭

1
net.ipv4.tcp_synack_retries = 1




# 为了打开对端的连接,内核需要发送一个syn并附带一个回应前面一个syn的ack,也就是所谓三次握手中的第二次握手,这个设置决定了内核放弃连接之前发送syn+ack包的数量

1
net.ipv4.tcp_syn_retries = 1




# 在内核放弃建立连接之前发送syn包的数量

1
net.ipv4.tcp_tw_recycle = 1




# 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭

1
net.core.netdev_max_backlog = 262144




# 每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目

1
net.core.somaxconn = 262144




# web应用中listen函数的backlog默认会给我们内核参数net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认为511,所以有必要调整这个值

1
net.ipv4.tcp_max_orphans = 3276800




# 系统中最多有多少个tcp套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的dos攻击,不能过分依靠它或者人为地减少这个值,更应该增加这个值(如果增加了内存之后)

1
net.ipv4.tcp_max_syn_backlog = 262144





# 表示syn队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接网络连接数。对于有128M内存的系统而言,缺省值是1024,小内存的系统则是128

1
net.core.rmem_max= 16777216





# 最大socket读buffer,可参考的优化值:873200

1
net.core.wmem_max = 16777216





# 最大socket写buffer,可参考的优化值:873200

1
net.core.wmem_default = 8388608




# 表示发送套接字缓冲区大小的缺省值(以字节为单位)

1
net.core.rmem_default = 8388608




# 表示接收套接字缓冲区大小的缺省值(以字节为单位)使配置立即生效

1
/sbin/sysctl-P






页: [1]
查看完整版本: LNMP环境之Nginx/Tengine的源代码安装及优化