CentOS 初始化脚本

偶尔善良 提交于 2020-01-16 01:22:54
#!/bin/bash
# curl https://yiyingcanfeng.github.io/centos-init.sh | bash
# 可选参数base kernel python php nodejs cmd_game jdk mysql mongodb docker
# 比如
# curl https://yiyingcanfeng.github.io/centos-init.sh | bash -s base

function system_config() {
    # 修改主机名
    #hostnamectl set-hostname aaa
    # 禁用selinux
    sed -i 's/SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
    setenforce 0
    # 修改开机引导等待时间
    sed -i 's/GRUB_TIMEOUT=.*/GRUB_TIMEOUT=2/g' /etc/default/grub
    grub2-mkconfig -o /boot/grub2/grub.cfg
    # 请根据具体情况来决定是否关闭防火墙
    systemctl stop firewalld
    systemctl disable  firewalld
}

function config_mirror_and_update() {
    MIRROR="https://mirrors.huaweicloud.com"
    #更换yum源
    cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    #curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s@baseurl=.*/centos@baseurl=$MIRROR/centos@g" /etc/yum.repos.d/CentOS-Base.repo
    yum makecache

    #同步时间
    yum install -y ntpdate
    ntpdate time.windows.com

    #配置EPEL源
    #EPEL (Extra Packages for Enterprise Linux) 是由 Fedora Special Interest Group 为企业 Linux 创建、维护和管理的一个高质量附加包集合,适用于但不仅限于 Red Hat Enterprise Linux (RHEL), CentOS, Scientific Linux (SL), Oracle Linux (OL)
    yum install -y epel-release
    cp /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
    mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
    # curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/epel.repo
    sed -i "s/metalink/#metalink/g" /etc/yum.repos.d/epel.repo
    sed -i "s@baseurl=.*/epel@baseurl=$MIRROR/epel@g" /etc/yum.repos.d/epel.repo

#配置ius源  https://ius.io/
#IUS只为RHEL和CentOS这两个发行版提供较新版本的rpm包。如果在os或epel找不到某个软件的新版rpm,软件官方又只提供源代码包的时候,可以来ius源中找,几乎都能找到。比如,python3.6(包括对应版本的pip,epel源里有python3.6但没有对应版本的pip),php7.2,redis5等等
# https://mirrors.aliyun.com  https://mirrors.tuna.tsinghua.edu.cn
    cat > /etc/yum.repos.d/ius.repo <<- "EOF"
[ius]
name=ius
baseurl=https://mirrors.aliyun.com/ius/stable/CentOS/7/$basearch
gpgcheck=0
enabled=1

EOF
    yum makecache
    yum update -y
#一些实用工具,这些大部分在EPEL源里
    yum install -y bash-completion git2u wget vim nano yum-utils unar screen lrzsz supervisor iotop iftop jnettop mytop apachetop atop htop ncdu nmap pv net-tools sl lynx links crudini the_silver_searcher tig cloc nload w3m axel tmux mc glances multitail redis5 lftp vsftpd
}

# 更新内核为主分支ml(mainline)版本
function update_kernel() {
    rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
    sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/elrepo.repo
    crudini --set /etc/yum.repos.d/elrepo.repo elrepo baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/elrepo/el7/\$basearch"
    crudini --set /etc/yum.repos.d/elrepo.repo elrepo-testing baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/testing/el7/\$basearch"
    crudini --set /etc/yum.repos.d/elrepo.repo elrepo-kernel baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/kernel/el7/\$basearch/"
    crudini --set /etc/yum.repos.d/elrepo.repo elrepo-extras baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/extras/el7/\$basearch/"
    yum-config-manager --enable elrepo-kernel
    yum install kernel-ml-devel kernel-ml -y
    KERNEL_VERSION=$(yum list kernel-ml | grep kernel.*@elrepo-kernel |  awk -F ' ' '{print $2}')
    GRUB_ITEM=$(awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg | grep ${KERNEL_VERSION})
    grub2-set-default '${GRUB_ITEM}'
    echo '请重启后执行uname -r查看是否生效'
}

function install_python() {
    # python3.6,包括对应版本的pip
    yum install python36u-pip -y
    # 使用国内pypi源,使用阿里云的源
    # 备选:http://pypi.douban.com/simple/  https://pypi.tuna.tsinghua.edu.cn/simple/  https://mirrors.aliyun.com/pypi/simple/
    mkdir -p ~/.pip
    cat > ~/.pip/pip.conf <<- "EOF"
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

EOF
    pip3.6 install --upgrade pip
    # 一些基于python的实用或者有意思的工具
    pip3.6 install cheat mycli icdiff you-get lolcat youtube-dl

}

function install_php() {
    yum install php72u* nginx -y
}

function install_nodejs_and_config() {
#配置nodejs的yum源,安装 nodejs (epel源里有nodejs,但版本比较老),使用清华大学的源
    yum install https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_10.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm -y
    cat > /etc/yum.repos.d/nodesource-el7.repo <<- "EOF"
[nodesource]
name=Node.js Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_10.x/el/7/$basearch
enabled=1
gpgcheck=0

[nodesource-source]
name=Node.js for Enterprise Linux 7 - $basearch - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_10.x/el/7/SRPMS
enabled=0
gpgcheck=1

EOF
    yum makecache
    yum install nodejs -y
    # 更换国内npm源
    npm config set registry https://mirrors.huaweicloud.com/repository/npm/
    # 备选:npm config set registry https://mirrors.huaweicloud.com/repository/npm/
    # npm config set registry https://registry.npm.taobao.org/
    npm cache clean -f
    # 一些基于nodejs的实用或者有意思的工具
    npm install --global n npm get-port-cli hasha-cli http-server

}

