sysctl.conf 优化方案

在服务器安装了LNMP(Linux+Nginx+Mysql+php)环境之后Linux服务器参数必须做必要的优化,包括网络参数优化、文件数优化、内存优化等等。

 

1、网络参数优化:

 

这部分主要参照张宴大师 《Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[原创]》其中

 

 五、优化Linux内核参数的内容:

vi/etc/sysctl.conf #编辑sysctl.conf文件

#在/etc/sysctl.conf中添加如下内容

 

cat<<EOF>>/etc/sysctl.conf

net.ipv4.tcp_max_syn_backlog = 65536

net.core.netdev_max_backlog =  32768

net.core.somaxconn = 32768

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 2

net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_len = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_max_orphans = 3276800

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 120

net.ipv4.ip_local_port_range = 1024  65535

EOF

 

/sbin/sysctl -p

 

 

2、系统最大打开文件数优化

默认情况下,系统允许最大打开文件数为1024个,这对于访问量较大的web服务器来说远远不够用。系统最大打开文件数可以通过ulimit来修改和查询。

一般情况下,可以通过ulimit -n来查看目前系统允许的最大打开文件数,例如 :

ulimit -n

1024 #系统返回

如果我们要修改系统允许最大文件打开数,可以通过ulimit -SHn来做,例如要临时修改系统最大打开文件数为51201个,那么用如下命令:

ulimit -SHn 51201

每次重启系统时,该值会被自动还原,可以将上述命令写入 /etc/rc.local 中。如果想永久更改,可以按照如下修改:

vi /etc/security/limits.conf 

# add

* soft nofile 51201

* hard nofile 51201

 

 

系统最大打开文件数影响到的配置文件(以下必须设置为系统设置相一致,例如设置系统最大打开文件数为:51201):

A、Nginx配置文件nginx.conf的配置项:worker_rlimit_nofile、worker_connections、open_file_cache等,例如设置为:

  worker_rlimit_nofile 51201;

  events

  {

    use epoll;

    worker_connections 51201;

  }

  http

  {

    #其它参数略过

    open_file_cache max=51201inactive=20s; 

    open_file_cache_min_uses1; 

    open_file_cache_valid 30s;

    #其它参数略过

  }

B、FastCGI配置文件php-fpm.conf的配置项

   51201

 

3、系统共享内存优化:

 

系统共享内存大小主要影响到eaccelerator的设置:

eaccelerator.shm_size="32"

 

该项为eaccelerator可使用的共享内存大小(单位为MB)设置。

在Linux下,单个进程的最大共享内存使用量受/proc/sys/kernel/shmmax中设置的数字限制(单位为字节),例如Redhat4.7的shmmax默认值为33554432字节(33554432bytes/1024/1024=32MB)。

临时更改该值(例如修改为128MB=128x1024x1024=134217728字节):

 

echo 134217728 > /proc/sys/kernel/shmmax

echo 134217728 > /proc/sys/kernel/shmall

 

按照以上方法更改,在每次重启系统时,该值会被自动还原。如果想永久更改,可以修改/etc/sysctl.conf文件,设置:

#系统共享内存大小优化

kernel.shmmax = 134217728

 

完整的/etc/sysctl.conf修改添加内容如下:

 

#在/etc/sysctl.conf中添加如下内容

# 网络参数优化开始

 

net.ipv4.tcp_max_syn_backlog = 65536

net.core.netdev_max_backlog = 32768

net.core.somaxconn = 32768

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 2

net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1

#net.ipv4.tcp_tw_len = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30

#net.ipv4.tcp_keepalive_time = 120

net.ipv4.ip_local_port_range = 1024 65535

#网络参数优化结束

 

#系统共享内存大小优化开始

kernel.shmmax = 134217728

#系统共享内存大小优化结束

#添加内容结束

 

修改完成后运行以下命令使参数生效

/sbin/sysctl -p

