搭建基于 Spring Boot 的 Java 项目服务器环境,通常分为 基础环境准备、应用部署 和 反向X_X/安全配置 三个主要阶段。以下是一个标准、生产可用的搭建流程(以 Linux CentOS/Ubuntu 为例):
✅ 一、前期准备
1. 服务器要求
- OS:CentOS 7+/8+、Ubuntu 18.04+、Debian 10+
- 内存:建议 ≥2GB(根据应用负载调整)
- 磁盘:≥20GB(含日志、依赖等)
- 网络:开放端口(如 80, 443, 8080 等)
2. 安装 JDK(Java Development Kit)
Spring Boot 需要 JDK 8 或更高版本(推荐 JDK 11 或 17)。
# CentOS/RHEL
sudo yum install java-11-openjdk-devel -y
# Ubuntu/Debian
sudo apt update
sudo apt install openjdk-11-jdk -y
# 验证安装
java -version
💡 若使用 Docker 部署,可跳过此步,由镜像内置 JDK。
✅ 二、打包 Spring Boot 应用
在本地开发环境中,将项目打包为可执行的 JAR 文件:
mvn clean package -DskipTests
生成的 JAR 文件位于 target/ 目录下,例如:myapp-0.0.1-SNAPSHOT.jar
✅ 三、上传并运行应用
方法 1:直接运行 JAR(简单场景)
# 创建应用目录
mkdir -p /opt/myapp
cd /opt/myapp
# 上传 JAR 文件(使用 scp 或 FTP)
scp myapp-0.0.1-SNAPSHOT.jar user@server:/opt/myapp/
# 后台运行
nohup java -jar myapp-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod > app.log 2>&1 &
⚠️ 注意:这种方式适合测试或小流量场景,缺乏进程管理和日志轮转。
方法 2:使用 Systemd 管理服务(推荐生产环境)
1. 创建 systemd 服务文件
# /etc/systemd/system/myapp.service
[Unit]
Description=My Spring Boot Application
After=syslog.target network.target
[Service]
User=root
Group=root
ExecStart=/usr/bin/java -jar /opt/myapp/myapp-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
SuccessExitStatus=143
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
2. 启用并启动服务
sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service
# 查看状态
sudo systemctl status myapp.service
# 查看日志
journalctl -u myapp.service -f
✅ 四、配置 Nginx 反向X_X(可选但推荐)
让外部通过域名或 IP:80 访问应用,隐藏内部端口。
1. 安装 Nginx
# CentOS
sudo yum install nginx -y
# Ubuntu
sudo apt install nginx -y
2. 配置 Nginx
# /etc/nginx/conf.d/myapp.conf
server {
listen 80;
server_name yourdomain.com; # 替换为你的域名或 IP
location / {
proxy_pass http://127.0.0.1:8080; # Spring Boot 默认端口
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;
}
}
3. 重启 Nginx
sudo nginx -t # 测试配置
sudo systemctl restart nginx
sudo systemctl enable nginx
✅ 五、HTTPS 配置(Let’s Encrypt + Certbot)
# 安装 Certbot
sudo yum install certbot python3-certbot-nginx # CentOS
sudo apt install certbot python3-certbot-nginx # Ubuntu
# 获取证书
sudo certbot --nginx -d yourdomain.com
# 自动续期测试
sudo certbot renew --dry-run
✅ 六、防火墙与安全组
1. 开放必要端口
# CentOS (firewalld)
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --add-port=443/tcp
sudo firewall-cmd --reload
# Ubuntu (ufw)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
2. 云服务商安全组
确保阿里云、腾讯云、AWS 等云平台的安全组中放行 HTTP/HTTPS 端口。
✅ 七、日志与监控(进阶)
1. 日志管理
- 使用
journald(systemd 自带) - 或集成 ELK(Elasticsearch + Logstash + Kibana)
- 或使用 Loki + Grafana
2. 监控
- Prometheus + Grafana
- Spring Boot Actuator(暴露
/actuator/health,/actuator/metrics)
在 application.yml 中添加:
management:
endpoints:
web:
exposure:
include: health,info,metrics
✅ 八、自动化部署(可选)
使用 Jenkins、GitLab CI、GitHub Actions 实现持续集成/部署:
# GitHub Actions 示例
name: Deploy to Server
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
- name: Build with Maven
run: mvn clean package -DskipTests
- name: Deploy via SSH
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_KEY }}
source: "target/*.jar"
target: "/opt/myapp/"
- name: Restart Service
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /opt/myapp
sudo systemctl restart myapp.service
📌 总结:最小可用部署清单
| 步骤 | 内容 |
|---|---|
| 1 | 安装 JDK |
| 2 | 打包 Spring Boot 为 JAR |
| 3 | 上传 JAR 到服务器 |
| 4 | 使用 Systemd 管理进程 |
| 5 | (可选)Nginx 反向X_X |
| 6 | (可选)HTTPS 配置 |
| 7 | 开放防火墙端口 |
| 8 | (可选)日志与监控 |
如需更复杂的部署(如集群、容器化、Kubernetes),可进一步引入 Docker + Docker Compose 或 K8s。欢迎继续提问具体场景!
云服务器