#!/bin/bash
#auto install LANMP
#by author ale 
#2019-4-30 20:39:04
#Httpd define path variable
H_FILES=httpd-2.4.38.tar.gz
H_FILES_DIR=httpd-2.4.38
H_URL=http://mirrors.cnnic.cn/apache/httpd/
H_PREFIX=/usr/local/apache


#Mysql define path variable
M_FILES=mysql-5.5.20.tar.gz
M_FILES_DIR=mysql-5.5.20
M_PREFIX=/usr/local/mysql
M_YUM="cmake  ncurses-devel ncurses pcre pcre-devel"

#Php define path variable
P_FILES=php-5.6.10.tar.gz
P_FILES_DIR=php-5.6.10
P_URL=http://ftp.ntu.edu.tw/php/distributions/
P_PREFIX=/usr/local/php
P_YUM="gd   curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel"

#Nginx define path variable
N_FILES=nginx-1.12.2.tar.gz
N_FILES_DIR=nginx-1.12.2
N_URL=http://nginx.org/download/
N_PREFIX=/usr/local/nginx
N_YUM="openssl openssl-devel"

print_menu(){
    echo -e "\033[32mPlease Select Install Menu follow:\033[0m"
    echo -e "\033[32m1)编译安装Apache服务\033[0m"
    echo -e "\033[32m2)编译安装NGINX服务\033[0m"
    echo -e "\033[32m3)编译安装MYSQL服务\033[0m"
    echo -e "\033[32m4)编译安装PHP服务\033[0m"
    echo -e "\033[32m5)配置index.php并启动LAMP服务\033[0m"
    echo -e "\033[32m6)整合LNMP\033[0m"
    exit 
}

apache_install(){
#Install httpd web server
if [[ "$1" -eq "1" ]];then
    wget -c $H_URL/$H_FILES;tar -xzf $H_FILES;cd $H_FILES_DIR;./configure --prefix=$H_PREFIX --enable-so --enable-cgi --enable-rewrite --enable-module=most --enable-mpms-shared=all 
    if [ $? -eq  0 ];then
        make &&make install
        cp $H_PREFIX/bin/apachectl /etc/init.d/httpd
        chmod o+x /etc/init.d/httpd
    fi
fi
}

nginx_install(){
#Install nginx web server
if [[ "$1" -eq "2" ]];then
        yum install -y $N_YUM
        wget -c $N_URL/$N_FILES;tar -xzf $N_FILES;cd $N_FILES_DIR;useradd www;./configure --prefix=$N_PREFIX --user=www --group=w
ww --with-http_stub_status_module --with-http_ssl_module
        make && make install
fi
}


mysql_install(){
#Install mysql server
if [[ "$1" == "3" ]];then
    yum install -y $M_YUM
    tar -xf $M_FILES;cd $M_FILES_DIR;cmake  . -DCMAKE_INSTALL_PREFIX=$M_PREFIX \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DWITH_DEBUG=0
make -j4 && make install -j4
cp support-files/my-small.cnf  /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
useradd mysql;$M_PREFIX/scripts/mysql_install_db --basedir=$M_PREFIX --datadir=/data/mysql --user=mysql
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
fi
}


php_install(){
#Install php server
if [[ "$1" -eq "4" ]];then
    yum -y install $P_YUM
    wget -c $P_URL/$P_FILES;tar -xzf $P_FILES;cd $P_FILES_DIR;./configure --prefix=$P_PREFIX --enable-fpm  --with-pdo-mysql  --enable-debug --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir   --enable-mbstring --with-curl   --with-mysql=$M_PREFIX  --with-config-file-path=$P_PREFIX/etc --with-apxs2=$H_PREFIX/bin/apxs     
    if [ $? -eq 0 ];then
        make -j4 && make install -j4
        cp php.ini-development $P_PREFIX/etc/php.ini
        cp  $P_PREFIX/etc/php-fpm.conf.default  $P_PREFIX/etc/php-fpm.conf
        $M_PREFIX/sbin/php-fpm 
        cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
        chmod o+x /etc/init.d/php-fpm
        echo -e "\033[32mThe Php Server Install Success\033[0m"
    else
        echo -e "\033[32mThe Php Server Install Failed,Please check..\033[0m"
    fi
fi
}
    
lamp_config(){
if [[ "$1" = "5" ]];then
    sed -i '/DirectoryIndex/s/index.html/index.php index.html/g' $H_PREFIX/conf/httpd.conf 
    $H_PREFIX/bin/apachectl restart    
    echo "AddType     application/x-httpd-php .php" >>$H_PREFIX/conf/httpd.conf
    IP=`ifconfig ens33|grep "broadcast"|awk '{print $2}'|cut -d: -f2`
    echo "You can access http://$IP/"
    cat >$H_PREFIX/htdocs/index.php <<EOF
    <?php
    phpinfo();
    ?>
EOF
fi
}

lnmp_config(){
if [[ "$1" = "6" ]];then
    \cp nginx.conf.swp $N_PREFIX/conf/nginx.conf 
    cat >$N_PREFIX/html/index.php <<EOF
        <?php
        phpinfo();
        ?>
EOF
    $N_PREFIX/sbin/nginx
    /etc/init.d/php-fpm restart
    /etc/init.d/mysqld restart
    systemctl stop firewalld
    setenforce 0
fi
}

case $1 in

    1)
        apache_install $1
    ;;
    2)
        nginx_install $1
    ;;
    3)
        mysql_install $1
    ;;
    4)
        php_install $1
    ;;
    5)
        lamp_config $1
    ;;
    6)
        lnmp_config $1
    ;;
    *)
        print_menu
    ;;
esac
nginx.conf.swp 内容如下
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
         location  ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}