#!/bin/bash
# Filename: kernel_optimize.sh
# Descript: kernel_optimize for web server

FILE=$(echo 'shuhui' | md5sum | cut -b1-25)

if [[ ! -e /tmp/${FILE} ]]
then 
	cat >> /etc/sysctl.conf <<EOF 

fs.file-max = 1048576
#fs.nr_open = 25000000

net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024  65535
# Disable SSR[Slow-Start Restart]
net.ipv4.tcp_slow_start_after_idle = 0

net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1

# Disable IPV6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

vm.vfs_cache_pressure = 500 

# see details in https://help.aliyun.com/knowledge_detail/39428.html
net.ipv4.conf.all.rp_filter=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_announce=2


net.ipv4.tcp_rmem = 8192 87380 1048576
net.ipv4.tcp_wmem = 8192 87380 1048576

net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.core.somaxconn =4096
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_fin_timeout = 30

#net.netfilter.nf_conntrack_buckets = 655350
#net.netfilter.nf_conntrack_tcp_timeout_established = 1200

net.ipv4.conf.lo.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0

# Forward
#net.ipv4.ip_forward=1
#net.ipv6.conf.all.forwarding=1

# Do not accept ICMP redirects
#net.ipv4.conf.all.accept_redirects = 0
#net.ipv6.conf.all.accept_redirects = 0

# Accept ICMP redirects only for gateways listed in our default
# gateway list (enabled by default)
#net.ipv4.conf.all.secure_redirects = 1

# Do not send ICMP redirects (we are not a router)
#net.ipv4.conf.all.send_redirects = 0

# Do not accept IP source route packets (we are not a router)
#net.ipv4.conf.all.accept_source_route = 0
EOF
	cat > /etc/security/limits.d/optimize.conf <<ENDF
	
*    soft    nofile    1000000
*    hard    nofile    1000000
ENDF

	touch /tmp/${FILE}
	source /etc/security/limits.conf
	/sbin/sysctl -p
fi
