像windows安装服务器套件一样在linux下安装你的服务器套件

前端之家收集整理的这篇文章主要介绍了像windows安装服务器套件一样在linux下安装你的服务器套件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

#!/bin/sh
#================================================================================
#name           :   EazyLamp
#version        :   0.1a
#description    :   Easy-to-use on click setup/transfer tool for lamp server
#                   configuration under linux,as easy as wamp server.
#author         :   latel ([email protected])
#github         :   https://github.com/latel/EasyLamp
#liencense      :   GPL
#date           :   2013/12/20
#------------------------------------------------------------
#history        :   2013/12/20    latel   first release
#================================================================================

BASE_DIR=$PWD/${0%/*}/../
cd $BASE_DIR
BASE_DIR=$PWD
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:$BASE_DIR/install/functions
TTY=`ps|grep $$|awk '{print $2}'`
USER=`who|grep ${TTY}|awk '{print $1}'`


#adjust your settings as follows which will be refered during installation
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#leave values to "do_not_install" on the item you don't wanted
#apache
apache_version="2.4.7"
#PHP
PHP_version="5.5.7"
#MysqL
MysqL_version="5.6.15"
MysqL_password="5070474849"

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#this script shall only run in root priviledge
get_root(){
    if [[ $EUID -ne 0 ]];then
        echo "This script must be run as root" 1>&2
        exit 1
    fi
}

#detect os type
detect_os(){
    if uname -a | grep -i "arch" >/dev/null 2>&1;then
        echo "arch"
    elif uname -a | grep -i "ubuntu|debian">/dev/null 2>&1;then
        echo "debian"
    elif uname -a | grep -i "centos|redhat">/dev/null 2>&1;then
        echo "redhat"
    elif cat /proc/version | grep -i "arch">/dev/null 2>&1;then
        echo "arch"
    elif cat /proc/version | grep -i "ubuntu|debian">/dev/null 2>&1;then
        echo "debian"
    elif cat /proc/version | grep -i "centos|redhat">/dev/null 2>&1;then
        echo "redhat"
    else
        echo "generic"
    fi
}

#check whether a command is already available in system
check_command_exists(){
    local command=$1
    local stop=$2
    if hash $1 2>/dev/null;then
        echo "0"
    else
        echo "1"
        if [ $2 == "1" ];then
            echo 'building tools $1 not installed. please do it yourself'
            exit 1
        fi
    fi
}

lowcase(){
    word=$1
    echo $word | tr '[A-Z]' '[a-z]'
}

#choose yes or no
confirm(){
    local prompt=$1
    local yaction=$2
    local naction=$3
    while true; do
        read -p "$prompt(y/n)?: " choice
        choice=`lowcase $choice`
        case $choice in
            y ) eval "$yaction";return 1;break;;
            n ) eval "$naction";return 0;break;;
            * ) echo "wrong input,please type y or n only."
        esac
    done
}

#detect source code's compress format
#execute accordingly extract command
extract_source(){
    #currently,only tar.gz and tar.bz2 format are supported
    local zip_name=`lowcase $1`
    local zip_destination=$2
    cd $BASE_DIR/install
    if [ -f packages/${zip_name}.tar.gz ];then
        tar zxvf packages/${zip_name}.tar.gz -C $zip_destination
        return 0
    elif [ -f packages/${zip_name}.tar.bz2 ];then
        tar jxvf packages/${zip_name}.tar.bz2 -C $zip_destination
        return 0
    else
        echo "!!no file is available for source ${zip_name},installation aborted."
        exit 1
    fi
}

#install building tools
install_tools(){
    if [ "`detect_os`" == "arch" ];then
        pacman -Syu
        pacman -S gcc g++ make cmake wget perl curl openssl build-essential
    elif [ "`detect_os`" == "debian" ];then
        apt-get update
        apt-get install gcc g++ make cmake wget perl curl openssl build-essential
    elif [ "`detect_os`" == "redhat" ];then
        yum -y gcc g++ make cmake wget perl curl openssl build-essential
    fi

    check_command_exists "gcc" "1"
    check_command_exists "g++" "1"
    check_command_exists "make" "1"
    check_command_exists "cmake" "1"
    check_command_exists "wget" "1"
    check_command_exists "perl" "1"
    check_command_exists "curl" "1"
}

#install dialog
install_dialog(){
    #prepare files
    tar xvf install/packages/dialog.tar.gz -C install/temp/
    #install
    cd install/temp/dialog-1.2-20130928
    ./configure
    make
    make install
    cd $BASE_DIR
}

#install apache
install_apache(){
    cd $BASE_DIR
    #check prevIoUs installation
    if [ -d apache* ];then
        confirm "You have installed `ls|grep apache` already,uninstall first" "rm -r apache*" "apache_version=`ls|grep apache|grep -oE '((([0-9]){1,}.){2}[0-9]{1,}){1}'` ; return 0"
        if [ $? != "1" ];then
            return 0
        fi
    fi
    #install dependencies

    #make directory
    cd $BASE_DIR
    [[ -d apache-${apache_version} ]] || mkdir apache-${apache_version}
    [[ -d www ]] || mkdir www
    [[ -d logs ]] || mkdir logs
    [[ -d conf ]] || mkdir conf
    #prepare files
    if [ -f www/index.PHP ];then
        echo "htdocs files existed,ignore."
    else
        touch www/index.PHP
        echo "<?PHP echo PHPinfo(); ?>" >> www/index.PHP
    fi
    extract_source "httpd-${apache_version}" "${BASE_DIR}/install/temp/"
    cd $BASE_DIR
    #install
    cd install/temp/httpd-${apache_version}
    ./configure\
        --prefix=$BASE_DIR/apache-${apache_version}\
        --logfiledir=$BASE_DIR/logs\
        --enable-so
        #--bindir=/usr/bin\
        #--sbindir=/usr/sbin\
        #--libdir=/usr/lib/httpd/lib\
        #--libexecdir=/usr/lib/httpd/modules\
        #--installbuilddir=/usr/lib/httpd/build\
        #--htmldir=$BASE_DIR/www\
    make
    make install

    #configuration
    cd $BASE_DIR
    groupadd httpd >/dev/null 2>&1 #create group and user if not exitsts
    useradd -s /bin/false -g httpd httpd >/dev/null 2>&1
    cd apache-${apache_version}
    mv htdocs/index.html $BASE_DIR/www/index.html
    cp conf/httpd.conf conf/httpd.conf.bak #backup original conf file
    sed -i -e "s/User daemon/User httpd/" -e "s/Group daemon/Group httpd/" conf/httpd.conf
    sed -i -e "s/apache-${apache_version}\/htdocs/www/" conf/httpd.conf
    sed -i -e "s/#ServerName www.example.com:80/ServerName www.example.com:80/" conf/httpd.conf
    sed -i -e "s/ErrorLog \"logs\/error_log\"/ErrorLog \"${BASE_DIR//\//\\/}\/logs\/httpd_error_log\"/" conf/httpd.conf
    sed -i -e "s/CustomLog \"logs\/access_log\" common/CustomLog \"${BASE_DIR//\//\\/}\/logs\/httpd_access_log\" common/" conf/httpd.conf
    #make files for later-reading
    cd $BASE_DIR
    touch {logs/httpd_access_log,logs/httpd_error_log}

    #cleanup
    cp apache-${apache_version}/bin/httpd apache-${apache_version}/bin/httpd.bak
    strip apache-${apache_version}/bin/httpd
}

#install PHP
install_PHP(){
    cd $BASE_DIR
    #check prevIoUs installation
    if [ -d PHP* ];then
        confirm "You'v installed `ls|grep PHP` already,uninstall first" "rm -r PHP*" "PHP_version=`ls|grep PHP|grep -oE '((([0-9]){1,}){1}'` ; return 0"
        if [ $? != "1" ];then
            return 0
        fi
    fi
    #install dependencies

    #make directory
    [[ -d PHP-${PHP_version} ]] || mkdir PHP-${PHP_version}
    [[ -d logs ]] || mkdir logs
    [[ -d conf ]] || mkdir conf
    #prepare files
    extract_source "PHP-${PHP_version}" "${BASE_DIR}/install/temp/"
    cd $BASE_DIR
    #install

    cd install/temp/PHP-${PHP_version}
    ./configure\
        --prefix=$BASE_DIR/PHP-${PHP_version}\
        --with-apxs2=$BASE_DIR/apache-${apache_version}/bin/apxs\
        --with-config-file-path=$BASE_DIR/conf\
        --with-MysqL\
        --with-pdo-MysqL\
        --enable-pdo\
        --with-openssl\
        --with-zlib\
        --with-curl\
        --with-gd\
        --with-mcrypt #PHPmyadmin need it
    make
    make install

    #configuration
    cd $BASE_DIR
    mv PHP/etc PHP-${PHP_version}/
    rm -r PHP
    cp install/temp/PHP-${PHP_version}/PHP.ini-development conf/PHP.ini
    sed -i -e "s/;date.timezone =/date.timezone = Asia\/Shanghai/" conf/PHP.ini
    sed -i -e "s/;extension=PHP_MysqL.dll/extension=PHP_MysqL.dll/" conf/PHP.ini
    sed -i -e "s/;extension=PHP_pdo_MysqL.dll/extension=PHP_pdo_MysqL.dll/" conf/PHP.ini
    sed -i -e "s/MysqL.default_socket =/MysqL.default_socket =${BASE_DIR//\//\\/}\/MysqL-${MysqL_version}\/MysqL.sock/" conf/PHP.ini
    sed -i -e "s/pdo_MysqL.default_socket=/pdo_MysqL.default_socket=${BASE_DIR//\//\\/}\/MysqL-${MysqL_version}\/MysqL.sock/" conf/PHP.ini
    cp install/resource/PHP5_module.conf apache-${apache_version}/conf/extra/PHP5_module.conf
    sed -i -e "s/PHPIniDir/PHPIniDir '${BASE_DIR//\//\\/}\/conf\/PHP.ini'/" apache-${apache_version}/conf/extra/PHP5_module.conf
    echo "Include conf/extra/PHP5_module.conf" >> apache-${apache_version}/conf/httpd.conf
    #do stuff for apache
    cp apache-${apache_version}/conf/httpd.conf conf/httpd.conf
}

#install MysqL
install_MysqL(){
    cd $BASE_DIR
    #check prevIoUs installation
    if [ -d MysqL* ];then
        confirm "You'v installed `ls|grep MysqL` already,uninstall first" "rm -r MysqL*" "MysqL_version=`ls|grep MysqL|grep -oE '((([0-9]){1,}){1}'` ; return 0"
        if [ $? != "1" ];then
            return 0
        fi
    fi
    #install dependencies

    #make directory
    cd $BASE_DIR
    [[ -d MysqL-${MysqL_version}/data ]] || mkdir -p MysqL-${MysqL_version}/data
    [[ -d logs ]] || mkdir logs
    [[ -d conf ]] || mkdir conf

    #prepare files
    extract_source "MysqL-${MysqL_version}" "${BASE_DIR}/install/temp/"
    cd $BASE_DIR
    #install
    cd install/temp/MysqL-${MysqL_version}
    cmake \
        -DCMAKE_INSTALL_PREFIX=$BASE_DIR/MysqL-${MysqL_version} \
        -DMysqL_DATADIR=$BASE_DIR/MysqL-${MysqL_version}/data \
        -DMysqL_UNIX_ADDR=$BASE_DIR/MysqL-${MysqL_version}/MysqL.sock \
        -DEFAULT_CHARSET=utf8 \
        -DEFAULT_COLLATION=utf8_general_ci
        #add a user line for coomon user to use MysqL
    #./configure\
    #    --prefix=$BASE_DIR/MysqL-${MysqL_version}
    make
    make install

    #configuration
    cd $BASE_DIR
    groupadd MysqL #create group and user if not exitstsapache-${apache_version}
    useradd -s /bin/false -g MysqL MysqL
    MysqL-${MysqL_version}/scripts/MysqL_install_db --basedir=$BASE_DIR/MysqL-${MysqL_version} --datadir=$BASE_DIR/MysqL-${MysqL_version}/data --user=MysqL
    cp install/temp/MysqL-${MysqL_version}/support-files/MysqL.server MysqL-${MysqL_version}/bin/MysqL.server
    chmod +x MysqL-${MysqL_version}/bin/MysqL.server
    cp install/temp/MysqL-${MysqL_version}/support-files/my-default.cnf MysqL-${MysqL_version}/my.cnf
    cd MysqL-${MysqL_version}
    echo "basedir = $BASE_DIR/MysqL-${MysqL_version}/" >> my.cnf
    echo "datadir = $BASE_DIR/MysqL-${MysqL_version}/data" >> my.cnf
    echo "port = 3306" >> my.cnf
    echo "socket = $BASE_DIR/MysqL-${MysqL_version}/MysqL.sock" >> my.cnf
    echo "log-error = $BASE_DIR/logs/MysqL_error.log" >> my.cnf
    echo "pid-file = $BASE_DIR/MysqL-${MysqL_version}/MysqL.pid" >> my.cnf
    echo "user = MysqL" >> my.cnf
    cd $BASE_DIR
    #make files for later-reading
    touch logs/MysqL_error.log
    #adjust permissions
    chown -R MysqL.MysqL MysqL-${MysqL_version}
}

gen_scripts(){
    cd $BASE_DIR
    [[ -f ezlamp-st ]] && rm ezlamp-st
    [[ -f ezlamp-ki ]] && rm ezlamp-ki
    [[ -f ezlamp-ki ]] && rm greenfy
    cp install/resource/ezlamp-st ezlamp-st
    cp install/resource/ezlamp-ki ezlamp-ki
    cp install/resource/greenfy greenfy
    chmod +x ezlamp-st ezlamp-ki greenfy
    chown $USER ez* green*
}

run(){
    cd $BASE_DIR
    apache-${apache_version}/bin/apachectl -k start -f ${BASE_DIR}/conf/httpd.conf #start apache
    MysqL-${MysqL_version}/bin/MysqL.server start --explicit_defaults_for_timestamp & #start MysqL daemon
}

#doing some post configuration work
post_configuration(){
    cd $BASE_DIR
    MysqL-${MysqL_version}/bin/MysqL_secure_installation
}

eazylamp(){
    echo
    echo
    echo "#############################################################################"
    echo
    echo "You are welcome to use this script to deploy your lamp,hope you like it."
    echo "If you have any question,please submit your issues to following address"
    echo "https://github.com/latel/EasyLamp"
    echo
    echo "#############################################################################"
    echo

    #user confirmation
    read -p "Are you sure you want to install lamp in $BASE_DIR(y/n)?" confirm
    cd $BASE_DIR
    if [ $confirm = y ];then
        get_root
        #install_tools
        touch /tmp/eazylamp_log #create file to log install information
        chattr +a /tmp/eazylamp_log #not allowed to delete,only add is alllowed
        detect_os
        if [ "`check_command_exists "dialog"`" == "1" ];then
            read -p "Dialog not detected,which can supply great installation experience,install(y/n)?: " confirm
            if [ $confirm == y ];then
                install_dialog
                [[ $? == 0 ]] && has_dialog = 1
            fi
        fi
        [[ -d install/temp ]] || mkdir install/temp
        echo "[1/7]installing apache..."
        install_apache
        echo "*apache2 installed"
        echo "[2/7]installing PHP..."
        install_PHP
        echo "*PHP5 installed"
        echo "[3/7]installing MysqL..."
        install_MysqL
        echo "*MysqL installed"
        cd $BASE_DIR
        echo "[4/7]removing tempotary files"
        #rm -r install/temp
        echo "[5/7]do some peformance enhancements,please wait..."
        chown -R $USER www
        echo "[6/7]generating maintain scripts..."
        gen_scripts
        echo
        echo "install complete"
        echo "#======================================"
        echo "# apache: ${apache_version}"
        echo "# PHP   : ${PHP_version}"
        echo "# MysqL : ${MysqL_version}"
        echo "#======================================"
        echo
        echo "[7/7]All done.  (。・ω・)ノ"
        echo
        echo "you may now delete the install folder now  "
        run
        echo "http://localhost/"
    else
        echo "please manually move your folder to your desire position"
    fi
}
eazylamp

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的Shell相关文章