Original

Deploy a SpringBoot Application on AWS


This blog introduces how I deployed my blog system, a SpringBoot application with JPA / Hibernate and MySQL database, to AWS EC2.


Step 1. Launch an instance on AWS EC2

1. Launch an instance

https://docs.aws.amazon.com/quickstarts/latest/vmlaunch/step-1-launch-instance.html

Select virtual machine image (I selected Amazon Linux AMI 2018.03.0), and use default settings to launch.

image Remember to save your private key as .pem.

2. Configure ports

image

Modify inbound rules to allow users to access to connect to http or https ports:

image

3. Connect to your instance using Putty and WinSCP

https://docs.aws.amazon.com/quickstarts/latest/vmlaunch/step-2-connect-to-instance.html

4. Check jdk version and change to 1.8 if necessary

sudo yum install java-1.8.0
sudo yum remove java-1.7.0-openjdk

Step 2. Install and configure MySQL database on AWS Linux

1. Install MySQL

sudo yum install mysql
sudo yum install mysql-server
sudo yum install mysql-devel

After installation,the loacation of MySQL database is /var/lib/mysql and the location of configuration file is /etc/my.cnf

2. Modify the group and ownership of /var/lib/mysql

sudo chgrp -r mysql /var/lib/mysql
sudo chmod -r 770 /var/lib/mysql

3. Start MySQL server, setup login user, password and create database

Database name, login user and password should be the same as in application.xml:

sudo service mysqld start
/usr/bin/mysqladmin -u root password 'your password'

To log in as user root and create database:

mysql -u root -p
mysql> CREATE DATABASE blog;

4. Change character set encoding

Check MySQL encoding first:

SHOW VARIABLES LIKE 'character_set_%';

If you find that:

| character_set_database   | latin1                     
| character_set_server     | latin1

Edit my.cnf:

sudo vi /etc/my.cnf

And add following settings below [mysqld]:

[mysqld]
character_set_server = utf8
collation-server=utf8_general_ci

Finally:

sudo service mysqld start

Step 3. Package Spring Boot application to JAR

Ensure that you add the following maven plug-in configuration in pom.xml before packaging:

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

Enable production profiles (application_pro.xml) in application.xml:

image

Modify application_pro.xml:

  • Modify MySQL connection URL to allow changing sever time zone later
  • Change hibernate property value to create to initialize tables

image

Run clean and package in sequence:

image

If the packaging task succeeds, you will find *.jar file inside the target directory:

image

Test run:

java -jar blog-0.0.1-SNAPSHOT.jar

See java console log and visit localhost:XXXX/ to check.

Step 4. Run .jar on AWS to initialize database

Upload blog-0.0.1-SNAPSHOT.jar to your instance - drag file from local panel to remote panel of WinSCP:

image

Test run to create tables:

java -jar blog-0.0.1-SNAPSHOT.jar

Modify & add data in databse if necessary. Ctrl+C to stop running.

Step 5. Modify application.xml, re-package and re-upload

Change hibernate property value to none:

image

Step 6. Set the time for instance on AWS

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html

Step 7. Run .jar on AWS EC2

To keep the program running after you exit Putty:

nohup java -jar blog-0.0.1-SNAPSHOT.jar &
0
  • By Yuechun Wang
  • Published 2020-08-04
  • The materials on this website may be freely copied and distributed for noncommercial personal use only so long as our copyright notice and website address is included.
  • Comments