![](/img/trans.png)
[英]How do I route SSL traffic through an AWS "Application Load Balancer" to an EC2 instance
[英]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 。
/usr/lib/systemd/system/demo.service
[Unit]
Description=Demo Script
[Service]
Type=forking
ExecStart=/usr/bin/demoscript.sh
您需要使其在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.