<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sudarshan Jadhav</title>
    <description>The latest articles on Forem by Sudarshan Jadhav (@sudarshan01).</description>
    <link>https://forem.com/sudarshan01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1313210%2Fd0b62a29-1cd4-4b17-a92f-479c348ac905.jpeg</url>
      <title>Forem: Sudarshan Jadhav</title>
      <link>https://forem.com/sudarshan01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sudarshan01"/>
    <language>en</language>
    <item>
      <title>Deploying a Java Spring Application on AWS EC2</title>
      <dc:creator>Sudarshan Jadhav</dc:creator>
      <pubDate>Thu, 29 Feb 2024 08:09:18 +0000</pubDate>
      <link>https://forem.com/sudarshan01/deploying-a-java-spring-application-on-aws-ec2-15jp</link>
      <guid>https://forem.com/sudarshan01/deploying-a-java-spring-application-on-aws-ec2-15jp</guid>
      <description>&lt;h3&gt;
  
  
  Configure AWS Resources
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Create an EC2 instance and select the necessary configurations based on the requirements of your applications.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Configure security group as follows:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Click on "Edit inbound rules" or "Add rule" to add the following rules:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;HTTP (Port 80)&lt;/li&gt;
&lt;li&gt;HTTPS (Port 443)&lt;/li&gt;
&lt;li&gt;SSH (Port 22)&lt;/li&gt;
&lt;li&gt;Tomcat (port 8080 )&lt;/li&gt;
&lt;li&gt;MySQL (port 3306)&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Configure MySQL
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Wherever your MySQL database resides, configure the mysqld.cnf file to specify the bind address for EC2, create a user, grant them all privileges for CRUD operations, and ensure the password is secure.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Configure Java Application
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Configure Spring applications application.properties/yml files to utilize the public IP of the EC2 instance and MySQL database username and password along with database name.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Connect to the instance :
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Using either a web console or SSH using the command below.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;ssh -i &amp;lt;path_to_private_key&amp;gt;&amp;lt;username&amp;gt;@&amp;lt;public_ip_or_dns_of_ec2_instance&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Download and Install JDK :&lt;/strong&gt;&lt;br&gt;
1.For Amazon Linux 2023&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo yum update -y
sudo yum install java-17-amazon-corretto -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.For Ubuntu&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update -y
sudo apt install openjdk-17-installer -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.For Windows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.Install chocolatey package manger 

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

2.Install using chocolatey
choco install -y jdk17
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Make a build file (jar) of a java Application
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.Using Gradle
gradle build

2.Using Maven
mvn clean package

3.Using Editor 
You can easily make build file using Intellij or sts tool 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Copy a jar file To some location
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Copy a jar file to S3 bucket&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy jar file from local machine to remote :&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scp  -i {PATH_TO_YOUR_PRIVATE_KEY} {LOCAL_JAR_PATH} {USERNAME}@{REMOTE_SERVER_IP}:{REMOTE_JAR_PATH}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Push Code To Github and then by cloning repository make &lt;br&gt;
jar file in ec2 instance.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By using any of the method copy a file to ec2-instance&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Deploying a java Application
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 1. Normal deployment:
java -jar /path/to/your/application.jar

 2. Background deployment (with nohup):
nohup java -jar /path/to/your/application.jar &amp;amp;

 3. Creating a systemd service:
sudo tee /etc/systemd/system/myapp.service &amp;lt;&amp;lt; EOF
[Unit]
Description=My Spring Boot Application
After=syslog.target

[Service]
User=your_username  # Replace with the appropriate user
ExecStart=/usr/bin/java -jar /path/to/application.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target
EOF


&amp;gt; sudo systemctl start myapp
&amp;gt; sudo systemctl enable myapp

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Thank You 😊 *&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>aws</category>
      <category>springboot</category>
      <category>ec2</category>
    </item>
  </channel>
</rss>
