Linux的shell脚本自动一键安装jdk,postgresql,nginx(兼容centos+ubuntu)

原创
2017/08/10 17:10
阅读数 1K

最外面的总脚本install.sh:

#!/bin/bash
basepath=$(cd `dirname $0`; pwd)
chmod +x *.sh
chmod +x ./Nginx/*.sh
chmod +x ./PostgreSQL/*.sh
chmod +x ./Jre/*.sh
chmod +x ./webapps/*.sh
echo "begin install java"
sleep 3
cd $basepath/Jre
bash ./install_java.sh
cd $basepath/Nginx
echo "begin install nginx"
sleep 3
bash ./install_nginx.sh $basepath
cd $basepath/PostgreSQL
echo "begin install postgres"
sleep 3
bash ./install_postgre.sh $basepath
systemv=$(lsb_release -a | grep Ubuntu)
if [ -z "$systemv" ]; then
    echo  "*/1 * * * * bash $basepath/monitor.sh >> $basepath/cronlog 2>&1" >> /var/spool/cron/root
    echo  "* * * * 1 bash $basepath/clearlog.sh >> $basepath/cronlog 2>&1" >> /var/spool/cron/crontabs/root
    service crond reload
else
    cp $basepath/webapps/keystore.p12 /root/
    echo  "*/1 * * * * bash $basepath/monitor.sh >> $basepath/cronlog 2>&1" >> /var/spool/cron/crontabs/root #每分钟跑监控脚本
    echo  "* * * * 1 bash $basepath/clearlog.sh >> $basepath/cronlog 2>&1" >> /var/spool/cron/crontabs/root #每周1清理日志
    chmod 0600 /var/spool/cron/crontabs/root
    service cron reload
    #如果cron没运行,ps -ef|grep cron查看进程是否有,然后查看日志:/var/log/cron.log
    #chmod 600 /var/spool/cron/crontabs/root
    #service cron restart
fi
jdk安装脚本:

#!/bin/bash
#install java
java=$(cat /etc/profile | grep JAVA_HOME)
if [ -z "$java" ]; then
    #unzip openjdk_1_7.zip
    tar -zxvf jdk-7u80-linux-x64.tar.gz#改为使用oracle的jdk,避免出现openjdk https/ssl兼容性错误ECKeyPairGenerator.generateKeyPair错误(我就遇到了,并且搞了2天。。。)
    basepath=$(cd `dirname $0`; pwd)
    JAVA_HOME1="$basepath/jdk1.7.0_80"
    echo "export JAVA_HOME=$basepath/jdk1.7.0_80" >> /etc/profile
    echo "export PATH=$JAVA_HOME1/bin:$PATH" >> /etc/profile
    echo "export CLASSPATH=$JAVA_HOME1/lib/dt.jar:$JAVA_HOME1/lib/tools.jar" >> /etc/profile
    #export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
    source /etc/profile
else
    echo java has installed >> log.txt
fi
nginx安装脚本(包括ssl支持):

#!/bin/bash
#postgre install
basepath=$(cd `dirname $0`; pwd)
echo "$basepath"
sleep 1
tar -zxvf openssl-1.0.2l.tar.gz
cd openssl-1.0.2l
./config
make && make install
#rewrite模块需要安装的包
cd $basepath
tar -zxvf pcre-8.41.tar.gz
cd pcre-8.41
./configure
make && make install
#gzip模块需要安装的包
cd $basepath
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install
cd $basepath
tar -zxvf nginx-1.12.1.tar.gz
echo "end of tar, start set export"
mkdir nginxhome
cd nginx-1.12.1
./configure --prefix=$basepath/nginxhome --with-http_ssl_module --with-openssl=$basepath/openssl-1.0.2l/ --with-zlib=$basepath/zlib-1.2.11/ --with-pcre=$basepath/pcre-8.41/#zlib为提供数据压缩功能,pcre为提供rewrite和if/else语句的功能
make && make install
cp -f $1/webapps/nginx_conf/nginx.conf $basepath/nginxhome/conf/#自己的nginxconf文件替换
cp $1/webapps/nginx_conf/vcm.* $basepath/nginxhome/conf/#自己的ssl的key,cert文件放入conf目录
postgresql数据库安装脚本:

#!/bin/bash
#postgre install
basepath=$(cd `dirname $0`; pwd)
echo "$basepath"
sleep 5
tar -zxvf postgresql-9.6.3-2-linux-x64-binaries.tar.gz
echo "end of tar, start set export"
#增加开机自动启动数据库功能
chmod +x /etc/rc.local
echo "/bin/bash $basepath/start.sh" >> /etc/rc.local
#userdel -r postgres
#密码:YFgD21M6
useradd -m -p '$6$h.exHsoB$XgPUfku6Zl0NB1HvMsMPu33sHABer2DnTpKJV7lTw5kgZjzjbrhOy/DzmGf8UWNwwf13/KluFQR9bmsy1J5Ix.' postgres#自动创建家目录,同时设置好密码,该密码为encrpt加密过的
#passwd postgres
su - postgres -s /bin/bash $basepath/initdb.sh $basepath $1
数据库初始化脚本initdb.sh:

#!/bin/bash
#第二段
echo "init db $1 $2"
sleep 5
echo "export PGHOME=$1/pgsql" >> ~/.profile
echo "export PGDATA=~/data" >> ~/.profile
echo "export PATH=$PATH:PGHOME/bin" >> ~/.profile
source ~/.profile
systemv=$(lsb_release -a | grep Ubuntu)
if [ -z "$systemv" ]; then
    initdb
    #替换配置文件,主要是为了修改数据库端口号为5433(默认5432)
    cp -f $1/postgresql.conf ~/data/
    pg_ctl start
    sleep 10
    echo "end of export, start set data"
    psql -h 127.0.0.1 -p 5433 -d postgres -U postgres -f $2/dbcreate.sql
    echo "end of create db"
    psql -h 127.0.0.1 -p 5433 -d vcm -U postgres -f $2/initialize.sql
else
    cd $1/pgsql/bin
    ./initdb
    cp -f $1/postgresql.conf ~/data/
    ./pg_ctl start
    sleep 10
    echo "end of export, start set data"
    ./psql -h 127.0.0.1 -p 5433 -d postgres -U postgres -f $2/webapps/sql_script/dbcreate.sql
    echo "end of create db"
    ./psql -h 127.0.0.1 -p 5433 -d vcm -U postgres -f $2/webapps/sql_script/initialize.sql
fi

目录结构:

每个文件夹里放对应的脚本及安装包

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部