200字
Unlock-Music Web 版部署教程
2025-12-22
2025-12-22

开源仓库地址:https://git.unlock-music.dev/um/web

使用以下脚本即可直接部署

#!/bin/bash
set -euo pipefail

# ==================== 配置区域(可根据需求修改) ====================
# 部署根目录
DEPLOY_DIR="/opt/unlock-music"
# Nginx 站点配置文件名
NGINX_CONF_NAME="unlock-music.conf"
# 监听端口(确保未被占用)
LISTEN_PORT="9200"
# 服务器域名(无域名则填 localhost 或服务器 IP)
SERVER_NAME="localhost"
# Node.js 版本(推荐 16+)
NODE_VERSION="16"
# ==================================================================

# 颜色输出函数
red_echo() { echo -e "\033[31m$1\033[0m"; }
green_echo() { echo -e "\033[32m$1\033[0m"; }
blue_echo() { echo -e "\033[34m$1\033[0m"; }
yellow_echo() { echo -e "\033[33m$1\033[0m"; }

# 检查是否为 root 用户
check_root() {
    if [ $EUID -ne 0 ]; then
        red_echo "错误:请使用 root 用户执行该脚本(sudo ./deploy_unlock_music.sh)"
        exit 1
    fi
}

# 识别操作系统
detect_os() {
    if [ -f /etc/redhat-release ]; then
        OS="centos"
    elif [ -f /etc/lsb-release ] || [ -f /etc/debian_version ]; then
        OS="ubuntu"
    else
        red_echo "错误:不支持当前操作系统,仅支持 CentOS/Ubuntu/Debian"
        exit 1
    fi
}

# 安装依赖(Node.js + Nginx + Git)
install_dependencies() {
    blue_echo "===== 开始安装系统依赖 ====="
    if [ "$OS" = "centos" ]; then
        # CentOS 配置
        yum update -y
        # 安装 EPEL 源
        yum install -y epel-release
        # 安装 Node.js(使用 Nodesource 源)
        curl -fsSL https://rpm.nodesource.com/setup_${NODE_VERSION}.x | bash -
        yum install -y nodejs git nginx
    else
        # Ubuntu/Debian 配置
        apt update -y
        # 安装 Node.js(使用 Nodesource 源)
        curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash -
        apt install -y nodejs git nginx
    fi
    # 验证依赖安装
    node -v && npm -v && nginx -v && git --version
    if [ $? -eq 0 ]; then
        green_echo "依赖安装完成"
    else
        red_echo "依赖安装失败,请手动排查"
        exit 1
    fi
}

# 创建部署目录
create_deploy_dir() {
    blue_echo "===== 创建部署目录 ====="
    mkdir -p ${DEPLOY_DIR}/{src,dist,logs}
    chmod -R 755 ${DEPLOY_DIR}
    green_echo "部署目录创建完成:${DEPLOY_DIR}"
}

# 拉取 unlock-music 源码
pull_source_code() {
    blue_echo "===== 拉取 unlock-music 源码 ====="
    cd ${DEPLOY_DIR}/src
    # 若已存在源码,先拉取最新版本
    if [ -d ".git" ]; then
        git pull
    else
        git clone https://git.unlock-music.dev/um/web.git .
    fi
    green_echo "源码拉取/更新完成"
}

