# 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](https://medium.com/javarevisited/7-best-aws-ec2-amazon-elastic-compute-cloud-online-courses-for-beginners-in-2021-f7a1a55ea719) with Ubuntu OS and connected to it with puTTy.

# **MySQL Database**

## **Install MySQL**

```bash
sudo apt install mysql-server
sudo mysql_secure_installation
sudo mysql
```

## **Create user and Grant Permissions**

mysql&gt; CREATE USER ‘ubuntu’@’[localhost](http://localhost)’ IDENTIFIED BY ‘password’;

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

```bash
SHOW VARIABLES LIKE 'validate_password%';
```

The output will be something like this:

```bash
+--------------------------------------+-------+
| 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](https://javarevisited.blogspot.com/2020/03/30-examples-of-mysql-commands-in-linux.html) as below to set the password policy:

```bash
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:

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

## **Migrate MySQL Database:**

Here are the steps to migrate the [MySQL databas](https://medium.com/javarevisited/top-5-courses-to-learn-mysql-in-2020-4ffada70656f)e:

1\. At the source server:

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

2\. At the target server:

```bash
mysql> CREATE DATABASE database_name;
```

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

```bash
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:

```bash
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
```

  
With:

1. ```bash
    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](https://medium.com/javarevisited/6-best-maven-courses-for-beginners-in-2020-23ea3cba89), an essential tool for Java developers:
    
    ```bash
    sudo apt updatesudo apt install maven #This will install JDK automaticallymvn -version
    ```
    
    ## **Clone JAVA application and configure**
    
    ```bash
    git clone [gitRepoURL]
    ```
    
    Edit AppPath/src/main/resources/application.properties, update the database name, username and password:
    
    ![](https://miro.medium.com/v2/resize:fit:1400/1*dKERl_AWpAOa6eiXB7hd0Q.png align="left")
    
    ## **Build Spring Boot Project With Maven**
    
    Run command [“”mvn install”](https://javarevisited.blogspot.com/2016/06/how-to-install-maven-in-on-windows-78-or-10.html#axzz5het8pfqP) to build the project:
    
    ```bash
    mvn install
    ```
    
    If you didn’t create the test module, there will be an error of Build Failure, can use “-DskipTests” to skip it.
    
    ```bash
    mvn -DskipTests install
    ```
    
    This command will create a .jar file in projectFolder/target. After built successfully, [run “java -jar” command](https://javarevisited.blogspot.com/2012/03/how-to-create-and-execute-jar-file-in.html) to start the project:
    
    ```bash
    java -jar target/YourProjectName-version-SNAPSHOT.jar
    ```
    
    By default, the app will listen to port 8080.
    
    # **NGINX Https Reverse Proxy**
    
    ## **Install Nginx**
    
    ```bash
    sudo apt updatesudo apt install nginxsudo service nginx start
    ```
    
    ## **Creating Self-signed Certificates**
    
    [AWS](https://medium.com/javarevisited/5-best-aws-courses-for-beginners-and-experienced-developers-to-learn-in-2021-563212409fbd?utm_source=dlvr.it&utm_medium=linkedin) 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.
    
    ```bash
    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”.
    
    ![](https://miro.medium.com/v2/resize:fit:1400/1*-7NjNKUOX5Fm1beJ8ZrshQ.png align="left")
    
    In the location block, use the “proxy\_pass” key to pass the requests to port 8080.
    
    ![](https://miro.medium.com/v2/resize:fit:1360/1*ITkJWkW-nF_GQkpPeRsKUw.png align="left")
    
    Restart Nginx by:
    
    ```bash
    sudo service nginx restart
    ```
    
    If everything goes well, our java app is accessible thru the “Public IPv4 DNS” address provided by [AWS](https://medium.com/javarevisited/6-best-aws-sysops-online-courses-for-devops-engineers-to-become-a-certified-aws-sysops-5c2ae9dad31d) now.
    
    ![](https://miro.medium.com/v2/resize:fit:1400/1*lp5FFLplPcajcB2_3Ma7aQ.png align="left")
    
    **Hurry!!! we have deployed the spring boot application successfully.**
