Shell脚本自动化部署LAMP环境

[root@localhost ~]# vim liang.sh
#!/bin/bash

# LAMP终极部署

cat <<-EOF

+-------------------------------------------------------------------------+

| LAMP终极部署 V1.0 |

+-------------------------------------------------------------------------+

| a. 部署Apache服务 |

| b. 部署php服务 |

| c. 部署Mysql服务 |

| d. 一键部署LAMP |

| q. 按q键退出程序 |

+-------------------------------------------------------------------------+

EOF

# 安装Apache

install_Apache()

{

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

"liang.sh" 475L, 6051C                                                     
#!/bin/bash

# LAMP终极部署

cat <<-EOF

+-------------------------------------------------------------------------+

| LAMP终极部署 V1.0 |

+-------------------------------------------------------------------------+

| a. 部署Apache服务 |

| b. 部署php服务 |

| d. 一键部署LAMP |

| q. 按q键退出程序 |

+-------------------------------------------------------------------------+

EOF

# 安装Apache

install_Apache()

{

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

sed -i '/^\bSELINUX\b/c SELINUX=disabled' /etc/selinux/config

mkdir /usr/local/apr &> /dev/null

mkdir /usr/local/apr-util &> /dev/null

mkdir /usr/local/apache &> /dev/null

cd /usr/local/src

echo "正在下载Apache服务,请稍等!!!"

wget http://archive.apache.org/dist/apr/apr-1.6.5.tar.gz &> /dev/null

wget http://archive.apache.org/dist/apr/apr-util-1.6.1.tar.gz &> /dev/null


if [ $? -eq 0 ]

then

echo "download success"

else

echo "download failed"

exit

fi

tar xf apr-1.6.5.tar.gz

tar xf apr-util-1.6.1.tar.gz

tar xf httpd-2.4.37.tar.gz

echo "正在安装所需的依赖包"

yum -y install gcc gcc-c++ openssl openssl-devel expat-devel &> /dev/null

then

echo "依赖包安装成功"

else

echo "依赖包安装失败"

exit

fi

cd /usr/local/src/apr-1.6.5/

echo "正在配置和编译安装apr,请喝口水稍等!!!"

./configure --prefix=/usr/local/apr/ &> /dev/null

make &> /dev/null && make install &>/dev/null

if [ $? -eq 0 ]

else

echo "apr installed failed"

exit

fi

cd /usr/local/src/apr-util-1.6.1/


make &> /dev/null && make install &> /dev/null

if [ $? -eq 0 ]

then

echo "apr-util installed"

else

echo "apr-util installed failed"

exit

fi

cd /usr/local/src/httpd-2.4.37/

echo "正在配置Apache"


make &> /dev/null && make install &> /dev/null

if [ $? -eq 0 ]

then

echo "Apache installed"

echo "Apache installed failed"

exit

fi

cd /usr/local/apache/bin/

echo ServerName www.fangxi.com >> /usr/local/apache/conf/httpd.conf

./apachectl start

if [ $? -eq 0 ]

then

echo "Apache安装成功并启动"

else

echo "Apache启动失败"

exit

fi

}

#安装php

install_php()

{

echo "正在安装php服务"


php-mbstring php-xml php-dom &> /dev/null

if [ $? -eq 0 ]

then

else

echo "php安装失败"

exit

fi

systemctl start php-fpm &> /dev/null

if [ $? -eq 0 ]

then

echo "php安装成功"

else

echo "php安装失败"

fi

}

#编译安装Mysql

install_mysql()

{

echo "开始安装mysql"

echo "正在准备编译环境,wait a minute"


&> /dev/null

if [ $? -eq 0 ]

then

echo "编译环境已准备好"

else

echo "编译环境准备失败"

exit

fi

echo "正在下载源码包----请稍稍等一下"


groupadd mysql

useradd -r -g mysql -s /bin/nologin mysql

tar xf mysql-boost-5.7.26.tar.gz

cd mysql-5.7.26/

echo "正在配置中,请再喝口水,小憩一下"

cmake . \

-DWITH_BOOST=boost/boost_1_59_0/ \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DSYSCONFDIR=/etc \

-DMYSQL_DATADIR=/usr/local/mysql/data \

-DINSTALL_MANDIR=/usr/share/man \

-DMYSQL_TCP_PORT=3306 \

-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \

-DDEFAULT_CHARSET=utf8 \

-DEXTRA_CHARSETS=all \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_READLINE=1 \

-DWITH_SSL=system \

-DWITH_EMBEDDED_SERVER=1 \

-DENABLED_LOCAL_INFILE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 &> /dev/null

if [ $? -eq 0 ]

then

echo "mysql配置成功"

else

echo "mysql配置失败"

fi

echo "----------正在安装编译安装Mysql请稍等-----------"

make &> /dev/null && make install /dev/null

if [ $? -eq 0 ]

then

echo "mysql编译安装成功"

else

echo "mysql编译安装失败"

exit

fi

echo [mysqld] > /etc/my.cnf

echo basedir=/usr/local/mysql >> /etc/my.cnf

echo datadir=/usr/local/mysql/data >> /etc/my.cnf

echo "mysql配置文件successed"

cd /usr/local/mysql/

mkdir mysql-files

chown -R mysql.mysql /usr/local/mysql

echo "-------------正在初始化Mysql请稍等--------------"


--datadir=/usr/local/mysql/data &> mima.txt

mima=awk '/password/ {print $NF}' mima.txt

echo "初始密码为:$mima"

if [ $? -eq 0 ]

then

echo "mysql初始化成功"

else

echo "mysql初始化失败"

exit

fi

bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

#给数据库加密

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld on

#添加到开机启动项

systemctl start mysqld

if [ $? -eq 0 ]

then

echo "mysql启动成功"

else

echo "mysql启动失败"

exit

fi

echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile

source /etc/profile

echo "----------修改数据库初始密码----------"

read -p "请输入你要设置的数据库密码" new_mima

mysqladmin -uroot -p${mima} password "$new_mima"

if [ $? -eq 0 ]

then

echo "mysql初始密码修改成功,mysql部署完成"

else

echo "mysql初始密码修改失败"

exit

fi

}

while :

do

read -p "请输入你要选择的参数: " var

case $var in

a)

