Shell学习成果之一键自动安装LAMP自动安装脚本

编程

一键自动安装LAMP环境,网速好的可以一键安装完成,网速不好的提前下载mysql安装包

系统版本:CentOS7.7.1908

MySQL版本:mysql-8.0.18

Apache版本:httpd-2.4.41

PHP版本:php-7.3.9

###############################################START########################################################

#!/bin/bash
#auto install LAMP
#By kangjian 2019-11-18

#Httpd define path varible
H_FILES=httpd-2.4.41.tar.bz2
H_FILES_DIR=httpd-2.4.41
H_URL=https://mirrors.aliyun.com/apache/httpd/
H_PREFIX=/usr/local/apache
H_APR_URL=https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.bz2
H_APR-UTIL_URL=https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.bz2

#PHP define path varible
P_FILES=php-7.3.9.tar.bz2
P_FILES_DIR=php-7.3.9
P_URL=http://mirrors.sohu.com/php/
P_PREFIX=/usr/local/php7

if [[ -z "$1" ]];then
        echo -e "33[36mPlease select install menu follow:33[1m"
        echo "1)编译安装Apache服务器"
        echo "2)编译安装Mysql服务器"
        echo "3)编译安装PHP服务器"
        echo "4)配置index.php并启动LAMP服务"
        echo -e "33[31mUsage:{/bin/sh $0 1|2|3|4|help}33[0m"
        exit
fi

if [[ "$1" -eq "1" ]];then
    #Http define install environment
    yum -y install gcc gcc-c++ expat-devel openssl pcre pcre-devel bzip2 wget;
    wget -c $H_URL/$H_FILES &&tar -jxvf $H_FILES ;
    wget -c https://mirrors.aliyun.com/apache/apr/apr-1.7.0.tar.bz2 &&tar -jxvf apr-1.7.0.tar.bz2 &&mv apr-1.7.0 $H_FILES_DIR/srclib/apr ;
    wget -c https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.bz2 &&tar -jxvf apr-util-1.6.1.tar.bz2 &&mv apr-util-1.6.1 $H_FILES_DIR/srclib/apr-util ;
    cd $H_FILES_DIR &&./configure --prefix=$H_PREFIX --with-included-apr

    if [ $? -eq 0 ];then
        make &&make install
        echo -e "33[32m编译安装Apache服务器成功!33[0m"
    else
        echo -e "33[32m编译安装Apache服务器失败!33[0m"
        exit
    fi
fi

if [[ "$1" -eq "2" ]];then
    #auto MySQL server
  olddb=`rpm -qa|grep mariadb` &&rpm -e --nodeps $olddb ;

  useradd -M -s /sbin/nologin mysql &&
  mkdir -p /usr/local/mysql &&
  mkdir -p /usr/local/mysql/data &&
  mkdir -p /usr/local/mysql/log &&
  mkdir -p /usr/local/mysql/log/binlog &&
  mkdir -p /usr/local/mysql/log/tmp &&
  touch /usr/local/mysql/log/mysqld.log &&
  chmod -R 777 /usr/local/mysql/log/ &&
  chmod -R 777 /usr/local/mysql/data &&
  chown -R mysql:mysql /usr/local/mysql/


  if [ $? -eq 0 ];then
      echo -e "33[32mMysql environment Complete!33[0m"
  else
      echo -e "33[32mMysql environment Complete Failed,Please check it.33[0m"
      exit
  fi

  #wget -c  https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.18-el7-x86_64.tar.gz ;    //网速好的取消注释,网速不好的提前把安装包与脚本放同一目录
  tar -xzvf mysql-8.0.18-el7-x86_64.tar.gz &&
  mv mysql-8.0.18-el7-x86_64/* /usr/local/mysql/ ;

  cd /usr/local/mysql &&/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --log-error=/usr/local/mysql/log/mysqld.log ;
  if [ $? -eq 0 ];then
      echo -e "33[32mMysql8.0 install complete!33[0m"
  else
      echo -e "33[32mMysql8.0 install Failed.please check it.33[0m"
      exit
  fi

  touch /etc/my.cnf ;
  echo "
  [client]
  port=3306
  default-character-set=utf8

  [mysqld]
  port=3306
  basedir=/usr/local/mysql
  datadir=/usr/local/mysql/data
  user=mysql
  default-storage-engine=INNODB
  character-set-server=utf8
  bind-address=0.0.0.0

  [mysqld_safe]
  log-error=/usr/local/mysql/log/mysqld.log" >> /etc/my.cnf ;

  chmod 644 /etc/my.cnf &&chown -R mysql:mysql /etc/my.cnf ;

  cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld ;
  sed -i "46s/basedir=/basedir=/usr/local/mysql/g" /etc/init.d/mysqld &&sed -i "47s/datadir=/datadir=/usr/local/mysql/g" /etc/init.d/mysqld ;
  chmod +x /etc/init.d/mysqld &&service mysqld start

  if [ $? -eq 0 ];then
      echo -e "33[32mMysql 8.0 auto install successfully!33[0m"
  else
      echo -e "33[32mMysql8.0 auto install Failed,Please check it.33[0m"
      exit
  fi

  echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile &&source /etc/profile ;
  chkconfig  --add mysqld &&chkconfig mysqld on
    echo -e "33[32m编译安装Mysql服务器成功!33[0m"
else
    echo  -e "33[32m编译安装Mysql服务器失败!33[0m"
    exit
fi

if [[ "$1" -eq "3" ]];then

  yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

  wget -c $P_URL/$P_FILES && tar -jxvf $P_FILES && cd $P_FILES_DIR &&./configure --prefix=$P_PREFIX --with-config-file-path=$P_PREFIX/etc --with-mysql=/usr/local/mysql --with-apxs2=$H_PREFIX/bin/apxs
           
  if [ $? -eq 0 ];then
          make ZEND_EXTRA_LIBS="-liconv" &&make install
          echo -e "33[32mThe $P_FILES_DIR server isntall successfully!33[0m"
  else
          echo -e "33[32mThe $P_FILES_DIR server install Failed,Please check it.33[0m"
          exit
  fi
    echo -e "33[32m编译安装PHP服务器成功!33[0m"
else
    echo -e "33[32m编译安装PHP服务器失败!33[0m"
    exit
fi

if [[ "$1" -eq "4" ]];then
    sed -i "/DirectoryIndex/s/index.html/index.php index.html/g" /usr/local/apache/conf/httpd.conf
    /usr/local/apache/bin/apachectl restart
    echo "AddType    application/x-httpd-php .php" >>/usr/local/apache/conf/httpd.conf
    IP=`ifconfig ens33|grep "broadcast"|awk "{print $2}"`
    echo "You can access http://$IP/"
cat >/usr/local/apache/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF

###############################################END##########################################################

以上是 Shell学习成果之一键自动安装LAMP自动安装脚本 的全部内容, 来源链接: utcz.com/z/510982.html

回到顶部