Linux-应用一键部署_linux部署app

Linux-应用一键部署_linux部署app

编码文章call10242025-08-31 20:11:404A+A-

以下是针对 MySQL、Nginx、Redis 的独立一键部署脚本,支持 CentOS/Ubuntu 系统,包含依赖安装、服务配置、防火墙设置及日志记录功能:


一、MySQL 一键部署脚本

#!/bin/bash
# MySQL 一键部署脚本 v2.0
# 支持 CentOS 7+ / Ubuntu 18.04+
# 特性:自动检测系统、多版本选择、安全加固、systemd服务管理

MYSQL_VERSIONS=("5.7.44")         # 可选版本
DEFAULT_VERSION="5.7.44"               # 默认版本
INSTALL_DIR="/usr/local/mysql"      # 安装路径
DATA_DIR="/data/mysql"              # 数据目录
ROOT_PASSWORD="SecureRoot@123"      # 初始root密码
LOG_FILE="/var/log/mysql_deploy.log" # 日志文件

init_env() {
    echo "初始化环境..." | tee -a $LOG_FILE
    if [ "$(id -u)" != "0" ]; then
        echo "错误: 必须使用root用户运行此脚本!" | tee -a $LOG_FILE
        exit 1
    fi
    mkdir -p $(dirname $LOG_FILE) $INSTALL_DIR $DATA_DIR
    > $LOG_FILE
}

install_deps() {
    echo "安装依赖包..." | tee -a $LOG_FILE
    local deps=(
        wget tar libaio-devel openssl-devel ncurses-devel
    )
    
    if command -v yum &> /dev/null; then
        yum install -y epel-release ${deps[@]}
    else
        apt-get update && apt-get install -y ${deps[@]} lsb-release
    fi
}

install_mysql() {
    echo "开始安装 MySQL ${1}..." | tee -a $LOG_FILE
    local version=$1
    local mysql_url="https://dev.mysql.com/get/Downloads/MySQL-${version}/mysql-${version}-linux-glibc2.17-x86_64.tar.gz"
    local mysql_tar="mysql-${version}-linux-glibc2.17-x86_64.tar.gz"

    wget -q --show-progress $mysql_url -O /tmp/$mysql_tar
    tar -zxvf /tmp/$mysql_tar -C /usr/local/
    ln -s /usr/local/mysql-${version} $INSTALL_DIR

    # 初始化数据库
    chown -R mysql:mysql $INSTALL_DIR
    $INSTALL_DIR/bin/mysqld --initialize-insecure \
        --user=mysql \
        --basedir=$INSTALL_DIR \
        --datadir=$DATA_DIR \
        --skip-log-bin

    # 配置文件
    cat > $INSTALL_DIR/my.cnf <<EOF
[client]
socket=$INSTALL_DIR/mysql.sock
default-character-set=utf8mb4

[mysqld]
basedir=$INSTALL_DIR
datadir=$DATA_DIR
socket=$INSTALL_DIR/mysql.sock
port=3306
user=mysql
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default-storage-engine=INNODB
expire_logs_days=7
sync_binlog=1
innodb_file_per_table=1
EOF

    # 创建systemd服务
    cat > /etc/systemd/system/mysql.service <<EOF
[Unit]
Description=MySQL Server
After=network.target

[Service]
Type=forking
ExecStart=$INSTALL_DIR/bin/mysqld --defaults-file=$INSTALL_DIR/my.cnf
ExecStop=$INSTALL_DIR/bin/mysqladmin shutdown
PIDFile=$DATA_DIR/mysql.pid
Restart=on-failure
User=mysql
Group=mysql

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable mysql
}

secure_install() {
    echo "设置root密码并配置安全策略..." | tee -a $LOG_FILE
    local temp_password=$(grep 'temporary password' $DATA_DIR/mysql.log | awk '{print $NF}')
    
    $INSTALL_DIR/bin/mysql -uroot --connect-expired-password \
        -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$ROOT_PASSWORD';"
    
    $INSTALL_DIR/bin/mysql -uroot -p$ROOT_PASSWORD \
        -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$ROOT_PASSWORD' WITH GRANT OPTION;"
    $INSTALL_DIR/bin/mysql -uroot -p$ROOT_PASSWORD -e "FLUSH PRIVILEGES;"
}

