Skip to main content

Command Palette

Search for a command to run...

Deploying a Java Spring Boot application on AWS EC2

Published
•3 min read•View as Markdown
Deploying a Java Spring Boot application on AWS EC2

This is a brief note about deploying a JAVA Spring Boot project on the AWS EC2 server.

Assuming you have created an EC2 instance with Ubuntu OS and connected to it with puTTy.

MySQL Database

Install MySQL

sudo apt install mysql-server
sudo mysql_secure_installation
sudo mysql

Create user and Grant Permissions

mysql> CREATE USER ‘ubuntu’@’localhost’ IDENTIFIED BY ‘password’;

In case of error “Your password does not satisfy the current policy requirements.”:

SHOW VARIABLES LIKE 'validate_password%';

The output will be something like this:

+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password.check_user_name    | ON    |
| validate_password.dictionary_file    |       |
| validate_password.length             | 6     |
| validate_password.mixed_case_count   | 1     |
| validate_password.number_count       | 1     |
| validate_password.policy             | LOW   |
| validate_password.special_char_count | 1     |
+--------------------------------------+-------+

Then use the command as below to set the password policy:

SET GLOBAL validate_password.length = 6;
SET GLOBAL validate_password.number_count = 0;

Grant Permissions

After create user successfully, grant permissions for the created user:

mysql> GRANT ALL PRIVILEGES ON *.* TO ‘ubuntu’@’localhost’;
mysql> FLUSH PRIVILEGES;

Migrate MySQL Database:

Here are the steps to migrate the MySQL database:

1. At the source server:

mysqldump -u [username] -p [database] > dump.sql

2. At the target server:

mysql> CREATE DATABASE database_name;

3. copy the generated dump.sql to the target server, then:

mysql -u ubuntu -p databasename < dump.sql

4. In case of error “Unknown collation: utf8mb4_0900_ai_ci”:

Just edit the dump.sql, replace the below string:

ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

With:

  1.  ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
    

    Be reminded, there might be more than one occurrence, need to replace them all.

    Apache Maven

    Install Apache Maven

    You can use following command to install Apache Maven, an essential tool for Java developers:

     sudo apt updatesudo apt install maven #This will install JDK automaticallymvn -version
    

    Clone JAVA application and configure

     git clone [gitRepoURL]
    

    Edit AppPath/src/main/resources/application.properties, update the database name, username and password:

    Build Spring Boot Project With Maven

    Run command “”mvn install” to build the project:

     mvn install
    

    If you didn’t create the test module, there will be an error of Build Failure, can use “-DskipTests” to skip it.

     mvn -DskipTests install
    

    This command will create a .jar file in projectFolder/target. After built successfully, run “java -jar” command to start the project:

     java -jar target/YourProjectName-version-SNAPSHOT.jar
    

    By default, the app will listen to port 8080.

    NGINX Https Reverse Proxy

    Install Nginx

     sudo apt updatesudo apt install nginxsudo service nginx start
    

    Creating Self-signed Certificates

    AWS provides services of CA(certificate authority), usually need to pay for it. Here we use OpenSSL to create a self-signed certificate to be in the place.

     sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt
    

    Above command will create the key file (stored in /etc/ssl/private) and the certificate file (stored in /etc/ssl/certs).

    Config Nginx

    Edit Nginx configuration file at /etc/nginx/sites-enabled/default

    Have the server listening to port 443.

    Put the file path from the last step for the keys of “ssl_certificate” and “ssl_certificate_key”.

    In the location block, use the “proxy_pass” key to pass the requests to port 8080.

    Restart Nginx by:

     sudo service nginx restart
    

    If everything goes well, our java app is accessible thru the “Public IPv4 DNS” address provided by AWS now.

    Hurry!!! we have deployed the spring boot application successfully.

More from this blog

Kunal's dev blogs

8 posts

From coding with Java Spring Boot and React to cloud deployment on OCI and Azure, I'm a full-stack developer who dances between servers and pixels, bringing digital dreams to life.