简体   繁体   English

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

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

I developed a Java server (using Spring) and uploaded the final executable JAR to an EC2 instance using FileZilla.我开发了一个 Java 服务器(使用 Spring)并使用 FileZilla 将最终的可执行文件 JAR 上传到 EC2 实例。 Now I want it to run.现在我想让它运行。

I've connected via SSH and used java -jar server.jar to run my server, and it worked (I've tried accessing it).我已经通过 SSH 连接并使用java -jar server.jar来运行我的服务器,并且它有效(我已经尝试访问它)。 However once the SSH connection is closed the server obviously stops running as well.然而,一旦 SSH 连接关闭,服务器显然也会停止运行。

How can I start my application in such a way so it keeps running?我怎样才能以这种方式启动我的应用程序以使其保持运行?

Edit: Using the command screen explained here I was able to run it in background and so it keeps running.编辑:使用此处解释的命令screen ,我能够在后台运行它,因此它一直在运行。

The issue is not cloud dependent its the configuration you have to do to run your jar as a service in your system. 问题不在于云,它取决于在系统中将jar作为服务运行所需的配置。 If you are using Elastic Bean Stalk change systemctl to initctl in below example. 如果您使用的是Elastic Bean Stalk ,请在以下示例中将systemctl更改为initctl

  1. Put the script commands you wish to run in /usr/bin/demoscript.sh 将希望运行的脚本命令放在/usr/bin/demoscript.sh中
  2. Remember to make the script executable with chmod +x. 请记住使用chmod + x使脚本可执行。
  3. Create the following file: 创建以下文件:

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

[Unit]
Description=Demo Script

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

You need to make it run as daemon process in linux. 您需要使其在Linux中作为守护进程运行。

There are many tutorial / templates available to create a daemon shell script. 有许多教程/模板可用于创建守护程序shell脚本。 Quick google search shows github has many templates, so check them out. 快速的Google搜索显示github有很多模板,因此请检查一下。

You could try using systemd which is a Linux service manager.您可以尝试使用 systemd,它是一个 Linux 服务管理器。 You can use it to run your service in the background.您可以使用它在后台运行您的服务。

To do that you need to first create a unit file that describes how systemd should manage your service (more info here ).为此,您需要首先创建一个描述 systemd 应如何管理您的服务的unit file (更多信息,请点击此处)。

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

Your file might look something like this您的文件可能看起来像这样

[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

Then in your home directory /home/ec2-user/workspace you can create the bash script that will run your java application.然后在您的主目录/home/ec2-user/workspace中,您可以创建 bash 脚本来运行您的 java 应用程序。

sudo nano your-script.sh

Your script might look like this您的脚本可能如下所示

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

All you need to do then is start the service with the command然后您需要做的就是使用命令启动服务

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

暂无
暂无

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

相关问题 如何通过 AWS“应用程序负载均衡器”将 SSL 流量路由到 EC2 实例 - How do I route SSL traffic through an AWS "Application Load Balancer" to an EC2 instance 如何在 MacOS 上编译 Rust 以在 AWS EC2 实例上运行? “无法执行二进制文件:Exec 格式错误” - How do I compile Rust on MacOS, to be run on an AWS EC2 instance? "cannot execute binary file: Exec format error" 如何让 apache 从克隆到我的 EC2 实例中的 github 存储库中的文件夹中加载索引文件? - How do I get apache to load an index file inside a folder from a github repo that is cloned into my EC2 instance? 我应该在哪里存储我的 object 在 AWS 运行 Ec2 实例与应用程序负载均衡器 - where should I store my object in AWS Running Ec2 Instance With Application Load balancer 如何在具有 mysql 数据库和 costum 域的 windows 服务器 ec2 实例上运行 django 应用程序 - How to run django app on windows server ec2 instance with mysql database and costum domain AWS EC2 - 如何确保实例正在重启? - AWS EC2 - how to make sure an instance is being rebooted? 如何使用 Terraform 将 EC2 实例启动到现有 VPC 中? - How do I launch an EC2 instance into an existing VPC using Terraform? 如何在 AWS 中编写一个 Lamdda function 以允许我连接到运行 MySQL 的 EC2 实例 - How do I write a Lamdda function in AWS that allows me to connect to an EC2 instance that is running MySQL 如何将各种密钥名称关联到 terraform 中的 ec2 实例? - How do I associate various key names to an ec2 instance in terraform? 如何与aws上的ec2实例建立安全连接 - How to make a secure connection to ec2 instance on aws
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM