从零开始:SpringBoot项目服务器部署完全指南
本文详细介绍了如何将SpringBoot项目从本地开发环境部署到生产服务器的全过程,包括服务器环境准备、远程连接工具使用、环境安装配置、代码部署及项目运行维护等全方位内容。
一、准备工作 1. 服务器选择与购买 选择适合的云服务器提供商:
国内 :阿里云、腾讯云、华为云
国外 :AWS、Google Cloud、DigitalOcean
购买时需要考虑的因素:
操作系统 :推荐 CentOS 7/8 或 Ubuntu 18.04/20.04/22.04 LTS
配置 :根据项目需求选择(建议最低 2核4G)
带宽 :根据访问量决定
存储 :根据数据量决定(建议最低 40GB SSD)
2. 域名与备案(国内服务器必须)
购买域名(阿里云、腾讯云、GoDaddy 等)
国内服务器需要进行 ICP 备案
配置 DNS 解析,将域名指向服务器 IP
3. 本地准备工作 安装远程连接工具 Windows 平台 :
Xshell :SSH 终端模拟器
Xftp :SFTP/FTP 文件传输工具
Mac/Linux 平台 :
终端自带 SSH 客户端
FileZilla :跨平台 FTP 客户端
SpringBoot 项目打包 确保项目可以在本地正常打包:
1 2 3 4 5 mvn clean package -Dmaven.test.skip=true gradle build -x test
二、连接与管理服务器 1. 使用 Xshell 连接服务器
打开 Xshell ,点击”新建”按钮
配置连接信息 :
名称:自定义会话名
协议:SSH
主机:服务器 IP 地址
端口:22(默认 SSH 端口)
用户身份验证:
方法:Password
用户名:root(首次登录)
密码:服务器密码
连接服务器 :点击”确定”后连接
2. 使用 Xftp 传输文件
打开 Xftp ,点击”新建”按钮
配置连接信息 (同 Xshell)
连接后操作 :
左侧为本地文件
右侧为服务器文件
拖拽文件即可传输
3. 服务器基础设置 修改密码
创建普通用户(安全实践) 1 2 3 4 5 6 7 8 9 10 useradd -m springuser passwd springuser usermod -aG wheel springuser usermod -aG sudo springuser
更新系统 1 2 3 4 5 yum update -y apt update && apt upgrade -y
配置防火墙 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 yum install -y firewalld systemctl start firewalld systemctl enable firewalld firewall-cmd --permanent --add-port=80/tcp firewall-cmd --permanent --add-port=443/tcp firewall-cmd --permanent --add-port=8080/tcp firewall-cmd --permanent --add-port=3306/tcp firewall-cmd --reload apt install -y ufw ufw enable ufw allow 80/tcp ufw allow 443/tcp ufw allow 8080/tcp ufw allow 3306/tcp
三、安装宝塔面板(可选) 宝塔面板是一个简化 Linux 服务器管理的可视化工具。
1. 安装宝塔面板 1 2 3 4 5 yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh
安装完成后,会显示以下信息:
2. 宝塔面板基本使用
访问面板 :在浏览器中输入面板地址(http://服务器IP:8888)
安装推荐软件 :
使用宝塔管理文件 :
使用宝塔管理数据库 :
使用宝塔配置网站 :
四、手动配置服务器环境 如果不使用宝塔面板,可以按以下步骤手动配置:
1. 安装 JDK 1 2 3 4 5 6 7 8 yum install -y java-11-openjdk-devel apt install -y openjdk-11-jdk java -version
2. 安装 MySQL CentOS 7/8 1 2 3 4 5 6 7 8 9 10 11 12 13 rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm yum install -y mysql-server systemctl start mysqld systemctl enable mysqld grep "temporary password" /var/log/mysqld.log mysql_secure_installation
Ubuntu 1 2 3 4 5 6 7 8 9 apt install -y mysql-server systemctl start mysql systemctl enable mysql mysql_secure_installation
3. 创建数据库与用户 1 2 3 4 5 6 7 8 9 10 11 12 13 mysql -u root -p CREATE DATABASE springapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'springuser' @'localhost' IDENTIFIED BY '密码' ; GRANT ALL PRIVILEGES ON springapp.* TO 'springuser' @'localhost' ; FLUSH PRIVILEGES; EXIT;
4. 安装 Nginx CentOS 7/8 1 2 3 4 5 6 7 8 9 yum install -y epel-release yum install -y nginx systemctl start nginx systemctl enable nginx
Ubuntu 1 2 3 4 5 6 apt install -y nginx systemctl start nginx systemctl enable nginx
五、部署 SpringBoot 项目 1. 上传项目 JAR 包 使用 Xftp 将打包好的 JAR 文件上传到服务器,例如:/opt/springapp/app.jar
1 2 3 4 5 6 mkdir -p /opt/springappchmod 755 /opt/springappchmod 755 /opt/springapp/app.jar
2. 创建配置文件 在 JAR 包同级目录创建 application-prod.properties
或 application-prod.yml
文件:
1 2 vi /opt/springapp/application-prod.properties
添加内容:
1 2 3 4 5 6 7 8 9 10 server.port =8080 spring.datasource.url =jdbc:mysql://localhost:3306/springapp?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8 spring.datasource.username =springuser spring.datasource.password =密码 spring.jpa.hibernate.ddl-auto =update logging.file.path =/opt/springapp/logs logging.level.root =warn logging.level.com.yourpackage =info
3. 创建启动脚本 1 vi /opt/springapp/start.sh
添加内容:
1 2 3 4 #!/bin/bash cd /opt/springappnohup java -jar app.jar --spring.config.location=file:./application-prod.properties > /dev/null 2>&1 &echo $! > app.pid
赋予执行权限:
1 chmod +x /opt/springapp/start.sh
4. 创建停止脚本 1 vi /opt/springapp/stop.sh
添加内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash PID_FILE=/opt/springapp/app.pid if [ -f "$PID_FILE " ]; then PID=$(cat $PID_FILE ) if [ -n "$PID " ]; then kill $PID rm $PID_FILE echo "Application stopped" else echo "PID file is empty" fi else echo "PID file not found" fi
赋予执行权限:
1 chmod +x /opt/springapp/stop.sh
5. 创建重启脚本 1 vi /opt/springapp/restart.sh
添加内容:
1 2 3 4 #!/bin/bash /opt/springapp/stop.sh sleep 5/opt/springapp/start.sh
赋予执行权限:
1 chmod +x /opt/springapp/restart.sh
6. 配置 Systemd 服务(推荐) 创建服务文件:
1 vi /etc/systemd/system/springapp.service
添加内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [Unit] Description =Spring Boot ApplicationAfter =syslog.target network.target[Service] User =rootWorkingDirectory =/opt/springappExecStart =/usr/bin/java -jar /opt/springapp/app.jar --spring.config.location=file:/opt/springapp/application-prod.propertiesExecStop =/bin/kill -15 $MAINPID SuccessExitStatus =143 Restart =alwaysRestartSec =5 [Install] WantedBy =multi-user.target
启用服务:
1 2 3 systemctl daemon-reload systemctl enable springapp systemctl start springapp
管理服务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 systemctl status springapp systemctl start springapp systemctl stop springapp systemctl restart springapp journalctl -u springapp -f
六、配置 Nginx 反向代理 1. 创建 Nginx 配置文件 1 2 3 4 5 vi /etc/nginx/conf.d/springapp.conf vi /etc/nginx/sites-available/springapp
添加内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 server { listen 80 ; server_name example.com www.example.com; location / { proxy_pass http://localhost:8080; proxy_set_header Host $host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; proxy_set_header X-Forwarded-Proto $scheme ; } location /static/ { alias /opt/springapp/static/; } }
在 Ubuntu 上创建符号链接:
1 2 ln -s /etc/nginx/sites-available/springapp /etc/nginx/sites-enabled/
2. 测试并重启 Nginx 1 2 3 4 5 nginx -t systemctl restart nginx
3. 配置 HTTPS(可选但推荐) 安装 Certbot:
1 2 3 4 5 yum install -y certbot python3-certbot-nginx apt install -y certbot python3-certbot-nginx
获取并配置 SSL 证书:
1 certbot --nginx -d example.com -d www.example.com
按照提示完成配置。
七、数据库迁移和导入 1. 导出本地数据库 1 2 3 4 5 mysqldump -u username -p databasename > backup.sql mysqldump -u username -p databasename > backup.sql
2. 上传数据库备份文件 使用 Xftp 上传 backup.sql
到服务器。
3. 导入数据库 1 mysql -u springuser -p springapp < backup.sql
八、项目维护与监控 1. 设置日志轮转 创建 logrotate 配置:
1 vi /etc/logrotate.d/springapp
添加内容:
1 2 3 4 5 6 7 8 9 /opt/springapp/logs/*.log { daily missingok rotate 30 compress delaycompress notifempty create 0640 root root }
2. 监控 JVM 性能 安装和使用 jstat
:
1 2 jstat -gc $(pgrep -f app.jar) 5000
3. 使用 Prometheus + Grafana 监控(可选) 修改 SpringBoot 项目,添加 Actuator 和 Micrometer 依赖:
1 2 3 4 5 6 7 8 9 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-actuator</artifactId > </dependency > <dependency > <groupId > io.micrometer</groupId > <artifactId > micrometer-registry-prometheus</artifactId > </dependency >
配置 Prometheus 和 Grafana:
1 2 3 docker run -d -p 9090:9090 --name prometheus -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus docker run -d -p 3000:3000 --name grafana grafana/grafana
九、自动化部署流程 1. 使用 Jenkins(可选) 安装 Jenkins:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.reposudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.keysudo yum install -y jenkins java-11-openjdk-develsudo systemctl start jenkinssudo systemctl enable jenkinswget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo apt-key add - sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt updatesudo apt install -y jenkinssudo systemctl start jenkinssudo systemctl enable jenkins
访问 http://服务器IP:8080
完成 Jenkins 初始设置。
2. 创建 Jenkins 任务
创建新任务 > 流水线(Pipeline)
配置 Git 仓库
编写 Pipeline 脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 pipeline { agent any stages { stage('Checkout' ) { steps { git 'https://github.com/username/repo.git' } } stage('Build' ) { steps { sh 'mvn clean package -Dmaven.test.skip=true' } } stage('Deploy' ) { steps { sh 'cp target/*.jar /opt/springapp/app.jar' sh 'systemctl restart springapp' } } } }
3. 配置 GitHub Webhook 在 GitHub 仓库中添加 Webhook,指向:http://服务器IP:8080/github-webhook/
十、常见问题与排错 1. 应用启动问题
查看应用日志 :
1 2 3 tail -f /opt/springapp/logs/spring.logjournalctl -u springapp -f
检查端口占用 :
1 netstat -tulpn | grep 8080
检查内存和 CPU :
2. 数据库连接问题
测试数据库连接 :
1 mysql -u springuser -p springapp -h localhost
检查数据库服务状态 :
3. Nginx 配置问题
测试 Nginx 配置 :
检查 Nginx 错误日志 :
1 tail -f /var/log/nginx/error.log
4. 防火墙问题
检查防火墙状态 :1 2 3 4 5 firewall-cmd --list-all ufw status
5. 内存不足
添加交换空间 :
1 2 3 4 5 6 7 8 dd if =/dev/zero of=/swapfile bs=1M count=2048chmod 600 /swapfilemkswap /swapfile swapon /swapfile echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab
优化 JVM 参数 : 修改启动脚本,添加内存限制:
1 java -Xms512m -Xmx1024m -jar app.jar
十一、安全最佳实践 1. 定期更新系统 1 2 3 4 5 yum update -y apt update && apt upgrade -y
2. 使用 fail2ban 防止暴力破解 1 2 3 4 5 6 7 8 9 yum install -y fail2ban systemctl enable fail2ban systemctl start fail2ban apt install -y fail2ban systemctl enable fail2ban systemctl start fail2ban
3. 禁用 root SSH 登录 编辑 SSH 配置文件:
修改配置:
重启 SSH 服务:
4. 定期备份数据 创建备份脚本:
1 vi /opt/scripts/backup.sh
添加内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash DATE=$(date +%Y%m%d) BACKUP_DIR="/opt/backups" mkdir -p $BACKUP_DIR mysqldump -u springuser -p'密码' springapp > $BACKUP_DIR /springapp_$DATE .sql tar -zcf $BACKUP_DIR /springapp_files_$DATE .tar.gz /opt/springapp find $BACKUP_DIR -name "*.sql" -mtime +30 -delete find $BACKUP_DIR -name "*.tar.gz" -mtime +30 -delete
设置执行权限并添加定时任务:
1 2 chmod +x /opt/scripts/backup.shcrontab -e
添加定时任务:
1 0 2 * * * /opt/scripts/backup.sh > /dev/null 2>&1
参考资源
本指南详细介绍了从零开始将 SpringBoot 项目部署到服务器的完整流程。无论你是初学者还是有经验的开发者,按照这些步骤,都能成功搭建一个稳定、安全、高效的生产环境。如有疑问,欢迎在评论区留言交流!