公司没有专职运维,搭建服务器全部都是程序员操作。以前手动搭建需要1天时间,搭建过程很慢、很无聊,于是自己总结了如下脚本,执行完需要18分钟,以后再也不用苦逼的搭建环境了。
build.sh
#!/bin/bash
echo "新建developmentEnvironment目录"
cd /usr
mkdir developmentEnvironment
cd /
#1.java
echo "下载jdk"
wget http://xx.com.cn/jdk-8u162-linux-x64.tar.gz
echo "解压jdk"
tar xf jdk-8u162-linux-x64.tar.gz -C /usr/developmentEnvironment
echo "配置jdk环境变量"
sed -i '$a #java' /etc/profile
sed -i '$a export JAVA_HOME=/usr/developmentEnvironment/jdk1.8.0_162' /etc/profile
sed -i '$a export JRE_HOME=/usr/developmentEnvironment/jdk1.8.0_162' /etc/profile
sed -i '$a export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' /etc/profile
sed -i '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
source /etc/profile
echo "jdk版本"
java -version
#2.mysql
echo "下载mysql国内源"
wget -P /etc/yum.repos.d http://xx.com.cn/mysql-community.repo
echo "安装mysql服务器"
yum -y install mysql-community-server
echo "启动mysql"
systemctl start mysqld
echo "mysql运行状态"
systemctl status mysqld
str=$(grep 'password' /var/log/mysqld.log)
echo "mysql密码日志: ${str}"
#分割符为'root@localhost: '
password=${str##*root@localhost: }
echo "mysql默认密码: ${password}"
mysql -uroot -p${password} << EOF
#更新mysql密码
alter user 'root'@'localhost' identified by '111111';
#开启mysql的远程访问
grant all privileges on *.* to 'root'@'%' identified by '111111' with grant option;
#刷新权限,退出
flush privileges;
exit;
EOF
echo "重启mysql服务使配置生效"
systemctl restart mysqld
echo "mysql版本"
mysql -V
#3.git
echo "git安装"
yum -y install git
echo "git版本"
git --version
#4.maven,换成自己的下载地址TODO
echo "下载maven"
wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
echo "解压maven"
tar xf apache-maven-3.6.3-bin.tar.gz -C /usr/developmentEnvironment
echo "配置maven环境变量"
sed -i '$a #maven' /etc/profile
sed -i '$a MAVEN_HOME=/usr/developmentEnvironment/apache-maven-3.6.3' /etc/profile
sed -i '$a export MAVEN_HOME' /etc/profile
sed -i '$a export PATH=${PATH}:${MAVEN_HOME}/bin' /etc/profile
source /etc/profile
echo "mvn版本"
mvn -v
#5.nginx
echo "nginx_1.gcc安装"
yum -y install gcc-c++
echo "nginx_2.PCRE pcre-devel 安装"
yum -y install pcre pcre-devel
echo "nginx_3.zlib安装"
yum -y install zlib zlib-devel
echo "nginx_4.OpenSSL安装"
yum -y install openssl openssl-devel
echo "下载nginx"
wget http://xx.com.cn/nginx-1.12.2.tar.gz
echo "解压nginx"
tar xf nginx-1.12.2.tar.gz -C /usr/developmentEnvironment
echo "nginx使用默认配置"
cd /usr/developmentEnvironment/nginx-1.12.2
./configure
echo "nginx编译安装"
make
make install
echo "启动nginx"
/usr/local/nginx/sbin/nginx
echo "配置nginx环境变量"
sed -i '$a #nginx' /etc/profile
sed -i '$a PATH=$PATH:/usr/local/nginx/sbin' /etc/profile
sed -i '$a export PATH' /etc/profile
source /etc/profile
echo "nginx版本"
nginx -v
echo "下载nginx自启动脚本"
wget -P /etc/init.d http://xx.com.cn/nginx
echo "nginx添加可执行权限"
chmod a+x /etc/init.d/nginx
echo "将nginx加入chkconfig管理列表"
chkconfig --add /etc/init.d/nginx
echo "设置nginx终端模式开机启动"
chkconfig nginx on
#6.shell
echo "新建shell目录"
mkdir /opt/shell
mkdir -p /opt/cf/backup
echo "下载backup.sh"
wget -P /opt/shell http://xx.com.cn/backup.sh
echo "backup.sh添加可执行权限"
chmod +x /opt/shell/backup.sh
echo "配置backup.cron"
touch /opt/shell/backup.cron
echo '0 6 * * * /opt/shell/backup.sh' > /opt/shell/backup.cron
echo "检查定时任务是否已创建"
crontab /opt/shell/backup.cron
crontab -l
echo "启动bakup.sh定时任务"
service crond restart
echo "下载start.sh"
wget -P /opt/shell http://xx.com.cn/start.sh
echo "start.sh添加可执行权限"
chmod +x /opt/shell/start.sh
echo "/etc/rc.d/rc.local添加start.sh"
sed -i '$a # start' /etc/rc.d/rc.local
sed -i '$a /opt/shell/start.sh' /etc/rc.d/rc.local
source /etc/rc.d/rc.local
echo "rc.local添加可执行权限"
chmod +x /etc/rc.d/rc.local
echo "版本"
java -version
mysql -V
git --version
mvn -v
nginx -v
echo "删除安装包"
rm -rf ./jdk-8u162-linux-x64.tar.gz
rm -rf ./apache-maven-3.6.3-bin.tar.gz
rm -rf ./nginx-1.12.2.tar.gz
脚本使用到的jdk-8u162-linux-x64.tar.gz、nginx-1.12.2.tar.gz、mysql-community.repo、nginx、backup.sh、start.sh已经上传到 Centos开发环境 ,大家可以下载下来,上传到自己的服务器,做成下载链接。接着将xx.com.cn替换成自己的下载地址, 111111替换成自己的数据库密码,就能运行脚本了。
下载链接用的公司内部的,无法分享,小伙伴有可用的下载链接、遇到问题欢迎留言!
参考:
1.shell脚本中执行mysql命令
2.Linux Shell 截取字符串