#!/bin/bash
#function:lnmp_auto_install
Source_Dir=/root/tools
host_ip=`ifconfig eth0|sed -n '2p'|sed -n 's@^.*dr:\(.*\) Bca.*$@\1@gp'`
ftp_ip=10.9.52.42
ftp_user=ftp_01
ftp_pass=ftp_2015
if [ ! -e $Source_Dir ];then
mkdir -p $Source_Dir
fi
install_nginx(){
echo -e "Start Install Nginx"
id nginx
if [ $? -ne 0 ];then
groupadd nginx
useradd -g nginx -s /sbin/nologin -M nginx
fi
yum install -y pcre-devel openssl-devel
cd $Source_Dir
tar xf nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
if [ $? -eq 0 ];then
echo -e "Finished Install Nginx"
fi
cd ..
cat > /etc/init.d/nginx <<eof
#!/bin/bash
#chkconfig: - 85 15
#description: nginx is a World Wide Web server. It is used to serve
path="/usr/local/nginx"
RETVAL=0
prog="nginx"
. /etc/init.d/functions
start(){
if [ ! -f \$path/logs/nginx.pid ]
 then
   [ -x \$path/sbin/nginx ] || exit 1
    \$path/sbin/nginx
   RETVAL=\$?
   if [ -f \$path/logs/nginx.pid ]
    then
      action "Nginx startup" /bin/true
     else
      action "Nginx startup" /bin/false
   fi
 else
   echo "Nginx is running."  
fi
return \$RETVAL
}
stop(){
if [ ! -f \$path/logs/nginx.pid ]
 then
   echo "Nginx is not running."
 else
   [ -x \$path/sbin/nginx ] || exit 1
   if [ -f \$path/logs/nginx.pid ]
    then
     kill \`cat \$path/logs/nginx.pid\`
     RETVAL=\$?
     sleep 2
   fi 
   if [ ! -f \$path/logs/nginx.pid ]
   then
   action "Nginx is stopped." /bin/true
    else
    action "Nginx is stopped." /bin/false
   fi
fi
return \$RETVAL
}
case "\$1" in
  start)
   start
   RETVAL=\$?
   ;;
  stop)
   stop
   RETVAL=\$?
   ;;
  restart)
   stop
   sleep 2
   start
   RETVAL=\$?
   ;;
  reload)
  \$path/sbin/nginx -s reload
   RETVAL=\$?
   ;;
  status)
  status \$prog
  RETVAL=\$?
  ;;
   *)
   echo "USAGE:\$0 {start|stop|restart|reload|status}"
esac
exit \$RETVAL 
eof
chmod +x /etc/init.d/nginx
/etc/init.d/nginx start
chkconfig nginx on
}
install_php(){
echo -e "Start Install Php-fpm"
yum -y install libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel openldap-devel libmcrypt-devel
cp /usr/lib64/libldap* /usr/lib/
cd $Source_Dir
tar xjf php-5.3.27.tar.bz2
cd php-5.3.27
./configure --prefix=/usr/local/php --with-config-file-path=/etc  --with-config-file-scan-dir=/etc/php.d --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libcurl --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath enable-shmop --enable-sysvsem --enable-inline-optimization   --with-curlwrappers --enable-mbregex --with-gettext --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc  --enable-zip --enable-soap --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
make && make install
if [ $? -eq 0 ];then
cp php.ini-production  /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig php-fpm on
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sed -i 's@user = nobody@user = nginx@g' /usr/local/php/etc/php-fpm.conf
sed -i 's@group = nobody@group = nginx@g' /usr/local/php/etc/php-fpm.conf
sed -i 's@pm.max_children = 5@pm.max_children = 50@g' /usr/local/php/etc/php-fpm.conf
sed -i 's@pm.start_servers = 2@pm.start_servers = 5@g' /usr/local/php/etc/php-fpm.conf
sed -i 's@pm.min_spare_servers = 1@pm.min_spare_servers = 2@g' /usr/local/php/etc/php-fpm.conf
sed -i 's@pm.max_spare_servers = 3@pm.max_spare_servers = 8@g' /usr/local/php/etc/php-fpm.conf
sed -i "s@listen = 127.0.0.1:9000@listen = ${host_ip}:9000@g" /usr/local/php/etc/php-fpm.conf
/etc/init.d/php-fpm start
fi
echo -e "Finished Install Php-fpm"
}
install_mysql(){
echo -e "Start Install Mysql"
yum -y install   ncurses-devel
id mysql
if [ $? -ne 0 ];then
groupadd mysql
useradd -g mysql -s /sbin/nologin -M mysql
fi
cd $Source_Dir
tar xf mysql-5.1.47.tar.gz
cd mysql-5.1.47
./configure --prefix=/usr/local/mysql --with-charset=utf8 --with-extra-charsets=all --enable-thread-safe-client --enable-assembler --with-readline --with-big-tables --with-plugins=all --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static
make && make install
cd ..
chown -R mysql:mysql /usr/local/mysql
mkdir -p /data/3306/data
cp /usr/local/mysql/share/mysql/my-medium.cnf /data/3306/my.cnf.ori
cat>/data/3306/my.cnf<<eof
[client]
port= 3306
socket= /tmp/mysql.sock
[mysqld]
port= 3306
socket= /data/3306/mysql.sock
basedir         = /usr/local/mysql
datadir         = /data/3306/data
pid-file        = /data/3307/mysql.pid
log-bin         = /data/3307/mysql-bin
skip-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=mysql-bin
binlog_format=mixed
server-id= 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
log_error=/data/3306/mysql_3306.err
pid-file=/data/3306/mysqld.pid
eof
cat>/data/3306/mysql<<eof
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd=""
CmdPath="/usr/local/mysql/bin"
mysql_sock="/data/\${port}/mysql.sock"
#startup function
function_start_mysql(){
  if [ ! -e "\$mysql_sock" ];then
     printf "Starting MySQL...\n"
     /bin/sh \${CmdPath}/mysqld_safe --defaults-file=/data/\${port}/my.cnf 2>&1 > /dev/null &
  
  else
    printf "MySQL is running...\n"
    exit
  fi
}
#stop function
function_stop_mysql(){
  if [ ! -e "\$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
  else 
    printf "Stoping MySQL...\n"
    \${CmdPath}/mysqladmin -u \${mysql_user} -p\${mysql_pwd} -S /data/\${port}/mysql.sock shutdown
  fi
}
#restart function
function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}
case \$1 in 
start)
     function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: /data/\${port}/mysql {start|stop|restart}\n"
;;
esac
eof
chmod 700 /data/3306/mysql
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/3306/data --user=mysql
chown -R mysql:mysql /data
/data/3306/mysql start
echo -e "Finished Install Mysql"
}
init_system(){
echo -e "Start System Initialize"
for service in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "network|sshd|crond|rsyslog"`
do
chkconfig $service off
done
/etc/init.d/iptables stop
sed -i "s@SELINUX=enforcing@SELINUX=disabled@g" /etc/selinux/config 
yum makecache
yum -y install ftp
cd ${Source_Dir}
ftp -i -n <<!
open ${ftp_ip} 21
user ${ftp_user} ${ftp_pass}
binary
cd tools/lnmp
mget * .
close 
bye
!
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
mv CentOS-Base.repo /etc/yum.repos.d
cd ~
ulimit -SHn 65535
echo "ulimit -SHn 65535" >> /etc/rc.local
cat >>/etc/security/limits.conf <<eof
*    hard   nofile  65535
eof
cat >> /etc/sysctl.conf << eof
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 1024 65535
eof
/sbin/sysctl -p
yum -y install gcc gcc-c++ make automake autoconf
echo -e "Finished System Initialize"
}
until
cat<<EOF
----------------------------------------
1.System Initialize
2.Install Nginx
3.Install Php-fpm
4.Install Mysql
5.Exit
----------------------------------------
EOF
read  -p "Please Enter Your Choice[1-5]:" num
test $num = 5
do
case $num in
1)
init_system
;;
2)
install_nginx
;;
3)
install_php
;;
4)
install_mysql
;;
*)
echo "Please Enter Your Choice:"
exit 1 
esac
done