公司没有专职运维,搭建服务器全部都是程序员操作。以前手动搭建需要1天时间,搭建过程很慢、很无聊,于是自己总结了如下脚本,执行完需要18分钟,以后再也不用苦逼的搭建环境了。
build.sh
#!/bin/bashecho "新建developmentEnvironment目录"
cd /usr
mkdir developmentEnvironment
cd /#1.java
echo "下载jdk"
wget http://xx.com.cn/jdk-8u162-linux-x64.tar.gzecho "解压jdk"
tar xf jdk-8u162-linux-x64.tar.gz -C /usr/developmentEnvironmentecho "配置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/profilesource /etc/profileecho "jdk版本"
java -version#2.mysql
echo "下载mysql国内源"
wget -P /etc/yum.repos.d http://xx.com.cn/mysql-community.repoecho "安装mysql服务器"
yum -y install mysql-community-serverecho "启动mysql"
systemctl start mysqldecho "mysql运行状态"
systemctl status mysqldstr=$(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;
EOFecho "重启mysql服务使配置生效"
systemctl restart mysqldecho "mysql版本"
mysql -V#3.git
echo "git安装"
yum -y install gitecho "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.gzecho "解压maven"
tar xf apache-maven-3.6.3-bin.tar.gz -C /usr/developmentEnvironmentecho "配置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/profileecho "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-develecho "nginx_3.zlib安装"
yum -y install zlib zlib-develecho "nginx_4.OpenSSL安装"
yum -y install openssl openssl-develecho "下载nginx"
wget http://xx.com.cn/nginx-1.12.2.tar.gzecho "解压nginx"
tar xf nginx-1.12.2.tar.gz -C /usr/developmentEnvironmentecho "nginx使用默认配置"
cd /usr/developmentEnvironment/nginx-1.12.2
./configureecho "nginx编译安装"
make
make installecho "启动nginx"
/usr/local/nginx/sbin/nginxecho "配置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/profileecho "nginx版本"
nginx -vecho "下载nginx自启动脚本"
wget -P /etc/init.d http://xx.com.cn/nginxecho "nginx添加可执行权限"
chmod a+x /etc/init.d/nginxecho "将nginx加入chkconfig管理列表"
chkconfig --add /etc/init.d/nginxecho "设置nginx终端模式开机启动"
chkconfig nginx on#6.shell
echo "新建shell目录"
mkdir /opt/shell
mkdir -p /opt/cf/backupecho "下载backup.sh"
wget -P /opt/shell http://xx.com.cn/backup.shecho "backup.sh添加可执行权限"
chmod +x /opt/shell/backup.shecho "配置backup.cron"
touch /opt/shell/backup.cron
echo '0 6 * * * /opt/shell/backup.sh' > /opt/shell/backup.cronecho "检查定时任务是否已创建"
crontab /opt/shell/backup.cron
crontab -lecho "启动bakup.sh定时任务"
service crond restartecho "下载start.sh"
wget -P /opt/shell http://xx.com.cn/start.shecho "start.sh添加可执行权限"
chmod +x /opt/shell/start.shecho "/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.localecho "rc.local添加可执行权限"
chmod +x /etc/rc.d/rc.localecho "版本"
java -version
mysql -V
git --version
mvn -v
nginx -vecho "删除安装包"
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 截取字符串