install_Apache

;;
mima=awk '/password/ {print $NF}' mima.txt

echo "初始密码为:$mima"

if [ $? -eq 0 ]

then

echo "mysql初始化成功"

else

echo "mysql初始化失败"

exit

fi

bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

#给数据库加密

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld on

#添加到开机启动项

systemctl start mysqld

if [ $? -eq 0 ]

then

echo "mysql启动成功"

else

echo "mysql启动失败"

exit

fi

echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile

source /etc/profile

echo "----------修改数据库初始密码----------"

read -p "请输入你要设置的数据库密码" new_mima

mysqladmin -uroot -p${mima} password "$new_mima"

if [ $? -eq 0 ]

then

echo "mysql初始密码修改成功,mysql部署完成"

else

echo "mysql初始密码修改失败"

exit

fi

}

while :

do

read -p "请输入你要选择的参数: " var

case $var in

a)

install_Apache

;;

b)

install_php

;;

c)

install_mysql

;;

d)

install_Apache

install_php

install_mysql

;;

q)

exit

;;

*)

printf "请按照上方提供的选项输入!!!\n"

;;

esac

done


[root@localhost ~]# chmod +x liang.sh 
[root@localhost ~]# bash liang.sh 

+-------------------------------------------------------------------------+

| LAMP终极部署 V1.0 |

+-------------------------------------------------------------------------+

| a. 部署Apache服务 |

| b. 部署php服务 |

| c. 部署Mysql服务 |

| d. 一键部署LAMP |

| q. 按q键退出程序 |

+-------------------------------------------------------------------------+

请输入你要选择的参数: 



 

 

Shell脚本自动化部署LAMP环境_第1张图片 

 

你可能感兴趣的:(Shell,linux,bash,shell)