繁体   English   中英

如何让我的服务器应用程序在 EC2 实例上运行?

[英]How do I make my server application run on an EC2 instance?

我开发了一个 Java 服务器(使用 Spring)并使用 FileZilla 将最终的可执行文件 JAR 上传到 EC2 实例。 现在我想让它运行。

我已经通过 SSH 连接并使用java -jar server.jar来运行我的服务器,并且它有效(我已经尝试访问它)。 然而,一旦 SSH 连接关闭,服务器显然也会停止运行。

我怎样才能以这种方式启动我的应用程序以使其保持运行?

编辑:使用此处解释的命令screen ,我能够在后台运行它,因此它一直在运行。

问题不在于云,它取决于在系统中将jar作为服务运行所需的配置。 如果您使用的是Elastic Bean Stalk ,请在以下示例中将systemctl更改为initctl

  1. 将希望运行的脚本命令放在/usr/bin/demoscript.sh中
  2. 请记住使用chmod + x使脚本可执行。
  3. 创建以下文件:

/usr/lib/systemd/system/demo.service

[Unit]
Description=Demo Script

[Service]
Type=forking
ExecStart=/usr/bin/demoscript.sh
  1. 重新加载systemd服务文件:systemctl daemon-reload
  2. 检查它是否与systemctl start demo一起使用

您需要使其在Linux中作为守护进程运行。

有许多教程/模板可用于创建守护程序shell脚本。 快速的Google搜索显示github有很多模板,因此请检查一下。

您可以尝试使用 systemd,它是一个 Linux 服务管理器。 您可以使用它在后台运行您的服务。

为此,您需要首先创建一个描述 systemd 应如何管理您的服务的unit file (更多信息,请点击此处)。

sudo vim /etc/systemd/system/your-application.service

您的文件可能看起来像这样

[Unit]
Description=Java Application as a Service
[Service]
User=ec2-user
#change this directory into your workspace
#mkdir workspace 
WorkingDirectory=/home/ec2-user/workspace
#path to the executable bash script which executes the jar file
ExecStart=/bin/bash /home/ec2-user/workspace/your-script.sh
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target

然后在您的主目录/home/ec2-user/workspace中,您可以创建 bash 脚本来运行您的 java 应用程序。

sudo nano your-script.sh

您的脚本可能如下所示

#!/bin/sh
java -jar your-application.jar

然后您需要做的就是使用命令启动服务

sudo systemctl enable your-application.service
sudo systemctl start your-application.service

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM