How to Deploy Spring Boot Application to AWS EC2 Amazon Linux


I created Spring Boot server application and deployed it to AWS EC2 Amazon Linux. I wrote the way to have deployed at that time.

Environment

I used gradle, not Maven.

  • Spring Boot 1.2.3
  • Tomcat 8
  • Java 1.8
  • MySQL 5.6 (AWS RDS)
  • gradle 3.4
  • Kotlin 1.0.0

Build Setting

Required Setting

First, apply required configuration. This is also wrote at Spring Boot document.

Add to Application Code

The class that contains main method should inherit SpringBootServletInitializer. And, override configure method. In Spring Boot document, the class name is Application, but in my appication it was Main class.

gradle Setting

build.gradle also should be changed.

baseName and version is not required. But apply plugin: 'war' is required for creating war file.

And add the following code.

I guess you already have several lines in dependencies section. Add providedRuntime setting.

Please look at Spring Boot document for more detail.

As a result, my build.gradle was changed to the following.

Then ./gradlew build generates war file into build/libs directory.

Environmental Setting

This section is required if you create multi environmental property files.

In Spring Boot, you can write in server.address or spring.datasource.url, etc. settings in application.properties. I created the following 3 files.

  • application-develpment.properties
  • application-local.properties
  • application-production.properties

If you build jar or war with ./gradlew build, you can execute it in specific environment like java -jar -Pspring.profiles.active=xxxxx xxxx.jar. But after deploying Tomcat, you cannot do that. So we should configure on building.

Then add the following lines to build.gradle.

Gradle 3.0 and later enable us to write build.gradle with Kotlin. In the case, write filter section like as follows.

activeProfiles の値を ?: を使って書いているのは、 activeProfiles が設定されなかった場合にもビルドを成功させるためです。 otherwise, when you open the project with Intellij IDEA first time, you can’t open the project because of the build failing.

And add spring.profiles.active=@activeProfiles@ to application.properties. At this time, my application.properties is below.

Thus, the build command when you want to compile with specific profile setting is ./gradlew -PactiveProfiles=xxxxx build. xxxxx のところには、 私の場合は local, development, production が入ります。

Establish Server

Launch server in AWS EC2, Amazon Linux.

Install Required Programs

This time, I don’t install Apache or Nginx.

Java

EC2 から gitリポジトリ を取得し、 EC2内でビルドして同一サーバ内にデプロイする方法をとったため、 Java は 1.8 の SDK をインストールしました。 Oracle のサイトから rpm パッケージ をダウンロードしてインストールします。 Usually I put downloaded rpm file into /usr/local/src.

git

git command is necessary, so install it by the following command.

Tomcat

Install Tomcat. Spring Boot contains internal Tomcat and it can be launched as sudo nohup java -jar .... But for this time, use Tomcat.

Execute the following command in Amazon Linux.

After that, Tomcat will be installed, and it can be launched by service command.

At last, execute chkconfig for launching Tomcat on server boot.

Tomcat Configuration

We want to make Tomcat listen to TCP 80 port, then change as below. It’s not required when you use Nginx or Apache and proxy 8080 port access to 80 port.

And change /usr/share/tomcat8/conf/server.xml. The file owner is tomcat, so you should edit like sudo -u tomcat vim /usr/share/tomcat8/conf/server.xml, as user tomcat. At the mid of the file, you can find <Connector port="8080" ..., then add the attribute proxyPort="80".

And reboot Tomcat.

Deploy

Dploy manually.

Compile

Go to the project directory and execute ./gradlew build or ./gradlew -PactiveProfiles=xxxx build. The program will be built.

Locate war at Tomcat Directory

Locate war file at Tomcat webapps directory

Reboot Tomcat

Reboot Tomcat

Now, we can get the content through 80 port of the server.