# 构建项目
build_project() {
    blue_echo "===== 开始构建项目 ====="
    cd ${DEPLOY_DIR}/src
    # 安装项目依赖(使用国内镜像加速)
    npm install --registry=https://registry.npmmirror.com
    # 构建项目
    npm run build
    # 复制构建产物到 dist 目录
    rm -rf ${DEPLOY_DIR}/dist/*
    cp -r ${DEPLOY_DIR}/src/dist/* ${DEPLOY_DIR}/dist/
    green_echo "项目构建完成,产物已复制到 ${DEPLOY_DIR}/dist"
}

# 配置 Nginx 站点
configure_nginx() {
    blue_echo "===== 配置 Nginx 站点 ====="
    # 删除原有同名配置
    rm -f /etc/nginx/conf.d/${NGINX_CONF_NAME}
    # 写入 Nginx 配置
    cat > /etc/nginx/conf.d/${NGINX_CONF_NAME} << EOF
server {
    listen ${LISTEN_PORT};
    server_name ${SERVER_NAME};
    root ${DEPLOY_DIR}/dist;
    index index.html index.htm;

    # 支持前端路由刷新
    location / {
        try_files \$uri \$uri/ /index.html;
    }

    # 允许大文件上传(解密音频文件,可根据需求调整)
    client_max_body_size 100m;

    # 日志配置
    access_log ${DEPLOY_DIR}/logs/access.log;
    error_log ${DEPLOY_DIR}/logs/error.log;
}
EOF
    # 验证 Nginx 配置
    nginx -t
    if [ $? -eq 0 ]; then
        # 重启 Nginx 生效
        if [ "$OS" = "centos" ]; then
            systemctl restart nginx
            systemctl enable nginx
        else
            service nginx restart
            update-rc.d nginx enable
        fi
        green_echo "Nginx 配置完成,已重启 Nginx 服务"
    else
        red_echo "Nginx 配置语法错误,请手动排查"
        exit 1
    fi
}

# 配置防火墙(开放监听端口)
configure_firewall() {
    blue_echo "===== 配置防火墙,开放 ${LISTEN_PORT} 端口 ====="
    if [ "$OS" = "centos" ]; then
        # CentOS 防火墙配置
        if systemctl is-active --quiet firewalld; then
            firewall-cmd --permanent --add-port=${LISTEN_PORT}/tcp
            firewall-cmd --reload
            green_echo "Firewalld 已开放 ${LISTEN_PORT} 端口"
        fi
    else
        # Ubuntu/Debian 防火墙配置(ufw)
        if command -v ufw &> /dev/null; then
            ufw allow ${LISTEN_PORT}/tcp
            ufw reload
            green_echo "UFW 已开放 ${LISTEN_PORT} 端口"
        fi
    fi
}

# 输出部署完成信息
print_success_info() {
    green_echo "===== unlock-music Web 版部署完成 ====="
    green_echo "部署目录:${DEPLOY_DIR}"
    green_echo "访问地址:http://${SERVER_NAME}:${LISTEN_PORT}"
    green_echo "日志目录:${DEPLOY_DIR}/logs"
    green_echo "若无法访问,请检查:1. 端口是否开放 2. Nginx 是否正常运行 3. 服务器安全组配置"
}

# 主流程
main() {
    blue_echo "===== 开始部署 unlock-music Web 版 ====="
    check_root
    detect_os
    install_dependencies
    create_deploy_dir
    pull_source_code
    build_project
    configure_nginx
    configure_firewall
    print_success_info
}

# 执行主流程
main "$@"

1. 创建并保存脚本

# 创建脚本文件
vim deploy_unlock_music.sh

# 将上述脚本完整复制粘贴到文件中,保存退出(Esc → :wq → 回车)

2. 赋予执行权限

chmod +x deploy_unlock_music.sh

3. 执行部署脚本

# 直接执行(需 root 权限,若无 root 则加 sudo)
./deploy_unlock_music.sh
# 或
sudo ./deploy_unlock_music.sh

4. 自定义配置(可选)

若需修改部署目录、监听端口等,可编辑脚本顶部的「配置区域」:

  • DEPLOY_DIR:部署根目录(默认 /opt/unlock-music

  • LISTEN_PORT:监听端口(默认 9200,若被占用可改为 8080 等)

  • SERVER_NAME:服务器域名(无域名则填服务器公网 IP)

  • NODE_VERSION:Node.js 版本(默认 16,推荐 16+)

打开本地浏览器,输入 http://<服务器IP或域名>:<LISTEN_PORT>(如 http://192.168.1.100:9200),能看到 unlock-music 网页界面即为部署成功。

常见问题排查

  1. 脚本执行权限不足:解决方案:执行 chmod +x deploy_unlock_music.sh 赋予执行权限。

  2. Node.js 安装失败:解决方案:手动安装 Node.js 16+,或修改脚本中的 NODE_VERSION 为兼容版本。

  3. 端口被占用:解决方案:修改脚本中的 LISTEN_PORT 为未被占用的端口(如 9201),重新执行脚本。

  4. Nginx 启动失败:解决方案:查看 Nginx 错误日志(${DEPLOY_DIR}/logs/error.log),或执行 nginx -t 验证配置语法。

  5. 浏览器无法访问:解决方案:

    • 检查服务器防火墙 / 安全组是否开放对应端口;

    • 验证 SERVER_NAMELISTEN_PORT 配置是否正确;

    • 执行 curl http://localhost:${LISTEN_PORT} 测试本地访问是否正常

后续维护

  1. 更新源码:重新执行部署脚本即可自动拉取最新源码并重建项目:

    ./deploy_unlock_music.sh
    
  2. 停止 / 重启服务

    # CentOS
    systemctl stop nginx
    systemctl restart nginx
    
    # Ubuntu/Debian
    service nginx stop
    service nginx restart
    
  3. 查看日志

    # 访问日志
    tail -f ${DEPLOY_DIR}/logs/access.log
    
    # 错误日志
    tail -f ${DEPLOY_DIR}/logs/error.log

也可直接使用以下仓库

根据系统下载对应的本地工具即可

国内访问:https://gitcode.com/gh_mirrors/qm/QMCDecode

项目地址:https://github.com/gongjiehong/QMCDecode

评论