以上,如有不足,欢迎大家指正和追加,谢谢!

 

 

 

高负载linux服务器的内核调优

vi /etc/sysctl.conf,修改内核参数:

cat<< EOF >>/etc/sysctl.conf

kernel.shmall = 268435456

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 1200

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.tcp_max_tw_buckets = 5000

net.ipv4.tcp_max_tw_buckets = 5000

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 5000 65000

net.ipv4.tcp_mem = 786432 1048576 1572864

net.core.wmem_max = 873200

net.core.rmem_max = 873200

net.ipv4.tcp_wmem = 8192 436600 873200

net.ipv4.tcp_rmem = 32768 436600 873200

net.core.somaxconn = 256

net.core.netdev_max_backlog = 1000

net.ipv4.tcp_max_syn_backlog = 2048

net.ipv4.tcp_retries2 = 5

net.ipv4.tcp_keepalive_time = 500

net.ipv4.tcp_keepalive_intvl = 30

net.ipv4.tcp_keepalive_probes = 3

net.ipv4.conf.lo.arp_ignore = 0

net.ipv4.conf.lo.arp_announce = 0

net.ipv4.conf.all.arp_ignore = 0

net.ipv4.conf.all.arp_announce = 0

EOF

 

 

几个解释:

net.ipv4.tcp_syncookies = 1

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

net.ipv4.tcp_tw_reuse = 1

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

net.ipv4.tcp_tw_recycle = 1

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

net.ipv4.tcp_fin_timeout = 30

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

net.ipv4.tcp_keepalive_time = 1200

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

net.ipv4.ip_local_port_range = 1024   65000

#表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。

net.ipv4.tcp_max_tw_buckets = 5000

#表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,

#TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改为5000。

#对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,

#但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死

 

 

 

完整的内核优化配置:

 

net.ipv4.tcp_max_tw_buckets = 6000

net.ipv4.tcp_sack = 1

net.ipv4.tcp_window_scaling = 1

net.ipv4.tcp_rmem = 4096 87380 4194304

net.ipv4.tcp_wmem = 4096 16384 4194304

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.core.netdev_max_backlog = 262144

net.core.somaxconn = 262144

net.ipv4.tcp_max_orphans = 3276800

net.ipv4.tcp_max_syn_backlog = 262144

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 1

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_fin_timeout = 1

net.ipv4.tcp_keepalive_time = 30

net.ipv4.ip_local_port_range = 1024 65000

 

 

 

~]# sysctl -p

net.ipv4.ip_forward = 0

net.ipv4.conf.default.rp_filter = 1

net.ipv4.conf.default.accept_source_route = 0

kernel.sysrq = 0

kernel.core_uses_pid = 1

net.ipv4.tcp_syncookies = 1

kernel.msgmnb = 65536

kernel.msgmax = 65536

kernel.shmmax = 4294967295

kernel.shmall = 268435456

kernel.sem = 250       32000   100     128

fs.file-max = 65535

net.ipv4.ip_local_port_range = 1024     65535

net.core.wmem_max = 262144

net.core.wmem_default = 262144

net.core.rmem_default = 262144

net.core.rmem_max = 262144

 

 

 

修改/etc/sysctl.conf, /sbin/sysctl -p立即生效

永久修改内核参数: echo 10″ >/proc/sys/net/ipv4/tcp_fin_timeout

 

各种参数的意思:http://ipsysctl-tutorial.frozentux.net/ipsysctl-tutorial.html

还参考了:http://trac.lighttpd.net/trac/wiki/Docs%3APerformance#platform-specific-notes

 

net.ipv4.tcp_fin_timeout = 10

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

#tcp port range

net.ipv4.ip_local_port_range = 4096 65000

#tcp send buffer. min, default , and max

net.ipv4.tcp_wmem = 4096 65536 16777216

#tcp reservie buffer.

#net.ipv4.tcp_rmem = 4096 65536 16777216

 

分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS