【转载】自动化之LAMP部署

  • ###################################auto_lamp.sh##########################################
  • #!/bin/bash
  • # -*- coding: utf-8 -*-
  • #-------------------------------------------------------------------------------
  • # Name:        auto_lamp.sh
  • # Purpose:
  • #
  • # Author:      hjjie2006.cublog.cn
  • # Mail:        hjjie2006@126.com
  • # Created:     01/10/2011
  • # Copyright:(c) hjjie 2011
  • #
  • #-------------------------------------------------------------------------------
  • # -*- coding: utf-8 -*-
  • #设定所需的安装包
  • #安装前请准备以下源码包  对于GD库的安装则调用另一个脚本 因此将gdsetup.sh与包放同一目录当中
  • MYSQL_PKG='mysql-5.1.36.tar.gz'
  • HTTPD_PKG='httpd-2.2.11.tar.gz'
  • PHP_PKG='php-5.2.10.tar.gz'
  • ##下面这部分是GD库所需的源码包
  • ##ZLIB_PKG='zlib-1.2.3.tar.gz'
  • ##PNG_PKG='libpng-1.2.26.tar.gz'
  • ##FREETYPE_PKG='freetype-2.3.5.tar.gz'
  • ##JPEG_PKG='jpegsrc.v6b.tar.gz'
  • ##FRONTCONF_PKG='fontconfig-2.4.2.tar.gz'
  • ##GD_PKG='gd-2.0.35.tar.gz'
  • ###对于GD安装时报错时安装
  • ##GETTEXT_PKG='gettext-0.17.tar.gz'
  • ##LIBXML='libxml2-2.6.31.tar.gz'
  • ##提供的http 地址 用于下载源码包
  • ##HTTP_DOWNLOAD=192.168.166.173/install
  • #-------------------------------------------------------------------------------
  • #源码包存放在当前脚本所在位置
  • PKG_PATH=$(pwd)
  • #GD库安装脚本
  • GDLIB_SETUP=$PKG_PATH/gdsetup.sh
  • #defined Installation path
  • #mysql Installation path
  • MYSQL_PATH=/usr/local/mysql
  • #mysql data Installation path
  • MYSQL_DATADIR=${MYSQL_PATH}/var
  • #apache Installation path
  • HTTPD_PATH=/usr/local/apache2
  • #gdlib  Installation path
  • GDLIB_PATH=/usr/local/libgd #此处如果时需要将GD库安装脚本也一并修改
  • #php  Installation path
  • PHP_PATH=/usr/local/php #如果GD库中各相关库路径有改动 需要将php编译参数中也一并修改
  • #mysql 运行用户
  • MYSQL_USER=mysql
  • MYSQL_GROUP=mysql
  • #-------------------------------------------------------------------------------
  • #去除tar.gz
  • MYSQL_PKG_=$(echo $MYSQL_PKG |sed 's/.tar.gz//')
  • HTTPD_PKG_=$(echo $HTTPD_PKG |sed 's/.tar.gz//')
  • PHP_PKG_=$(echo $PHP_PKG |sed 's/.tar.gz//')
  • cat << EOF
  • #############################################
  •   1.$MYSQL_PKG_ install
  •   2.$HTTPD_PKG_ install
  •   3.GD-lib install
  •   4.$PHP_PKG_ install
  •   5.LAMP Environment install
  • #############################################
  • EOF
  • #Color select
  • result_output(){
  •     if [ $1 = failed   ];then  #红色 失败  #[FAILED]
  •         if [ -z "$2" ];then
  •             echo -e "[$1]\033[32;31;1m [[FAILED]] \033[39;49;0m"
  •         else
  •             echo -e "[$1]\033[32;31;1m [[$2]] \033[39;49;0m"
  •         fi
  •     elif    [ $1 = ok ];then        #绿色 成功  #[OK  ]
  •         if [ -z "$2" ];then
  •             echo -e "[$1]\033[32;49;1m [[OK]] \033[39;49;0m"
  •         else
  •             echo -e "[$1]\033[32;49;1m [[$2]] \033[39;49;0m"
  •         fi
  •     elif    [ $1 = warn ];then      #蓝色背景 红色前景 用于警告 [warn]
  •         if [ -z "$2" ];then
  •             echo -e "[$1]\033[44;31;1m [[WARNING]] \033[0m"
  •         else
  •             echo -e "[$1]\033[44;31;1m [[$2]] \033[0m"
  •         fi
  •     fi
  • }
  • #退出状态检查
  • exec_check(){
  •     if [ $? != 0 ];then
  •         result_output failed
  •         exit 1
  •     else
  •         result_output ok
  •         sleep 1
  •     fi
  • }
  • #检查相关RPM是否安装 remove
  • rpm_exist_check(){
  • rpm_is_exist=$(rpm -qa | grep $rpm_info)
  •     if [ ! -z "$rpm_is_exist" ];then
  •         yum -y remove  mysql-server
  •     fi
  • }
  • #检查文件是否存在/不存在进行下载
  • file_exist_check(){
  •     if [ -f $file_name ];then
  •         result_output ok "$file_name  is already exist"
  •     else
  •         result_output failed "$file_name is not  exist"
  •         result_output warn "Please Download $file_name ..."
  •         exit 1
  •     fi
  • }
  • #检查是否已经安装
  • installpath_exist_check(){
  •     if [ -d $install_path ];then
  •         result_output warn "$install_path already exists!!!!"
  •         if [ "$select_name" == "LAMP environment" ];then
  •         cat << EOF
  • +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  • [r/R] will be removed $install_path; and continue this
  • [c/C] will be quit this install and continue Next-install !!
  • Do not select everying,installation program will
  • +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  • EOF
  •         else
  •         cat << EOF
  • +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  • [r/R] will be removed $install_path; and continue this
  • Do not select everying,installation program will
  • +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  • EOF
  •         fi
  •         read -p "Please enter your select:" s
  •         if ["$s" == "r" -"$s" == "R" ];then
  •             echo "Remove $install_path"
  •             sleep 1
  •             rm -rf $install_path
  •         elif [ "$s" == "c" -o "$s" == "C" ];then
  •             result_output ok "Conntinue next install ...."
  •             echo
  •             return_set=1 #if the return_set  value is not null ; the install function whill break
  •         else
  •             echo "Do not select everying,installation program quit!\n"
  •             exit 0
  •         fi
  •     fi
  • }
  • #Mysql安装
  • mysql_install(){
  •     install_path="$MYSQL_PATH"
  •     installpath_exist_check
  •     if [ ! -z $return_set ];then
  •         return_set='' #设置变量 return_set='' 初始化为空
  •         return 1    #break
  •     fi
  •     rpm_info="mysql-server"
  •     rpm_exist_check
  •     #用户检查/添加
  •     group_mysql_exist=$(grep mysql /etc/passwd)
  •     user_mysql_exist=$(cat /etc/passwd | grep mysql)
  •     if [ -z "$group_mysql_exist" ];then
  •         groupadd $MYSQL_USER
  •         else
  •         result_output warn "group $MYSQL_GROUP is already exist"
  •     fi
  •     if [ -z "$user_mysql_exist" ];then
  •         useradd $MYSQL_USER -c "start mysqld's account" -d /dev/null -g $MYSQL_GROUP -s /sbin/nologin
  •     else
  •         result_output warn "user $MYSQL_USER is already exist"
  •     fi
  •     file_name=$MYSQL_PKG
  •     file_exist_check
  •     tar zxvf $PKG_PATH/$MYSQL_PKG
  •     cd $PKG_PATH/$MYSQL_PKG_
  •     cd $PKG_PATH/$MYSQL_PKG_
  •     exec_check
  •     ./configure  \
  •     --prefix=$MYSQL_PATH \
  •     --enable-thread-safe-client \
  •     --with-charset=utf8 \
  •     --with-extra-charsets=all \
  •     --enable-assembler
  •     exec_check
  •     make  && make install
  •     exec_check
  •     rm -f /etc/my.cnf
  •     cp support-files/my-medium.cnf /etc/my.cnf
  •     $MYSQL_PATH/bin/mysql_install_db --user=$MYSQL_USER
  •     chown -R root:$MYSQL_GROUP  $MYSQL_PATH
  •     chown -R $MYSQL_USER:$MYSQL_GROU  $MYSQL_DATADIR
  •     result_output ok "$MYSQL_PKG is complete install"
  • }
  • #apache 安装
  • apache_install(){
  •     install_path="$HTTPD_PATH"
  •     installpath_exist_check
  •     if [ ! -z $return_set ];then
  •         return_set=''
  •         return 1
  •     fi
  •     rpm_info="http"
  •     rpm_exist_check
  •     file_name=$HTTPD_PKG
  •     file_exist_check
  •     tar zxvf $HTTPD_PKG
  •     cd $PKG_PATH/$HTTPD_PKG_
  •     ./configure --prefix=$HTTPD_PATH \
  •     --enable-so \
  •     --enable-rewrite
  •     exec_check
  •     make  && make install
  •     exec_check
  •     result_output ok "$HTTPD_PKG_ is complete install"
  • }
  • #GD 库安装
  • gdlib_install(){
  •     exec_check(){
  •         if [ $? != 0 ];then
  •             result_output failed "Num:$num   命令:$cmd   执行结果:FAILED"
  •             exit 1
  •         else
  •             result_output ok "Num:$num   命令:$cmd   执行结果:OK  "
  •             sleep 2
  •         fi
  •     }
  •     install_path="$GDLIB_PATH"
  •     installpath_exist_check
  •     if [ ! -z $return_set ];then
  •         return_set=''
  •         return 1
  •     fi
  •     #安装GD库时产生的临时文件  - 记录执行的单条命令
  •     GDLIB_CMD=${PKG_PATH}/gdlib_cmd
  •     #安装GD库时中间如果报错然后去再次执行时 就从报错的那断
  •     GDLIB_LABLE=${PKG_PATH}/gdlib_lable
  •     #对执行到的命令行数进行记录
  •     GDLIB_NUM=${PKG_PATH}/gdlib_num
  •     #记录执行过程中最近一次的cd目录
  •     GDLIB_PWD=${PKG_PATH}/gdlib_pwd
  •     #将GD安装脚本清理注释
  •     GDLIB_SETUP_TEMP=${PKG_PATH}/gdlib_setup_temp
  •     grep -v '^$' $GDLIB_SETUP | grep -v '^#' > $GDLIB_SETUP_TEMP
  •     IFS=$'\n'
  •     error_info=''
  •     num=0
  •     if [ -s $GDLIB_LABLE ];then
  •         GDLIB_SETUP=$GDLIB_LABLE
  •         num=$(expr $(cat $GDLIB_NUM) - 1)
  •         cd $(cat $GDLIB_PWD)
  •     fi
  •     for cmd in $(grep -v '^$' $GDLIB_SETUP | grep -v '^#');do
  •         sleep 0.5
  •         echo "$cmd" > $GDLIB_CMD
  •         num=$(expr $num + 1)
  •         echo $num > $GDLIB_NUM
  •         #echo -n "$num"
  •         sed -"$num",'$p' $GDLIB_SETUP_TEMP > $GDLIB_LABLE
  •         #exec_check
  •         if grep 'cd' $GDLIB_CMD >/dev/null;then
  •             source $GDLIB_CMD
  •             tem_dir=$(pwd)
  •             echo $tem_dir > $GDLIB_PWD
  •             #echo "tem_dir==$tem_dir"
  •             cd $tem_dir
  •             exec_check
  •             continue
  •         elif grep 'tar' $GDLIB_CMD >/dev/null;then
  •             cd $PKG_PATH
  •         fi
  •         sh $GDLIB_CMD
  •         exec_check
  •     done
  •     rm -rf $GDLIB_LABLE
  •     rm -rf $GDLIB_NUM
  •     rm -rf $GDLIB_CMD
  •     rm -rf $GDLIB_SETUP_TEMP
  •     rm -rf $GDLIB_PWD
  •     cmd=''
  •     result_output ok "GD Install Successful!!!"
  •     IFS=$' \t\n'
  • }
  • #PHP安装
  • php_install(){
  •     install_path="$PHP_PATH"
  •     installpath_exist_check
  •     if [ ! -z $return_set ];then
  •         return_set=''
  •         return 1
  •     fi
  •     error_info=''
  •     if ! $GDLIB_PATH/bin/gdlib-config  --version >/dev/null;then
  •         result_output failed "GD_LIB  error or not installed"
  •         exit 1
  •     fi
  •     if ! $MYSQL_PATH/bin/mysql ->/dev/null;then
  •         result_output failed "$MYSQL_PKG_  error or not installed"
  •         exit 1
  •     fi
  •     if ! $HTTPD_PATH/bin/apachectl ->/dev/null;then
  •         result_output failed "$HTTPD_PKG_  error or not installed"
  •         exit 1
  •     fi
  •     install_path="$PHP_PATH"
  •     installpath_exist_check
  •     tar -xzvf $PKG_PATCH/$PHP_PKG
  •     cd $PKG_PATH/$PHP_PKG_
  •     ./configure  --prefix=$PHP_PATH \
  •     --with-apxs2=$HTTPD_PATH/bin/apxs  \
  •     --with-mysql=$MYSQL_PATH  \
  •     --with-gd=/usr/local/libgd  \
  •     --enable-gd-native-ttf  \
  •     --with-ttf  \
  •     --enable-ftp    \
  •     --enable-gd-jis-conv  \
  •     --with-freetype-dir=/usr/local/freetype  \
  •     --with-jpeg-dir=/usr/local/libjpeg  \
  •     --with-png-dir=/usr   \
  •     --with-zlib-dir=/usr/local/zlib   \
  •     --enable-xml  \
  •     --enable-mbstring  \
  •     --enable-sockets
  •     exec_check
  •     make && make install
  •     exec_check
  •     cp php.ini-dist /usr/local/php/lib/php.ini
  •     exec_check
  • }
  • #LAMP
  • lamp_install(){
  •     #如果已安装则跳过 继续安装下面部分
  •     mysql_install
  •     apache_install
  •     gdlib_install
  •     php_install
  • }
  • case_select(){
  •     read -p "Are you sure install the $select_name ?[y/Y]" s
  •     if [ "$s" == "y" -o "$s" == "Y" ];then
  •             $select_install
  •     else
  •             echo "Do not select [y/Y],installation program quit!\n"
  •             exit 1
  •     fi
  •     }
  • read -p "Please enter the installation program number:" n
  • case $n in
  •     1)
  •         select_name=$MYSQL_PKG_
  •         select_install=mysql_install
  •         case_select
  •         ;;
  •     2)
  •         select_name=$HTTPD_PKG_
  •         select_install=apache_install
  •         case_select
  •         ;;
  •     3)
  •         select_name=gdlib
  •         select_install=gdlib_install
  •         case_select
  •         ;;
  •     4)
  •         select_name=$PHP_PKG_
  •         select_install=php_install
  •         case_select
  •         ;;
  •     5)
  •         select_name="LAMP environment"
  •         select_install=lamp_install
  •         case_select
  •         ;;
  •     *)
  •         echo "Do not select [y/Y],installation program quit!\n"
  •         exit 1
  • esac
  • ####这里是另一个脚本##这里是另一个脚本###gdsetup.sh###这里是另一个脚本#####这里是另一个脚本###
    1. #!/bin/bash
    2. # -*- coding: utf-8 -*-
    3. #-------------------------------------------------------------------------------
    4. # Name:        gdsetup.sh
    5. # Purpose:
    6. # Author:      hjjie.cublog.cn
    7. # Mail:        hjjie2006@126.com
    8. #
    9. # Created:     28/09/2011
    10. # Copyright:(c) Jerome 2011
    11. #
    12. #-------------------------------------------------------------------------------
    13. #(1).安装zlib
    14. tar  -xzvf zlib-1.2.3.tar.gz
    15. cd  zlib-1.2.3
    16. ./configure --prefix=/usr/local/zlib
    17. make  &&  make install
    18. #(2).安装libpng
    19. tar zxvf libpng-1.2.26.tar.gz
    20. cd libpng-1.2.26/scripts/
    21. mv makefile.linux ../makefile
    22. cd ..
    23. make && make install
    24. #注意,这里的makefile不是用./configure生成,而是直接从scripts/里拷一个
    25. #3).安装freetype
    26. tar -xzvf freetype-2.3.5.tar.gz
    27. cd freetype-2.3.5
    28. ./configure --prefix=/usr/local/freetype
    29. make && make install
    30. #4).安装Jpeg
    31. tar -zxvf jpegsrc.v6b.tar.gz
    32. cd jpeg-6b/
    33. mkdir -p /usr/local/libjpeg
    34. mkdir -p /usr/local/libjpeg/include
    35. mkdir -p /usr/local/libjpeg/bin
    36. mkdir -p /usr/local/libjpeg/lib
    37. mkdir -p /usr/local/libjpeg/man
    38. mkdir -p /usr/local/libjpeg/man/man1
    39. ./configure --prefix=/usr/local/libjpeg  --enable-shared  --enable-static
    40. make && make install
    41. #注意,这里configure一定要带--enable-shared参数,不然,不会生成共享库
    42. #5).安装Fontconfig
    43. tar -zxvf fontconfig-2.4.2.tar.gz
    44. cd fontconfig-2.4.2
    45. ./configure --with-freetype-config=/usr/local/freetype/bin/freetype-config
    46. make && make install
    47. #6).安装gettext
    48. #cp /usr/lib/libattr.* /lib/
    49. tar -vxzf gettext-0.17.tar.gz
    50. cd gettext-0.17
    51. ./configure
    52. make && make install
    53. #7).安装GD
    54. tar -zxvf gd-2.0.35.tar.gz
    55. cd gd-2.0.35
    56. ./configure --prefix=/usr/local/libgd  --with-png  --with-freetype=/usr/local/freetype  --with-jpeg=/usr/local/libjpeg  --enable-m4_pattern_allow
    57. make && make install
    58. #8).编辑/etc/ld.so.conf,添加以下几行到此文件中
    59. if ! grep '/usr/local/zlib/lib' /etc/ld.so.conf >/dev/null; then echo '/usr/local/zlib/lib' >>/etc/ld.so.conf ;fi
    60. if ! grep '/usr/local/freetype/lib' /etc/ld.so.conf >/dev/null; then echo '/usr/local/freetype/lib' >>/etc/ld.so.conf ;fi
    61. if ! grep '/usr/local/libjpeg/lib' /etc/ld.so.conf >/dev/null; then echo '/usr/local/libjpeg/lib' >>/etc/ld.so.conf ;fi
    62. if ! grep '/usr/local/libgd/lib' /etc/ld.so.conf >/dev/null; then echo '/usr/local/libgd/lib' >>/etc/ld.so.conf ;fi
    63. ldconfig
    64. #9).安装libxml
    65. tar -xzvf libxml2-2.6.31.tar.gz
    66. cd libxml2-2.6.31
    67. ./configure
    68. make  && make install
posted on 2013-04-13 17:19  tangoo  阅读(418)  评论(0)    收藏  举报