'Deploying Spring Boot war in AWS ec2 instance

I am trying to deploy a spring boot application as war file in Apache Tomcat/8.5.29 on aws ec2 instance. Same war file working fine if i paste it in local machine's tomcat webapps folder and restart the local's machine tomcat server, the deployed war file get extracted automatically and the application is accessible on local.

I am using same version of java "1.8" on local machine as well as on ec2 instance.

But when i trying the same on aws and restart the tomcat the war file is not getting extracted and not accessible.I have also checked log file on server, may be there some error, but i found empty logs file. i'm deploying the war file in 'var/lib/tomcat' folder.

As i have deploy the build of angular 5 application on the same aws's tomcat it also working fine.

Here is my code

1.ScApplication.java

@SpringBootApplication
public class ScApplication extends SpringBootServletInitializer{

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ScApplication.class);
}

     public static void main(String[] args) {
         SpringApplication.run(ScApplication.class, args);
    }
}

2.pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.student.corner SC 0.0.1-SNAPSHOT war

<name>SC</name>
<description>Student Corner</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-social-facebook</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-social-linkedin</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.7.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.mobile/spring-mobile-device -->
    <dependency>
        <groupId>org.springframework.mobile</groupId>
        <artifactId>spring-mobile-device</artifactId>
        <version>1.1.5.RELEASE</version>
    </dependency>    
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>



Solution 1:[1]

You may like to let Spring manage tomcat for you.

You can build spring boot as an executable jar instead of WAR. Then, you can easily manage your spring boot application using the Linux system systemd Service.

This approach will help you manage your deployment life cycle hooks like Start-Service, Stop-service, Validate-Service to integrate with just one line command.

You will not need to deal with shell scripts of your own.

Moreover, you can get spring boot logs into cloudwatch logs by installing the cloudwatch agent.

Step 1:

Modify your spring-boot-maven-plugin as below

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

Step 2:

https://docs.spring.io/spring-boot/docs/current/reference/html/deployment.html#deployment.installing.nix-services.system-d

Following steps from above link -

Write below systemd service unit file with file name - myapp.service

[Unit]
Description=yourappname
After=syslog.target

[Service]
User=youruser
ExecStart=/opt/app/yourApp.jar
StandardOutput=/var/log/yourApp/logs.log
StandardError=/var/log/yourApp/Errorlogs.log
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Save myapp.service in this location on your EC2 - /etc/systemd/system

Step 3:

Once service unit file created run below commands once to enable your service -

systemctl enable myapp.service

Step 4:

After deployment of your jar on location as specified in service unit file, you can invoke below command to manage your service -

  • Start - systemctl start myapp
  • Step - systemctl stop myapp
  • Validate - systemctl is-active --quiet myapp && echo Service is running

That's All!

Note - Modify app name as path as per your application requirement.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ramanuj Dad