#命令行小游戏
function install_cmd_game() {
    # 2048
    curl https://raw.githubusercontent.com/mydzor/bash2048/master/bash2048.sh -o 2048.sh && chmod 755 2048.sh
    # 扫雷
    curl https://raw.githubusercontent.com/feherke/Bash-script/master/minesweeper/minesweeper.sh -o minesweeper.sh && chmod 755 minesweeper.sh
    # 俄罗斯方块
    git clone https://github.com/uuner/sedtris.git
    chmod 755 sedtris/*
    # ./sedtris/sedtris.sh
}

#安装jdk和tomcat
function install_jdk_and_tomcat() {
#安装openjdk
    yum install java-1.8.0-openjdk-devel.x86_64 -y

#或者 oraclejdk
# wget https://mirrors.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz
# tar -zxvf jdk-8u202-linux-x64.tar.gz -C /usr/
# cat >> /etc/profile <<- "EOF"
# export JAVA_HOME=/usr/jdk1.8.0_202
# export PATH=$JAVA_HOME/bin:$PATH
# export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

# EOF
# source /etc/profile

#安装tomcat9
#https://tomcat.apache.org/download-90.cgi 注:请随时关注官网的最新版本,新版本发布后旧版本的链接会失效!
    cd /usr
    TOMCAT_VERSION=$(lftp https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/ -e "cls;bye" | awk -F '/' '{print $1}' | awk -F 'v' '{print $2}')
    wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz
    tar -zxf apache-tomcat-${TOMCAT_VERSION}.tar.gz
    cat > /usr/lib/systemd/system/tomcat.service <<- "EOF"
[Unit]
Description=Tomcat9.0.17
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
Environment='CATALINA_OPTS=-Xms128M -Xmx512M -server -XX:+UseParallelGC'
WorkingDirectory=/usr/apache-tomcat-9.0.17

ExecStart=/usr/apache-tomcat-9.0.17/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

EOF
    sed -i "s:Description=Tomcat-.*:Description=Tomcat-${TOMCAT_VERSION}:g" /usr/lib/systemd/system/tomcat.service
    sed -i "s:WorkingDirectory=/usr/apache-tomcat-.*:WorkingDirectory=/usr/apache-tomcat-${TOMCAT_VERSION}:g" /usr/lib/systemd/system/tomcat.service
    sed -i "s:ExecStart=/usr/apache-tomcat-.*:ExecStart=/usr/apache-tomcat-${TOMCAT_VERSION}/bin/startup.sh:g" /usr/lib/systemd/system/tomcat.service
    systemctl daemon-reload
    systemctl start tomcat

}

#安装mysql5.7 http://mirrors.tuna.tsinghua.edu.cn/mysql,使用清华大学的源
function install_mysql_and_config() {
    cat > /etc/yum.repos.d/mysql-community.repo <<- "EOF"
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7
enabled=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

EOF

    yum install mysql-community-server -y
    #mysql配置
    if [[ "${MYSQL_PASSWORD}" == "" ]];then
    #root用户密码
    MYSQL_PASSWORD=1111
    fi
    systemctl start mysqld
    systemctl enable mysqld
    passlog=$(grep 'temporary password'  /var/log/mysqld.log)
    pass=${passlog:${#passlog}-12:${#passlog}}
    mysql -uroot -p"${pass}" -e"alter user root@localhost identified by 'QQQqqq111...' " --connect-expired-password
    pass=QQQqqq111...
    mysql -uroot -p"${pass}" -e"set global validate_password_policy=0;" --connect-expired-password
    mysql -uroot -p"${pass}" -e"set global validate_password_length=4;" --connect-expired-password
    mysql -uroot -p"${pass}" -e"set global validate_password_mixed_case_count=0;" --connect-expired-password
    mysql -uroot -p"${pass}" -e"set global validate_password_number_count=0;" --connect-expired-password
    #echo 'enter your mysql password'
    #read password
    mysql -uroot -p"${pass}" -e"set password=password('${MYSQL_PASSWORD}');" --connect-expired-password
    mysql -uroot -p"${MYSQL_PASSWORD}" -e"update mysql.user set host='%' where user='root';" --connect-expired-password
    mysql -uroot -p"${MYSQL_PASSWORD}" -e"flush privileges;" --connect-expired-password

}

#安装mongodb,使用清华大学的源
function install_mongodb() {
    echo "" > /etc/yum.repos.d/mongodb.repo
    for version in "3.0" "3.2" "3.4" "3.6" "4.0"; do
    cat >> /etc/yum.repos.d/mongodb.repo <<- EOF
[mongodb-org-$version]
name=MongoDB Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el7-$version/
gpgcheck=0
enabled=1

EOF
    done
    yum makecache
    yum install mongodb-org -y

}

#安装docker
function install_docker() {
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sed -i "s@https://download.docker.com@https://mirrors.aliyun.com/docker-ce@g"  /etc/yum.repos.d/docker-ce.repo
    yum install docker-ce -y
    systemctl start docker
#配置国内docker加速器
cat > /etc/docker/daemon.json <<- "EOF"
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
    systemctl restart docker
}
system_config
# 如果不指定参数,则执行所有功能模块
if [[ -z $* ]]; then
    config_mirror_and_update
    update_kernel
    install_python
    install_php
    install_nodejs_and_config
    install_cmd_game
    install_jdk_and_tomcat
    install_mysql_and_config
    install_mongodb
    install_docker
fi

for arg in $* ; do
    case ${arg} in
    base)
    config_mirror_and_update
    ;;
    kernel)
    config_mirror_and_update
    update_kernel
    ;;
    python)
    config_mirror_and_update
    install_python
    ;;
    php)
    config_mirror_and_update
    install_php
    ;;
    nodejs)
    config_mirror_and_update
    install_nodejs_and_config
    ;;
    cmd_game)
    config_mirror_and_update
    install_cmd_game
    ;;
    jdk)
    config_mirror_and_update
    install_jdk_and_tomcat
    ;;
    mysql)
    config_mirror_and_update
    install_mysql_and_config
    ;;
    mongodb)
    config_mirror_and_update
    install_mongodb
    ;;
    docker)
    config_mirror_and_update
    install_docker
    ;;
    esac
done

 

#!/bin/bash# curl https://yiyingcanfeng.github.io/centos-init.sh | bash# 可选参数base kernel python php nodejs cmd_game jdk mysql mongodb docker# 比如# curl https://yiyingcanfeng.github.io/centos-init.sh | bash -s base
function system_config() {    # 修改主机名    #hostnamectl set-hostname aaa    # 禁用selinux    sed -i 's/SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config    setenforce 0    # 修改开机引导等待时间    sed -i 's/GRUB_TIMEOUT=.*/GRUB_TIMEOUT=2/g' /etc/default/grub    grub2-mkconfig -o /boot/grub2/grub.cfg    # 请根据具体情况来决定是否关闭防火墙    systemctl stop firewalld    systemctl disable  firewalld}
function config_mirror_and_update() {    MIRROR="https://mirrors.huaweicloud.com"    #更换yum源    cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup    #curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo    sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/CentOS-Base.repo    sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/CentOS-Base.repo    sed -i "s@baseurl=.*/centos@baseurl=$MIRROR/centos@g" /etc/yum.repos.d/CentOS-Base.repo    yum makecache
    #同步时间    yum install -y ntpdate    ntpdate time.windows.com
    #配置EPEL源    #EPEL (Extra Packages for Enterprise Linux) 是由 Fedora Special Interest Group 为企业 Linux 创建、维护和管理的一个高质量附加包集合,适用于但不仅限于 Red Hat Enterprise Linux (RHEL), CentOS, Scientific Linux (SL), Oracle Linux (OL)    yum install -y epel-release    cp /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup    mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup    # curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo    sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/epel.repo    sed -i "s/metalink/#metalink/g" /etc/yum.repos.d/epel.repo    sed -i "s@baseurl=.*/epel@baseurl=$MIRROR/epel@g" /etc/yum.repos.d/epel.repo
#配置ius源  https://ius.io/#IUS只为RHEL和CentOS这两个发行版提供较新版本的rpm包。如果在os或epel找不到某个软件的新版rpm,软件官方又只提供源代码包的时候,可以来ius源中找,几乎都能找到。比如,python3.6(包括对应版本的pip,epel源里有python3.6但没有对应版本的pip),php7.2,redis5等等# https://mirrors.aliyun.com  https://mirrors.tuna.tsinghua.edu.cn    cat > /etc/yum.repos.d/ius.repo <<- "EOF"[ius]name=iusbaseurl=https://mirrors.aliyun.com/ius/stable/CentOS/7/$basearchgpgcheck=0enabled=1
EOF    yum makecache    yum update -y#一些实用工具,这些大部分在EPEL源里    yum install -y bash-completion git2u wget vim nano yum-utils unar screen lrzsz supervisor iotop iftop jnettop mytop apachetop atop htop ncdu nmap pv net-tools sl lynx links crudini the_silver_searcher tig cloc nload w3m axel tmux mc glances multitail redis5 lftp vsftpd}
# 更新内核为主分支ml(mainline)版本function update_kernel() {    rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org    rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm    sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/elrepo.repo    crudini --set /etc/yum.repos.d/elrepo.repo elrepo baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/elrepo/el7/\$basearch"    crudini --set /etc/yum.repos.d/elrepo.repo elrepo-testing baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/testing/el7/\$basearch"    crudini --set /etc/yum.repos.d/elrepo.repo elrepo-kernel baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/kernel/el7/\$basearch/"    crudini --set /etc/yum.repos.d/elrepo.repo elrepo-extras baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/extras/el7/\$basearch/"    yum-config-manager --enable elrepo-kernel    yum install kernel-ml-devel kernel-ml -y    KERNEL_VERSION=$(yum list kernel-ml | grep kernel.*@elrepo-kernel |  awk -F ' ' '{print $2}')    GRUB_ITEM=$(awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg | grep ${KERNEL_VERSION})    grub2-set-default '${GRUB_ITEM}'    echo '请重启后执行uname -r查看是否生效'}
function install_python() {    # python3.6,包括对应版本的pip    yum install python36u-pip -y    # 使用国内pypi源,使用阿里云的源    # 备选:http://pypi.douban.com/simple/  https://pypi.tuna.tsinghua.edu.cn/simple/  https://mirrors.aliyun.com/pypi/simple/    mkdir -p ~/.pip    cat > ~/.pip/pip.conf <<- "EOF"[global]index-url = https://mirrors.aliyun.com/pypi/simple/
[install]trusted-host=mirrors.aliyun.com
EOF    pip3.6 install --upgrade pip    # 一些基于python的实用或者有意思的工具    pip3.6 install cheat mycli icdiff you-get lolcat youtube-dl
}
function install_php() {    yum install php72u* nginx -y}
function install_nodejs_and_config() {#配置nodejs的yum源,安装 nodejs (epel源里有nodejs,但版本比较老),使用清华大学的源    yum install https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_10.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm -y    cat > /etc/yum.repos.d/nodesource-el7.repo <<- "EOF"[nodesource]name=Node.js Packages for Enterprise Linux 7 - $basearchbaseurl=https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_10.x/el/7/$basearchenabled=1gpgcheck=0
[nodesource-source]name=Node.js for Enterprise Linux 7 - $basearch - Sourcebaseurl=https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_10.x/el/7/SRPMSenabled=0gpgcheck=1
EOF    yum makecache    yum install nodejs -y    # 更换国内npm源    npm config set registry https://mirrors.huaweicloud.com/repository/npm/    # 备选:npm config set registry https://mirrors.huaweicloud.com/repository/npm/    # npm config set registry https://registry.npm.taobao.org/    npm cache clean -f    # 一些基于nodejs的实用或者有意思的工具    npm install --global n npm get-port-cli hasha-cli http-server
}
#命令行小游戏function install_cmd_game() {    # 2048    curl https://raw.githubusercontent.com/mydzor/bash2048/master/bash2048.sh -o 2048.sh && chmod 755 2048.sh    # 扫雷    curl https://raw.githubusercontent.com/feherke/Bash-script/master/minesweeper/minesweeper.sh -o minesweeper.sh && chmod 755 minesweeper.sh    # 俄罗斯方块    git clone https://github.com/uuner/sedtris.git    chmod 755 sedtris/*    # ./sedtris/sedtris.sh}
#安装jdk和tomcatfunction install_jdk_and_tomcat() {#安装openjdk    yum install java-1.8.0-openjdk-devel.x86_64 -y
#或者 oraclejdk# wget https://mirrors.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz# tar -zxvf jdk-8u202-linux-x64.tar.gz -C /usr/# cat >> /etc/profile <<- "EOF"# export JAVA_HOME=/usr/jdk1.8.0_202# export PATH=$JAVA_HOME/bin:$PATH# export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# EOF# source /etc/profile
#安装tomcat9#https://tomcat.apache.org/download-90.cgi 注:请随时关注官网的最新版本,新版本发布后旧版本的链接会失效!    cd /usr    TOMCAT_VERSION=$(lftp https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/ -e "cls;bye" | awk -F '/' '{print $1}' | awk -F 'v' '{print $2}')    wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz    tar -zxf apache-tomcat-${TOMCAT_VERSION}.tar.gz    cat > /usr/lib/systemd/system/tomcat.service <<- "EOF"[Unit]Description=Tomcat9.0.17After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]Type=forkingEnvironment='CATALINA_OPTS=-Xms128M -Xmx512M -server -XX:+UseParallelGC'WorkingDirectory=/usr/apache-tomcat-9.0.17
ExecStart=/usr/apache-tomcat-9.0.17/bin/startup.shExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true
[Install]WantedBy=multi-user.target
EOF    sed -i "s:Description=Tomcat-.*:Description=Tomcat-${TOMCAT_VERSION}:g" /usr/lib/systemd/system/tomcat.service    sed -i "s:WorkingDirectory=/usr/apache-tomcat-.*:WorkingDirectory=/usr/apache-tomcat-${TOMCAT_VERSION}:g" /usr/lib/systemd/system/tomcat.service    sed -i "s:ExecStart=/usr/apache-tomcat-.*:ExecStart=/usr/apache-tomcat-${TOMCAT_VERSION}/bin/startup.sh:g" /usr/lib/systemd/system/tomcat.service    systemctl daemon-reload    systemctl start tomcat
}
#安装mysql5.7 http://mirrors.tuna.tsinghua.edu.cn/mysql,使用清华大学的源function install_mysql_and_config() {    cat > /etc/yum.repos.d/mysql-community.repo <<- "EOF"[mysql-connectors-community]name=MySQL Connectors Communitybaseurl=http://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7enabled=1gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-community]name=MySQL Tools Communitybaseurl=http://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7enabled=1gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community]name=MySQL 5.7 Community Serverbaseurl=http://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7enabled=1gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]name=MySQL 8.0 Community Serverbaseurl=http://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7enabled=0gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
EOF
    yum install mysql-community-server -y    #mysql配置    if [[ "${MYSQL_PASSWORD}" == "" ]];then    #root用户密码    MYSQL_PASSWORD=1111    fi    systemctl start mysqld    systemctl enable mysqld    passlog=$(grep 'temporary password'  /var/log/mysqld.log)    pass=${passlog:${#passlog}-12:${#passlog}}    mysql -uroot -p"${pass}" -e"alter user root@localhost identified by 'QQQqqq111...' " --connect-expired-password    pass=QQQqqq111...    mysql -uroot -p"${pass}" -e"set global validate_password_policy=0;" --connect-expired-password    mysql -uroot -p"${pass}" -e"set global validate_password_length=4;" --connect-expired-password    mysql -uroot -p"${pass}" -e"set global validate_password_mixed_case_count=0;" --connect-expired-password    mysql -uroot -p"${pass}" -e"set global validate_password_number_count=0;" --connect-expired-password    #echo 'enter your mysql password'    #read password    mysql -uroot -p"${pass}" -e"set password=password('${MYSQL_PASSWORD}');" --connect-expired-password    mysql -uroot -p"${MYSQL_PASSWORD}" -e"update mysql.user set host='%' where user='root';" --connect-expired-password    mysql -uroot -p"${MYSQL_PASSWORD}" -e"flush privileges;" --connect-expired-password
}
#安装mongodb,使用清华大学的源function install_mongodb() {    echo "" > /etc/yum.repos.d/mongodb.repo    for version in "3.0" "3.2" "3.4" "3.6" "4.0"; do    cat >> /etc/yum.repos.d/mongodb.repo <<- EOF[mongodb-org-$version]name=MongoDB Repositorybaseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el7-$version/gpgcheck=0enabled=1
EOF    done    yum makecache    yum install mongodb-org -y
}
#安装dockerfunction install_docker() {    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo    sed -i "s@https://download.docker.com@https://mirrors.aliyun.com/docker-ce@g"  /etc/yum.repos.d/docker-ce.repo    yum install docker-ce -y    systemctl start docker#配置国内docker加速器cat > /etc/docker/daemon.json <<- "EOF"{  "registry-mirrors": ["https://registry.docker-cn.com"]}EOF    systemctl restart docker}system_config# 如果不指定参数,则执行所有功能模块if [[ -z $* ]]; then    config_mirror_and_update    update_kernel    install_python    install_php    install_nodejs_and_config    install_cmd_game    install_jdk_and_tomcat    install_mysql_and_config    install_mongodb    install_dockerfi
for arg in $* ; do    case ${arg} in    base)    config_mirror_and_update    ;;    kernel)    config_mirror_and_update    update_kernel    ;;    python)    config_mirror_and_update    install_python    ;;    php)    config_mirror_and_update    install_php    ;;    nodejs)    config_mirror_and_update    install_nodejs_and_config    ;;    cmd_game)    config_mirror_and_update    install_cmd_game    ;;    jdk)    config_mirror_and_update    install_jdk_and_tomcat    ;;    mysql)    config_mirror_and_update    install_mysql_and_config    ;;    mongodb)    config_mirror_and_update    install_mongodb    ;;    docker)    config_mirror_and_update    install_docker    ;;    esacdone

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!