Installing Spring Boot services in Amazon AWS as an init.d (I)


Spring manual for installing services in Unix/Linux is easy and straightforward to follow. Nonetheless, when I tried to start the service, although it was working launching it with java -jar, I got the following errors:
service proxy start
>> invalid file (bad magic number): Exec format error
or
>> /etc/init.d/proxy: /etc/init.d/proxy: cannot execute binary file
After several hours trying to solve the issue, I found that the error was because my Maven configuration was incorrect. According to Spring the second plugin in the code creates a ‘fully executable’ jar with Maven, what they don't mention is that you need to add the maven plugin too. I've updated the build part of the Maven configuration and the service finally worked.
<build>
        <resources>
            …
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.7.RELEASE</version>
                <configuration>
                    <executable>true</executable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
I encourage you to install the services as systemd, the successor of the System V init system, you can find the post here: Installing Spring Boot Services in Ubuntu as a systemd (II)

Lets review how to create the service once you had the correct maven configuration:
  1. Compile, as usual, I use Netbeans clean & build, but it can be done from the terminal.
    mvn clean install
  2. Copy the jar into the server
    scp -i /Home/credentials.pem ProxyServiceBoot-1.0.jar ec2-user@ec2-xx-xxx-xx-xxx.compute-1.amazonaws.com:./services
  3. Give execution permission
    chmod 500 ProxyServiceBoot-1.0.jar
  4. Create the link
     sudo ln -s /home/ec2-user/services/ProxyServiceBoot-1.0.jar /etc/init.d/proxy
  5. Check that the link exists
    ls -la /etc/init.d/proxy
    >> /etc/init.d/proxy -> /home/ec2-user/services/ProxyServiceBoot-1.0.jar
  6. Launch the service!
    sudo service proxy start
    >> Started [18123]
  7. I add a REST call to test that the services are up & running. Check with the browser that everything is correctly set. Remember to open the port in the AWS configuration to allow access. In my case 8181.
    @RequestMapping(value = "/test", method = RequestMethod.GET)
        public String home() {
            return "Proxy up & running!";
        }
    http://ec2-xx-xxx-xx-xxx.compute-1.amazonaws.com:8181/proxy/test
    Proxy up & running!

Comments