- #!/bin/bash
-
- # 福泉WebAPI 安装脚本
- # 作者: Your Name
- # 版本: 1.0.0
-
- # 颜色定义
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- BLUE='\033[0;34m'
- NC='\033[0m'
-
- print_message() {
- local color=$1
- local message=$2
- echo -e "${color}[$(date '+%Y-%m-%d %H:%M:%S')] $message${NC}"
- }
-
- # 检查是否为root用户
- check_root() {
- if [ "$EUID" -eq 0 ]; then
- print_message $YELLOW "警告: 不建议使用root用户运行此脚本"
- read -p "是否继续? (y/N): " -n 1 -r
- echo
- if [[ ! $REPLY =~ ^[Yy]$ ]]; then
- exit 1
- fi
- fi
- }
-
- # 检查系统类型
- check_system() {
- if [[ "$OSTYPE" == "linux-gnu"* ]]; then
- print_message $GREEN "检测到Linux系统"
- else
- print_message $RED "此脚本仅支持Linux系统"
- exit 1
- fi
- }
-
- # 检查Java环境
- check_java() {
- if command -v java &> /dev/null; then
- JAVA_VERSION=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2)
- print_message $GREEN "检测到Java版本: $JAVA_VERSION"
- return 0
- else
- print_message $RED "未找到Java环境"
- return 1
- fi
- }
-
- # 安装Java
- install_java() {
- print_message $BLUE "正在安装Java环境..."
-
- # 检测包管理器
- if command -v apt-get &> /dev/null; then
- # Ubuntu/Debian
- sudo apt-get update
- sudo apt-get install -y openjdk-8-jdk
- elif command -v yum &> /dev/null; then
- # CentOS/RHEL
- sudo yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
- elif command -v dnf &> /dev/null; then
- # Fedora
- sudo dnf install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
- else
- print_message $RED "不支持的包管理器,请手动安装JDK 1.8+"
- exit 1
- fi
-
- # 验证安装
- if check_java; then
- print_message $GREEN "Java安装成功"
- else
- print_message $RED "Java安装失败"
- exit 1
- fi
- }
-
- # 创建应用目录
- create_app_dir() {
- APP_DIR="/opt/fuquan-webapi"
-
- if [ ! -d "$APP_DIR" ]; then
- sudo mkdir -p "$APP_DIR"
- print_message $BLUE "创建应用目录: $APP_DIR"
- fi
-
- # 设置权限
- sudo chown -R $USER:$USER "$APP_DIR"
- print_message $GREEN "应用目录创建完成"
- }
-
- # 复制文件
- copy_files() {
- APP_DIR="/opt/fuquan-webapi"
-
- # 复制JAR文件
- if [ -f "webapi-0.0.1-SNAPSHOT.jar" ]; then
- cp webapi-0.0.1-SNAPSHOT.jar "$APP_DIR/"
- print_message $GREEN "JAR文件复制完成"
- else
- print_message $RED "未找到JAR文件,请先编译项目"
- exit 1
- fi
-
- # 复制启动脚本
- if [ -f "start.sh" ]; then
- cp start.sh "$APP_DIR/"
- chmod +x "$APP_DIR/start.sh"
- print_message $GREEN "启动脚本复制完成"
- fi
-
- # 创建配置文件
- if [ ! -f "$APP_DIR/application.yml" ]; then
- cat > "$APP_DIR/application.yml" << EOF
- spring:
- datasource:
- url: jdbc:sqlserver://localhost:1433;databaseName=fuquan_db;encrypt=false;trustServerCertificate=true
- username: your_username
- password: your_password
- driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
- jpa:
- hibernate:
- ddl-auto: update
- show-sql: false
- properties:
- hibernate:
- dialect: org.hibernate.dialect.SQLServer2012Dialect
-
- server:
- port: 8806
- servlet:
- context-path: /fuquanapi
-
- api:
- client:
- ak: your_access_key_here
- sk: your_secret_key_here
-
- logging:
- level:
- com.example.webapi: INFO
- file:
- name: logs/app.log
- EOF
- print_message $GREEN "配置文件创建完成"
- fi
- }
-
- # 创建系统服务
- create_service() {
- SERVICE_FILE="/etc/systemd/system/fuquan-webapi.service"
-
- if [ ! -f "$SERVICE_FILE" ]; then
- sudo tee "$SERVICE_FILE" > /dev/null << EOF
- [Unit]
- Description=福泉WebAPI服务
- After=network.target
-
- [Service]
- Type=simple
- User=$USER
- WorkingDirectory=/opt/fuquan-webapi
- ExecStart=/opt/fuquan-webapi/start.sh start
- ExecStop=/opt/fuquan-webapi/start.sh stop
- Restart=always
- RestartSec=10
-
- [Install]
- WantedBy=multi-user.target
- EOF
- print_message $GREEN "系统服务创建完成"
-
- # 重新加载systemd
- sudo systemctl daemon-reload
- print_message $BLUE "系统服务已注册"
- else
- print_message $YELLOW "系统服务已存在"
- fi
- }
-
- # 设置防火墙
- setup_firewall() {
- print_message $BLUE "配置防火墙规则..."
-
- # 检测防火墙类型
- if command -v ufw &> /dev/null; then
- # Ubuntu UFW
- sudo ufw allow 8806/tcp
- print_message $GREEN "UFW防火墙规则已添加"
- elif command -v firewall-cmd &> /dev/null; then
- # CentOS/RHEL firewalld
- sudo firewall-cmd --permanent --add-port=8806/tcp
- sudo firewall-cmd --reload
- print_message $GREEN "firewalld防火墙规则已添加"
- else
- print_message $YELLOW "未检测到防火墙,请手动开放8806端口"
- fi
- }
-
- # 显示安装完成信息
- show_completion() {
- print_message $GREEN "=========================================="
- print_message $GREEN "福泉WebAPI 安装完成!"
- print_message $GREEN "=========================================="
- echo ""
- print_message $BLUE "应用目录: /opt/fuquan-webapi"
- print_message $BLUE "配置文件: /opt/fuquan-webapi/application.yml"
- print_message $BLUE "启动脚本: /opt/fuquan-webapi/start.sh"
- echo ""
- print_message $YELLOW "请编辑配置文件设置数据库连接信息:"
- print_message $YELLOW "sudo nano /opt/fuquan-webapi/application.yml"
- echo ""
- print_message $BLUE "启动服务:"
- print_message $BLUE "sudo systemctl start fuquan-webapi"
- print_message $BLUE "sudo systemctl enable fuquan-webapi"
- echo ""
- print_message $BLUE "查看服务状态:"
- print_message $BLUE "sudo systemctl status fuquan-webapi"
- echo ""
- print_message $GREEN "访问地址: http://localhost:8806/fuquanapi"
- print_message $GREEN "API文档: http://localhost:8806/fuquanapi/swagger-ui.html"
- }
-
- # 主函数
- main() {
- print_message $BLUE "开始安装福泉WebAPI..."
-
- check_root
- check_system
-
- if ! check_java; then
- install_java
- fi
-
- create_app_dir
- copy_files
- create_service
- setup_firewall
- show_completion
- }
-
- # 执行主函数
- main "$@"
|