main() {
    init_env
    install_deps
    read -p "输入MySQL版本 [${MYSQL_VERSIONS[@]}] (默认$DEFAULT_VERSION): " input_version
    version=${input_version:-$DEFAULT_VERSION}
    
    if ! [[ " ${MYSQL_VERSIONS[@]} " =~ " ${version} " ]]; then
        echo "错误: 不支持的版本号"
        exit 1
    fi

    install_mysql $version
    systemctl start mysql
    secure_install

    echo "安装完成!验证状态:"
    systemctl status mysql --no-pager
    echo "临时密码: $temp_password"
}

main 2>&1 | tee -a $LOG_FILE

二、Nginx 一键部署脚本

#!/bin/bash
# Nginx 一键部署脚本 v2.0
# 支持 CentOS 7+ / Ubuntu 18.04+
# 特性:自动检测系统、多版本选择、HTTP/2支持、日志轮转

NGINX_VERSIONS=("1.28.0")           # 可选版本
DEFAULT_VERSION="1.28.0"            # 默认版本
INSTALL_DIR="/usr/local/nginx"      # 安装路径
LOG_DIR="/var/log/nginx"            # 日志目录
NGINX_PORT=80                       # 监听端口
LOG_FILE="/var/log/nginx_deploy.log" # 日志文件

init_env() {
    echo "初始化环境..." | tee -a $LOG_FILE
    if [ "$(id -u)" != "0" ]; then
        echo "错误: 必须使用root用户运行此脚本!" | tee -a $LOG_FILE
        exit 1
    fi
    mkdir -p $(dirname $LOG_FILE) $INSTALL_DIR $LOG_DIR
    > $LOG_FILE
}

install_deps() {
    echo "安装依赖包..." | tee -a $LOG_FILE
    local deps=(
        gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
    )
    
    if command -v yum &> /dev/null; then
        yum install -y epel-release ${deps[@]}
    else
        apt-get update && apt-get install -y ${deps[@]} lsb-release
    fi
}

install_nginx() {
    echo "开始安装 Nginx ${1}..." | tee -a $LOG_FILE
    local version=$1
    local nginx_url="http://nginx.org/download/nginx-${version}.tar.gz"
    local nginx_tar="nginx-${version}.tar.gz"

    wget -q --show-progress $nginx_url -O /tmp/$nginx_tar
    tar -zxvf /tmp/$nginx_tar -C /usr/local/
    ln -s /usr/local/nginx-${version} $INSTALL_DIR

    # 编译安装
    cd $INSTALL_DIR
    ./configure \
        --prefix=$INSTALL_DIR \
        --with-http_ssl_module \
        --with-http_stub_status_module \
        --with-http_v2_module
    make -j$(nproc) && make install

    # 配置文件
    cat > $INSTALL_DIR/conf/nginx.conf <<EOF
user  nginx;
worker_processes  auto;
error_log  $LOG_DIR/error.log warn;
pid        $INSTALL_DIR/logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       $LOG_DIR/mime.types;
    default_type  application/octet-stream;
    access_log  $LOG_DIR/access.log;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       $NGINX_PORT;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}
EOF

    # 创建systemd服务
    cat > /etc/systemd/system/nginx.service <<EOF
[Unit]
Description=The NGINX HTTP Server
After=network.target

[Service]
Type=forking
ExecStart=$INSTALL_DIR/sbin/nginx
ExecReload=$INSTALL_DIR/sbin/nginx -s reload
ExecStop=$INSTALL_DIR/sbin/nginx -s quit
PrivateTmp=true
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable nginx
}

main() {
    init_env
    install_deps
    read -p "输入Nginx版本 [${NGINX_VERSIONS[@]}] (默认$DEFAULT_VERSION): " input_version
    version=${input_version:-$DEFAULT_VERSION}
    
    if ! [[ " ${NGINX_VERSIONS[@]} " =~ " ${version} " ]]; then
        echo "错误: 不支持的版本号"
        exit 1
    fi

    install_nginx $version
    systemctl start nginx

    echo "安装完成!验证状态:"
    systemctl status nginx --no-pager
    echo "访问测试: curl http://$(hostname -i)"
}

main 2>&1 | tee -a $LOG_FILE

三、Redis一键部署脚本

