linux命令部署springboot
linux命令部署springboot详细介绍
下面为你介绍在 Linux 系统上部署 Spring Boot 应用的步骤:
1. 准备工作
确保你的 Linux 系统已经安装了 Java 运行环境(JRE)或者 Java 开发工具包(JDK),可以使用以下命令来检查 Java 版本:
bash
如果没有安装,可依据系统发行版采用合适的包管理器来安装,例如在 Ubuntu/Debian 系统上:
bash
updateopenjdk-17-jre
在 CentOS/RHEL 系统上:
bash
yum java-17-openjdk
2. 打包 Spring Boot 应用
在本地开发环境里,使用 Maven 或者 Gradle 把 Spring Boot 应用打包成可执行的 JAR 文件。
Maven
在项目根目录下执行:
bash
mvn clean package
Gradle
在项目根目录下执行:
bash
gradle clean build
上述命令执行完毕后,会在项目的target(Maven)或者build/libs(Gradle)目录下生成一个可执行的 JAR 文件。
3. 上传 JAR 文件到 Linux 服务器
借助scp命令把本地生成的 JAR 文件上传到 Linux 服务器,示例如下:
bash
/path/to/your/app.jar user@server:/destination/path
其中,/path/to/your/app.jar是本地 JAR 文件的路径,user是服务器的用户名,server是服务器的 IP 地址或者域名,/destination/path是 JAR 文件在服务器上的目标路径。
4. 运行 Spring Boot 应用
在 Linux 服务器上,使用java -jar命令来运行 Spring Boot 应用:
bash
/destination/path/app.jar
若想让应用在后台持续运行,可使用nohup命令:
bash
/destination/path/app.jar app.log >
上述命令会把应用的输出重定向到app.log文件,并且让应用在后台运行。
5. 验证应用是否正常运行
你可以使用curl命令或者在浏览器中访问应用的 URL 来验证应用是否正常运行,例如:
bash
http://localhost:8080
若返回预期的结果,就表明应用已经成功部署并运行。
6. 停止应用
若要停止正在运行的 Spring Boot 应用,可使用ps和kill命令找到并终止对应的进程,示例如下:
bash
app.jarPID
其中,<PID>是应用进程的 ID。
7. 使用 Systemd 管理应用(可选)
为了更方便地管理 Spring Boot 应用的启动、停止和重启,你可以创建一个 Systemd 服务文件。
创建服务文件
bash
/etc/systemd/system/springboot-app.service
在文件中添加以下内容:
plaintext
[Unit]Description=Spring Boot Application
After=syslog.target network.target
[Service]
User=your_user
ExecStart=/usr/bin/java -jar /destination/path/app.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
注意将your_user替换为运行应用的用户名,/destination/path/app.jar替换为 JAR 文件的实际路径。
重新加载 Systemd 配置
bash
systemctl daemon-reload
启动、停止和重启应用
bash
systemctl start springboot-app
systemctl stop springboot-app
systemctl restart springboot-app
systemctl springboot-app
99123456789101112 systemctl springboot-app
systemctl springboot-app
systemctl springboot-app
systemctl enable springboot-app
通过以上步骤,你就能在 Linux 系统上成功部署和管理 Spring Boot 应用了。