install.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/bash
  2. # ========================================
  3. # DHCP & DNS 管理器 - Linux 安装脚本(修复版)
  4. # ========================================
  5. set -e
  6. INSTALL_DIR="/opt/dhcp-dns-manager"
  7. SERVICE_NAME="dhcp-dns-manager"
  8. echo "========================================"
  9. echo " DHCP & DNS 管理器 - Linux 安装脚本"
  10. echo "========================================"
  11. echo ""
  12. # 检查是否以 root 运行
  13. if [ "$EUID" -ne 0 ]; then
  14. echo "❌ 请使用 sudo 运行此脚本"
  15. echo " sudo ./install.sh"
  16. exit 1
  17. fi
  18. # 检测系统
  19. if [ -f /etc/debian_version ]; then
  20. OS="debian"
  21. echo "✓ 检测到 Debian/Ubuntu 系统"
  22. elif [ -f /etc/redhat-release ]; then
  23. OS="redhat"
  24. echo "✓ 检测到 RHEL/CentOS 系统"
  25. else
  26. OS="unknown"
  27. echo "⚠ 未识别的 Linux 发行版,尝试通用安装"
  28. fi
  29. # 安装依赖
  30. echo ""
  31. echo "📦 安装依赖..."
  32. if [ "$OS" = "debian" ]; then
  33. apt update
  34. apt install -y curl wget git build-essential
  35. elif [ "$OS" = "redhat" ]; then
  36. yum install -y curl wget git gcc make
  37. fi
  38. # 检查 Go 环境
  39. if ! command -v go &> /dev/null; then
  40. echo ""
  41. echo "📦 安装 Go 环境..."
  42. GO_VERSION="1.21.0"
  43. wget -q https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz
  44. tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz
  45. rm go${GO_VERSION}.linux-amd64.tar.gz
  46. echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile
  47. export PATH=$PATH:/usr/local/go/bin
  48. echo "✓ Go 已安装"
  49. else
  50. echo "✓ Go 环境已存在: $(go version)"
  51. fi
  52. # 创建安装目录
  53. echo ""
  54. echo "📁 创建安装目录..."
  55. mkdir -p $INSTALL_DIR
  56. # 复制文件
  57. echo ""
  58. echo "📋 复制项目文件..."
  59. cp -r ./* $INSTALL_DIR/
  60. # 编译程序
  61. echo ""
  62. echo "🔨 编译程序..."
  63. cd $INSTALL_DIR
  64. # 整理并下载依赖
  65. echo "整理依赖..."
  66. go mod tidy
  67. # 下载依赖
  68. echo "下载 Go 依赖..."
  69. go mod download
  70. # 编译
  71. echo "编译程序..."
  72. CGO_ENABLED=1 go build -o dhcp-dns-manager ./cmd
  73. # 创建数据目录
  74. mkdir -p $INSTALL_DIR/data
  75. chown -R root:root $INSTALL_DIR
  76. # 创建 systemd 服务
  77. echo ""
  78. echo "⚙️ 创建 systemd 服务..."
  79. cat > /etc/systemd/system/$SERVICE_NAME.service << EOF
  80. [Unit]
  81. Description=DHCP & DNS Manager Service
  82. After=network.target
  83. Documentation=https://github.com/your-repo/dhcp-dns-manager
  84. [Service]
  85. Type=simple
  86. User=root
  87. WorkingDirectory=$INSTALL_DIR
  88. ExecStart=$INSTALL_DIR/dhcp-dns-manager -config $INSTALL_DIR/configs/config.json
  89. Restart=always
  90. RestartSec=5
  91. StandardOutput=journal
  92. StandardError=journal
  93. SyslogIdentifier=dhcp-dns-manager
  94. # 安全设置
  95. NoNewPrivileges=false
  96. ProtectSystem=false
  97. ProtectHome=false
  98. [Install]
  99. WantedBy=multi-user.target
  100. EOF
  101. # 重载 systemd
  102. systemctl daemon-reload
  103. # 配置防火墙
  104. echo ""
  105. echo "🔥 配置防火墙..."
  106. if command -v ufw &> /dev/null; then
  107. echo "配置 UFW 防火墙..."
  108. ufw allow 53/udp comment "DNS"
  109. ufw allow 67/udp comment "DHCP"
  110. ufw allow 8080/tcp comment "Web UI"
  111. elif command -v firewall-cmd &> /dev/null; then
  112. echo "配置 Firewalld..."
  113. firewall-cmd --permanent --add-port=53/udp
  114. firewall-cmd --permanent --add-port=67/udp
  115. firewall-cmd --permanent --add-port=8080/tcp
  116. firewall-cmd --reload
  117. else
  118. echo "⚠ 未检测到防火墙工具,请手动配置"
  119. fi
  120. # 启动服务
  121. echo ""
  122. echo "🚀 启动服务..."
  123. systemctl enable $SERVICE_NAME
  124. systemctl start $SERVICE_NAME
  125. # 检查状态
  126. sleep 2
  127. echo ""
  128. echo "========================================"
  129. echo " ✅ 安装完成!"
  130. echo "========================================"
  131. echo ""
  132. echo "服务状态:$(systemctl is-active $SERVICE_NAME)"
  133. echo ""
  134. echo "📱 Web 界面:http://$(hostname -I | awk '{print $1}'):8080"
  135. echo "👤 默认账号:admin / admin"
  136. echo ""
  137. echo "常用命令:"
  138. echo " 查看状态:systemctl status $SERVICE_NAME"
  139. echo " 查看日志:journalctl -u $SERVICE_NAME -f"
  140. echo " 重启服务:systemctl restart $SERVICE_NAME"
  141. echo " 停止服务:systemctl stop $SERVICE_NAME"
  142. echo ""
  143. echo "⚠️ 首次使用请修改默认密码!"
  144. echo ""