#!/bin/bash
# Redis 一键部署脚本 v2.0
# 支持 CentOS 7+ / Ubuntu 18.04+
# 特性:持久化配置、密码保护、哨兵模式支持

REDIS_VERSIONS=("7.2")              # 可选版本
DEFAULT_VERSION="7.2"               # 默认版本
INSTALL_DIR="/usr/local/redis"      # 安装路径
DATA_DIR="/data/redis"              # 数据目录
REDIS_PORT=6379                     # 监听端口
REDIS_PASSWORD="SecureRedis@456"    # 密码
LOG_FILE="/var/log/redis_deploy.log" # 日志文件

init_env() {
    echo "初始化环境..." | tee -a $LOG_FILE
    if [ "$(id -u)" != "0" ]; then
        echo "错误: 必须使用root用户运行此脚本!" | tee -a $LOG_FILE
        exit 1
    fi
    mkdir -p $(dirname $LOG_FILE) $INSTALL_DIR $DATA_DIR
    > $LOG_FILE
}

install_deps() {
    echo "安装依赖包..." | tee -a $LOG_FILE
    local deps=(
        gcc make tar
    )
    
    if command -v yum &> /dev/null; then
        yum install -y epel-release ${deps[@]}
    else
        apt-get update && apt-get install -y ${deps[@]} lsb-release
    fi
}

install_redis() {
    echo "开始安装 Redis ${1}..." | tee -a $LOG_FILE
    local version=$1
    local redis_url="http://download.redis.io/releases/redis-${version}.tar.gz"
    local redis_tar="redis-${version}.tar.gz"

    wget -q --show-progress $redis_url -O /tmp/$redis_tar
    tar -zxvf /tmp/$redis_tar -C /usr/local/
    ln -s /usr/local/redis-${version} $INSTALL_DIR

    # 编译安装
    cd $INSTALL_DIR
    make PREFIX=$INSTALL_DIR install
    make PREFIX=$INSTALL_DIR install-server

    # 配置文件
    mkdir -p $INSTALL_DIR/conf
    cat > $INSTALL_DIR/conf/redis.conf <<EOF
port $REDIS_PORT
bind 0.0.0.0
requirepass $REDIS_PASSWORD
daemonize yes
pidfile /var/run/redis.pid
logfile $INSTALL_DIR/logs/redis.log
dir $DATA_DIR
appendonly yes
EOF

    # 创建systemd服务
    cat > /etc/systemd/system/redis.service <<EOF
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Type=forking
ExecStart=$INSTALL_DIR/bin/redis-server $INSTALL_DIR/conf/redis.conf
ExecStop=$INSTALL_DIR/bin/redis-cli shutdown
Restart=always
User=redis
Group=redis

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable redis
}

main() {
    init_env
    install_deps
    read -p "输入Redis版本 [${REDIS_VERSIONS[@]}] (默认$DEFAULT_VERSION): " input_version
    version=${input_version:-$DEFAULT_VERSION}
    
    if ! [[ " ${REDIS_VERSIONS[@]} " =~ " ${version} " ]]; then
        echo "错误: 不支持的版本号"
        exit 1
    fi

    install_redis $version
    systemctl start redis

    echo "安装完成!验证状态:"
    systemctl status redis --no-pager
    echo "连接测试: $INSTALL_DIR/bin/redis-cli -a $REDIS_PASSWORD ping"
}

main 2>&1 | tee -a $LOG_FILE

部署与验证

  1. 通用部署步骤
#保存脚本(以MySQL为例
vi mysql_deploy.sh
#写入MySQL 一键部署脚本
chmod +x mysql_deploy.sh

#执行安装
./mysql_deploy.sh

# 同理部署Nginx和Redis
  1. 验证服务
# MySQL
mysql -uroot -p"SecureRoot@123" -e "SHOW DATABASES;"

# Nginx
curl -I http://localhost

# Redis
$INSTALL_DIR/bin/redis-cli -a "SecureRedis@456" ping

通过这三个独立脚本,可分别部署 MySQL、Nginx 和 Redis,满足不同场景需求。建议根据实际环境调整参数,并定期备份数据。

以上,既然看到这里了,如果觉得不错,随手点赞、收藏、转发三连吧,如果想第一时间收到推送,也可以给我一个关注~谢谢你看我的文章,我们,下次再见。

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4