Deploy Spring Boot app in digitalocean cloud (or any cloud as long asyou have ssh access)

First thing first. Assuming that you got an account at digitalocean, lets create a droplet. But before that add your ssh keys of your computers in Settings>Security>Add SSH Key.

1. Resolving SSH Keys


If you need to generate ssh key go here.

If you already have a ssh key then copy the content of your public key and paste it on digialocean.

2. Create Droplet


For this purpose I choose ubuntu 16.04 with 1 gigabyte if Ram. Choose it according to your need. And yes, DON'T forget to choose your pc (SSH Keys) when creating droplet. After creating droplet you'll get an IP address of your machine. If you can't find the IP, well consult your eye specialist ;)

3. Access to your droplet


If you are from mac or linux just open terminal and execute this command.
ssh [email protected]_address

If you copied your public key to digitalocean when creating the droplet you should get access at this point. If you forgot to do that you have to add your ssh key on the operating system manually.

4. Install Java


To install Java 8 execute the following command.
sudo apt-get update && sudo apt-get install openjdk-8-jdk

5. Install Mysql


sudo apt-get install mysql-server

On production server you'll like to execute this following script. This changes some of the less secure default options for things like remote root logins and sample users. On older versions of MySQL, you needed to initialize the data directory manually as well, but this is done automatically now.
sudo mysql_secure_installation

To test your mysql installation:
systemctl status mysql.service

6. Create Database and User


Login to mysql console
mysql -u root -p

Create Database
CREATE DATABASE new_database;
OR (Considered as better practice)
CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Showing databases
SHOW DATABASES;

Create Database User
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

Grant Privileges to that user
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

Drop user
DROP USER ‘newuser’@‘localhost’;

Finally quit mysql using
quit

7. Uploading .jar file through scp


First build your spring boot application as a .jar file using this command.
mvn clean package -Dmaven.test.skip=true

When .jar file is built successfully copy this file to your server using scp.
scp [source] [destination]

in my case
scp /Volumes/workspace/workspace/springspace/hrsweb/target/hrs-0.0.1-SNAPSHOT.jar [email protected]_address:/root

It'll start copying your file. To learn more about using scp go here.

You may want to change your filename after uploding it. this will do the trick.
mv old_file_name new_file_name

8. Run App


To run .jar file in the background (so that it isn't closed when you ssh session is closed)
nohup java -jar appname.jar

You may need these commands during this process.

Find the process id of specific application.
ps aux | grep <process name>

and killing it
kill <process id>



Here's a related article you may want to read.

Deploy Spring Boot app on Pivotal Web Service (cloud foundry PaaS)



Comments

  1. can you please show how the application.properties file will look alike in your case.

    ReplyDelete
    Replies
    1. Hi,

      You've to specify datasource informations on application.properties file.
      Here's the configurations I'm using.

      spring.datasource.url=jdbc:mysql://localhost/db_name?useUnicode=yes&characterEncoding=UTF-8&characterSetResults=UTF-8
      spring.datasource.username=db_user
      spring.datasource.password=password
      spring.datasource.driver-class-name=com.mysql.jdbc.Driver

      and other datasource configurations.

      Delete
  2. Hi,

    Super helpful guide. I've got one question though. I've uploaded my Springboot app as a Jar to my droplet and got it running, but when I try to hit one of the endpoints described in my Controller with {{IP_ADDRESS}}:{{PORT}}/{{ENDPOINT}} I get an error in Postman saying there was an error connecting. Is there anything else I need to do to hit that endpoint?

    Thanks!

    ReplyDelete

Post a Comment

Popular posts from this blog

Upload large files : Spring Boot

User activity logging: Spring