#!/bin/bash

M_FILES=mysql-5.5.48.tar.gz
M_DIR=mysql-5.5.48
M_PRE=/usr/local/mysql
M_URL=http://download.softagency.net/MySQL/Downloads/MySQL-5.5/mysql-5.5.48.tar.gz


P_FILES=php-5.4.22.tar.gz
P_DIR=php-5.4.22
P_PRE=/usr/local/php
P_URL=http://mirrors.sohu.com/php/php-5.4.22.tar.gz

N_FILES=nginx-1.4.7.tar.gz
N_DIR=nginx-1.4.7
N_PRE=/usr/local/nginx
N_URL=http://nginx.org/download/nginx-1.4.7.tar.gz

MULU=`pwd`

function mysql_install() {
yum install  gcc-c++ cmake ncurses-devel bison perl wget -y
wget $M_URL
useradd  -s  /sbin/nologin  mysql 
mkdir  -p  /data/mysql
chown  -R  mysql.mysql  /data/mysql
tar -zxvf $M_FILES;cd $M_DIR
cmake  -DCMAKE_INSTALL_PREFIX=$M_PRE -DMYSQL_DATADIR=/data/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1  -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_TCP_PORT=3306  -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1  -DEXTRA_CHARSETS=all  -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_USER=mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock
        make -j2 && make install
  if [ $? -eq 0 ];then
    echo "the mysql install is successful"
  else
    echo "the mysql install is failed"
    exit
  fi
}


function php_install() {
yum  install  -y libxml2-devel  libcurl-devel  openjpeg-devel  libjpeg-turbo-devel libpng-devel  freetype-devel
wget $P_URL
tar -zxvf $P_FILES ;cd $P_DIR
./configure --prefix=/usr/local/php  --enable-fpm --enable-debug --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-mbstring --with-curl --with-mysql=/usr/local/mysql
make  &&   make install
  if [ $? -eq 0 ];then
	echo "the php install is successful"
  else
	echo "the php install is failed"
	exit
  fi
}


function nginx_install() {
yum install  pcre-devel openssl-devel  -y
wget $N_URL
tar -zxvf $N_FILES ;cd $N_DIR
useradd -s /sbin/nologing   -M  www
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make  &&  make  install
  if [ $? -eq 0 ];then
	echo "the nginx install is successful"
  else
	echo "the nginx install is failed"
	exit
  fi
}


function lnmp_zh() {
cd $MULU
cp  $M_DIR/support-files/my-medium.cnf  /etc/my.cnf
cp  $M_DIR/support-files/mysql.server  /etc/init.d/mysqld
chmod  755  /etc/init.d/mysqld
/usr/local/mysql/scripts/mysql_install_db  --basedir=/usr/local/mysql --datadir=/data/mysql  --user=mysql
/etc/init.d/mysqld  start
chkconfig --add  mysqld
echo "PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
source  /etc/profile

cp $P_DIR/sapi/fpm/php-fpm  /etc/init.d/php-fpm
cp /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf
cp $P_DIR/php.ini-production  /usr/local/php/lib/php.ini	
/etc/init.d/php-fpm  

sed -i  's@index  index.html index.htm@index  index.php index.html@' /usr/local/nginx/conf/nginx.conf
sed -i '55a location ~ \.php$ {' /usr/local/nginx/conf/nginx.conf
sed -i '56a root     html;' /usr/local/nginx/conf/nginx.conf
sed -i '57a fastcgi_pass   127.0.0.1:9000;' /usr/local/nginx/conf/nginx.conf
sed -i '58a fastcgi_index  index.php;' /usr/local/nginx/conf/nginx.conf
sed -i '59a fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;' /usr/local/nginx/conf/nginx.conf
sed -i '60a include        fastcgi_params;' /usr/local/nginx/conf/nginx.conf
sed -i '61a }' /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx
}

cat <