diff --git a/install.sh b/install.sh index b8c047c..01c7e8c 100644 --- a/install.sh +++ b/install.sh @@ -34,6 +34,9 @@ ADMIN_PASSWORD="" DOMAIN="" USE_DOMAIN=false SSL_METHOD="" +HTTP_PORT="80" +HTTPS_PORT="443" +BACKEND_PORT="40001" ################################################################################ # 工具函数 @@ -532,6 +535,135 @@ install_pm2() { print_success "PM2 安装完成" } +################################################################################ +# 端口检测和配置 +################################################################################ + +check_port_available() { + local port=$1 + if command -v netstat &> /dev/null; then + if netstat -tuln | grep -q ":${port} "; then + return 1 # 端口被占用 + fi + elif command -v ss &> /dev/null; then + if ss -tuln | grep -q ":${port} "; then + return 1 # 端口被占用 + fi + fi + return 0 # 端口可用 +} + +configure_ports() { + print_step "端口配置" + echo "" + + # 检测80端口 + if ! check_port_available 80; then + print_warning "检测到 80 端口已被占用" + echo "" + echo "80端口被其他服务占用,您可以:" + echo -e "${GREEN}[1]${NC} 使用其他HTTP端口 (例如: 8080, 8888)" + echo -e "${GREEN}[2]${NC} 停止占用80端口的服务" + echo "" + + read -p "请选择 [1-2]: " port_choice < /dev/tty + + if [[ "$port_choice" == "1" ]]; then + while true; do + read -p "请输入HTTP端口 [建议: 8080]: " custom_port < /dev/tty + custom_port=${custom_port:-8080} + + if [[ ! "$custom_port" =~ ^[0-9]+$ ]] || [[ $custom_port -lt 1024 ]] || [[ $custom_port -gt 65535 ]]; then + print_error "端口范围: 1024-65535" + continue + fi + + if ! check_port_available $custom_port; then + print_error "端口 $custom_port 已被占用,请选择其他端口" + continue + fi + + HTTP_PORT=$custom_port + print_success "将使用 HTTP 端口: $HTTP_PORT" + break + done + else + print_info "请手动停止占用80端口的服务后重新运行此脚本" + echo "" + print_info "查看端口占用: netstat -tunlp | grep :80" + print_info "或者: ss -tunlp | grep :80" + exit 1 + fi + else + print_success "80 端口可用" + fi + + # 检测443端口(仅在使用HTTPS时需要) + if [[ "$USE_DOMAIN" == "true" ]] && [[ "$SSL_METHOD" != "8" ]]; then + if ! check_port_available 443; then + print_warning "检测到 443 端口已被占用" + echo "" + + while true; do + read -p "请输入HTTPS端口 [建议: 8443]: " custom_https_port < /dev/tty + custom_https_port=${custom_https_port:-8443} + + if [[ ! "$custom_https_port" =~ ^[0-9]+$ ]] || [[ $custom_https_port -lt 1024 ]] || [[ $custom_https_port -gt 65535 ]]; then + print_error "端口范围: 1024-65535" + continue + fi + + if ! check_port_available $custom_https_port; then + print_error "端口 $custom_https_port 已被占用,请选择其他端口" + continue + fi + + HTTPS_PORT=$custom_https_port + print_success "将使用 HTTPS 端口: $HTTPS_PORT" + break + done + else + print_success "443 端口可用" + fi + fi + + # 检测后端端口 + if ! check_port_available 40001; then + print_warning "检测到 40001 端口已被占用" + echo "" + + while true; do + read -p "请输入后端服务端口 [建议: 40002]: " custom_backend_port < /dev/tty + custom_backend_port=${custom_backend_port:-40002} + + if [[ ! "$custom_backend_port" =~ ^[0-9]+$ ]] || [[ $custom_backend_port -lt 1024 ]] || [[ $custom_backend_port -gt 65535 ]]; then + print_error "端口范围: 1024-65535" + continue + fi + + if ! check_port_available $custom_backend_port; then + print_error "端口 $custom_backend_port 已被占用,请选择其他端口" + continue + fi + + BACKEND_PORT=$custom_backend_port + print_success "将使用后端端口: $BACKEND_PORT" + break + done + else + print_success "40001 端口可用" + fi + + echo "" + print_info "端口配置摘要:" + echo " - HTTP端口: $HTTP_PORT" + if [[ "$USE_DOMAIN" == "true" ]] && [[ "$SSL_METHOD" != "8" ]]; then + echo " - HTTPS端口: $HTTPS_PORT" + fi + echo " - 后端端口: $BACKEND_PORT" + echo "" +} + ################################################################################ # 访问模式选择 ################################################################################ @@ -977,7 +1109,7 @@ DATABASE_PATH=./data/database.db STORAGE_ROOT=./storage # 服务端口 -PORT=40001 +PORT=${BACKEND_PORT} # 环境 NODE_ENV=production @@ -1028,7 +1160,7 @@ configure_nginx_http() { cat > /etc/nginx/sites-available/${PROJECT_NAME}.conf << EOF server { - listen 80; + listen ${HTTP_PORT}; server_name ${server_name}; # 前端静态文件 @@ -1040,7 +1172,7 @@ server { # 后端API location /api { - proxy_pass http://localhost:40001; + proxy_pass http://localhost:${BACKEND_PORT}; proxy_http_version 1.1; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection 'upgrade'; @@ -1080,13 +1212,13 @@ EOF configure_nginx_https() { cat > /etc/nginx/sites-available/${PROJECT_NAME}.conf << EOF server { - listen 80; + listen ${HTTP_PORT}; server_name ${DOMAIN}; - return 301 https://\$server_name\$request_uri; + return 301 https://\$server_name:\${HTTPS_PORT}\$request_uri; } server { - listen 443 ssl http2; + listen ${HTTPS_PORT} ssl http2; server_name ${DOMAIN}; ssl_certificate /etc/nginx/ssl/${DOMAIN}.crt; @@ -1105,7 +1237,7 @@ server { # 后端API location /api { - proxy_pass http://localhost:40001; + proxy_pass http://localhost:${BACKEND_PORT}; proxy_http_version 1.1; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection 'upgrade'; @@ -1174,8 +1306,8 @@ health_check() { fi # 检查端口 - if netstat -tunlp | grep -q ":40001"; then - print_success "后端端口监听正常 (40001)" + if netstat -tunlp | grep -q ":${BACKEND_PORT}"; then + print_success "后端端口监听正常 (${BACKEND_PORT})" else print_error "后端端口监听异常" return 1 @@ -1225,19 +1357,42 @@ print_completion() { # 访问地址 if [[ "$USE_DOMAIN" == "true" ]]; then if [[ "$SSL_METHOD" == "8" ]]; then - echo -e "${CYAN}访问地址:${NC} http://${DOMAIN}" + if [[ "$HTTP_PORT" == "80" ]]; then + echo -e "${CYAN}访问地址:${NC} http://${DOMAIN}" + else + echo -e "${CYAN}访问地址:${NC} http://${DOMAIN}:${HTTP_PORT}" + fi else - echo -e "${CYAN}访问地址:${NC} https://${DOMAIN}" + if [[ "$HTTPS_PORT" == "443" ]]; then + echo -e "${CYAN}访问地址:${NC} https://${DOMAIN}" + else + echo -e "${CYAN}访问地址:${NC} https://${DOMAIN}:${HTTPS_PORT}" + fi fi else PUBLIC_IP=$(curl -s ifconfig.me || curl -s icanhazip.com || echo "服务器IP") - echo -e "${CYAN}访问地址:${NC} http://${PUBLIC_IP}" + if [[ "$HTTP_PORT" == "80" ]]; then + echo -e "${CYAN}访问地址:${NC} http://${PUBLIC_IP}" + else + echo -e "${CYAN}访问地址:${NC} http://${PUBLIC_IP}:${HTTP_PORT}" + fi fi echo -e "${CYAN}管理员账号:${NC} ${ADMIN_USERNAME}" echo -e "${CYAN}管理员密码:${NC} ********" echo "" + # 端口信息 + if [[ "$HTTP_PORT" != "80" ]] || [[ "$HTTPS_PORT" != "443" ]] || [[ "$BACKEND_PORT" != "40001" ]]; then + echo -e "${YELLOW}端口配置:${NC}" + echo " HTTP端口: $HTTP_PORT" + if [[ "$USE_DOMAIN" == "true" ]] && [[ "$SSL_METHOD" != "8" ]]; then + echo " HTTPS端口: $HTTPS_PORT" + fi + echo " 后端端口: $BACKEND_PORT" + echo "" + fi + # 常用命令 echo -e "${YELLOW}常用命令:${NC}" echo " 查看服务状态: pm2 status" @@ -1570,6 +1725,9 @@ main() { # 选择访问模式 choose_access_mode + # 端口配置 + configure_ports + # 配置管理员账号 configure_admin_account diff --git a/一键部署命令.txt b/一键部署命令.txt new file mode 100644 index 0000000..788176f --- /dev/null +++ b/一键部署命令.txt @@ -0,0 +1,119 @@ +╔═══════════════════════════════════════════════════════════════╗ +║ ║ +║ 🌩️ 玩玩云 - 一键部署命令 ║ +║ ║ +╚═══════════════════════════════════════════════════════════════╝ + +📦 项目地址: +https://gitee.com/yu-yon/vue-driven-cloud-storage + + +🚀 一键安装命令(在服务器上执行): + +方式1 (推荐): +curl -fsSL https://gitee.com/yu-yon/vue-driven-cloud-storage/raw/master/install.sh | bash + +方式2: +wget -qO- https://gitee.com/yu-yon/vue-driven-cloud-storage/raw/master/install.sh | bash + + +🗑️ 一键卸载命令(在服务器上执行): + +方式1: +curl -fsSL https://gitee.com/yu-yon/vue-driven-cloud-storage/raw/master/install.sh | bash -s -- --uninstall + +方式2: +wget -qO- https://gitee.com/yu-yon/vue-driven-cloud-storage/raw/master/install.sh | bash -s -- --uninstall + +方式3 (如果已下载脚本): +bash install.sh --uninstall + + +✅ v1.1.3 更新说明: +- 集成一键卸载功能到主脚本 +- 双重确认机制防止误删 +- 可选数据备份功能 +- 完整清理项目文件、PM2进程、Nginx配置、SSL证书 +- 保留系统环境(Node.js、Nginx、PM2、编译工具) +- 智能残留检查 + +✅ v1.1.2 更新说明: +- 修复npm依赖安装失败问题 +- 降级better-sqlite3以兼容Node.js 18.x+ +- 自动安装C++编译工具(gcc-c++, make, python3) +- 确保native模块编译成功 + +✅ v1.1.1 更新说明: +- 升级到 Node.js 20.x LTS(长期支持版本) +- 解决 Node.js 18.x 即将停止支持的警告问题 +- 更长的官方支持周期(至2026年4月) +- 更好的性能和安全性 + +✅ v1.1.0 更新说明: +- 新增支持 Rocky Linux、AlmaLinux、Fedora、openSUSE +- 实现统一包管理器检测,自动适配不同系统 +- 优化阿里云镜像源配置,支持更多发行版 +- 改进系统兼容性,未识别系统可自动检测包管理器 +- 全面支持 APT、YUM、DNF、Zypper 包管理器 + + +✨ 支持的操作系统: +- ✅ Ubuntu 18.04 / 20.04 / 22.04 / 24.04 +- ✅ Debian 10 / 11 / 12 +- ✅ CentOS 7 / 8 +- ✅ RHEL 7 / 8 / 9 +- ✅ Rocky Linux 8 / 9 +- ✅ AlmaLinux 8 / 9 +- ✅ Fedora 35+ +- ✅ openSUSE Leap +- ✅ 其他基于以上系统的发行版 + + +✨ 脚本特性: +- ✅ 完全自动化部署,无需手动操作 +- ✅ 支持 Ubuntu/Debian/CentOS/RHEL/Rocky/AlmaLinux/Fedora/openSUSE +- ✅ 自动检测系统并安装对应依赖 +- ✅ 自动安装 Node.js、Nginx、PM2 等依赖 +- ✅ 提供官方源和阿里云镜像源选择 +- ✅ 支持域名/IP 两种访问模式 +- ✅ 6种SSL证书自动部署方案 +- ✅ 智能容错和重试机制 +- ✅ 健康检查和完整提示 + + +📋 用户只需: +1. 选择软件源(官方/阿里云) +2. 选择访问模式(域名/IP) +3. 如果是域名模式,选择SSL证书方案 +4. 输入管理员账号和密码 +5. 等待自动部署完成 + + +💡 使用示例: + +1. 登录服务器(SSH) +2. 复制一键安装命令并执行 +3. 按照提示输入选项(例如输入 2 选择阿里云源) +4. 等待部署完成 +5. 访问您的域名或IP地址 + + +📖 详细文档: +https://gitee.com/yu-yon/vue-driven-cloud-storage/blob/master/INSTALL_GUIDE.md + + +🔧 部署后管理: +- 查看服务状态: pm2 status +- 查看日志: pm2 logs wanwanyun-backend +- 重启服务: pm2 restart wanwanyun-backend +- 配置文件: /var/www/wanwanyun/backend/.env + + +📞 获取帮助: +- 提交Issue: https://gitee.com/yu-yon/vue-driven-cloud-storage/issues +- 查看文档: 项目README.md + + +═══════════════════════════════════════════════════════════════ + +祝您使用愉快! 🎉