#!/bin/bash
#Filename:PHP-v2.0.sh
#Description: web Installation Script
#Author: shuhui.qiu  by 2015
# Version: 2.0



# EXPORT PATH
export PATH=/bin:/sbin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

pwd="/data/PKG"
PREFIX="/data/server"
cpu_count=$(cat /proc/cpuinfo |grep -c processor)
mysqlrootpwd="123456"


# Check Run user
if [ $(id -u) != "0" ]; then
	echo "Error: Please use root role to install me!"
	exit 1;
	yum -y install gcc gcc-c++ automake lrzsz ftp;
fi
	
function yum_repo() {

	cd /etc/yum.repos.d/;
	mv CentOS-* backup/
	wget -c http://mirrors.163.com/.help/CentOS6-Base-163.repo
	yum clean all;yum makecache;yum update -y
}
	
function set_timezone() {
   	# Set timzone[shanghai]
	rm -rf /etc/localtime
	ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 }
 
function selinux() {
	sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux 
	setenforce 0
 }
 
 function firewall {
	# Flush rules
	iptables -F INPUT
	iptables -F FORWARD
	iptables -F OUTPUT
	iptables -F POSTROUTING -t nat
	iptables -F PREROUTING -t nat

	# Setting default rules
	iptables -P INPUT DROP
	iptables -A INPUT -i lo -j ACCEPT
	iptables -A OUTPUT -o lo -j ACCEPT

	# Firewall Rules
	iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
	

	# Openport Rules
	for tcp in 22 3306;
	do
		iptables -A INPUT -p tcp --dport ${tcp} -j ACCEPT;
	done

	# ICMP Rules
	iptables -A INPUT -p icmp -m limit --limit 3/s -j ACCEPT
	iptables -A INPUT -p icmp -j DROP

	/sbin/service iptables save;/sbin/service iptables restart
	chconfig iptables on
 }
 
function set_ulimit() {
	
	cat >> /etc/security/limits.conf <<ENDF
	
*    soft    nofile    51200
*    hard    nofile    51200
ENDF
} 


function SYS_CTL() {
	# Set sys_int
	cat >> /etc/sysctl.conf <<ENDF
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.ip_local_port_range = 1024  65535
# Disable SSR
net.ipv4.tcp_slow_start_after_idle=0
fs.file-max=51200
# Set keepalive
net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_intvl = 20
net.ipv4.tcp_keepalive_probes = 3
ENDF
	sysctl -p

}

function  sys_service() {

	service iptables stop;chkconfig iptables off;
	# Off sys_service
	for service in `chkconfig --list |grep 3:on|awk '{ print $1}'`
	do
		chkconfig ${service} off
	done
	 
	for service in crond network rsyslog sshd
	do
	   chkconfig ${service} on
	done
 }
 
 function utpdate() {
	# Set
	yum -y install ntpdate;
	/usr/sbin/ntpdate ntp.api.bz
	echo "* */2 * * * root /usr/sbin/ntpdate ntp.api.bz 1> /dev/null 2>&1" >> /var/spool/cron/root
	clock -w 
 }
 
function sshd_config() {
	sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
	sed -i 's/UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
	#sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
	sed -i 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
	/etc/init.d/sshd restart
}

function backup() {
	echo 'function backup() {'> /etc/profile.d/backup.sh
	echo 'newname=$1.$(date +%Y%m%d.%H%m.bak);'>> /etc/profile.d/backup.sh
	echo 'mv $1 $newname;'>> /etc/profile.d/backup.sh
	echo 'echo "Backed up $1 to $newname.";'>> /etc/profile.d/backup.sh
	echo 'cp -p $newname $1;'>> /etc/profile.d/backup.sh
	echo '}'>> /etc/profile.d/backup.sh
	chmod +x /etc/profile.d/backup.sh
	source /etc/profile.d/backup.sh

}


function log_formart(){
	echo -en "\n\nexport HISTFILESIZE=10000\nexport HISTSIZE=10000\nexport PROMPT_COMMAND=\"history -a\"\nexport HISTTIMEFORMAT=\"\$(whoami) %F %T \"" >> /etc/profile
}


function mysql55() {
	
	# Create MySQL User
	id mysql>/dev/null 2>&1
	if [ $? -eq "1" ];then  groupadd mysql;useradd -g mysql mysql -s /bin/false;fi
	mkdir ${PREFIX}/mysql/data -pv;chown mysql.mysql ${PREFIX}/mysql/data;
	
	cd ${pwd}
	
	for m in wget gcc gcc-c++ make cmake ncurses-devel libxml2-devel libtool-ltdl-devel autoconf bison zlib-devel;
	do
		yum -y install ${m};
	done
	
	wget -c http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.43.tar.gz;
	tar zxvf mysql-5.5.43.tar.gz;cd mysql-5.5.43;
	cmake -DCMAKE_INSTALL_PREFIX=${PREFIX}/mysql \
	-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
	-DDEFAULT_CHARSET=utf8\
	-DEXTRA_CHARSETS=all \
	-DDEFAULT_COLLATION=gbk_chinese_ci
	-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \
	-DWITH_MYISAM_STORAGE_ENGINE=1 \
	-DWITH_INNOBASE_STORAGE_ENGINE=1 \
	-DWITH_MEMORY_STORAGE_ENGINE=1 \
	-DWITH_READLINE=1 \
	-DENABLED_LOCAL_INFILE=1 \
	-DMYSQL_DATADIR=${PREFIX}/mysql/data \
	-DMYSQL_USER=mysql \
	-DMYSQL_TCP_PORT=3306 \
	-DSYSCONFDIR=/etc \
	-DINSTALL_SHAREDIR=share
	make -j ${cpu_count};make install
	
	# Create sys_service
	cp support-files/mysql.server /etc/init.d/mysqld
	chmod 755 /etc/init.d/mysqld
	chown -R mysql.mysql ${PREFIX}/mysql

	# Init
	${PREFIX}/mysql/scripts/mysql_install_db --user=mysql --datadir=${PREFIX}/mysql/data --basedir=${PREFIX}/mysql/ --defaults-file=/etc/my.cnf
	
	
	echo "${PREFIX}/mysql/lib/">>/etc/ld.so.conf;/sbin/ldconfig
	
	wget http://down.vqiu.cn/conf/Linux_conf/mysql/my.cnf -O /etc/my.cnf
	
	# Set ENV.
	echo "export PATH=\$PATH:${PREFIX}/mysql/bin">/etc/profile.d/mysql.sh
	chmod +x /etc/profile.d/mysql.sh;source /etc/profile.d/mysql.sh

	
	# Start MySQL
	/etc/init.d/mysqld start
	
	${PREFIX}/mysql/bin/mysqladmin -u root password ${mysqlrootpwd};
	${PREFIX}/mysql/bin/mysql -uroot password${mysqlrootpwd} -e "drop database test;DROP USER ''@'%';flush privileges;"
	
	chkconfig mysqld on
}


function SYS_INIT(){
	if [ ! -e /tmp/sys_init.if ];
	then
		yum_repo
		set_timezone
		selinux
		set_ulimit
		firewall
		sys_service
		sshd_config
		SYS_CTL
		utpdate
		backup
		log_formart
		touch /tmp/sys_init.if > /dev/null 2>&1;
		fi
}


SYS_INIT
mysql56



