The instruction below is only work after creating the EC2 Instance with Amazon Linux 2 AMI
1.Install FileZilla Client
2.Go to Edit/Settings/ choose SFTP and set the private key from AWS for the instance EC2
3.Copy the host address from AWS EC2 - Public IPv4 address
4.Go to File/Site Manager, set up like picture below
Prepare the Java File and Give Super User/Admid Permission Under the root directory, use 2 command lines
mkdir /usr/java
sudo su
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3a%2F%2Fwww.oracle.com%2Ftechnetwork%2Fjava%2Fjavase%2Fdownloads%2Fjdk8-downloads-2133151.html; oraclelicense=accept-securebackup-cookie;" "https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-linux-x64.rpm"
Step 1 – Install Java on Amazon Linux The OpenJDK 8 is available under default yum repositories and OpenJDK 11 is available under Amazon Linux 2 extras repositories. You can simply install Java 11 or Java 8 on the Amazon Linux system using the following commands.
Run below commands to install Java 11 on Amazon Linux:
sudo amazon-linux-extras install java-openjdk11
Run below commands to install Java 8 on Amazon Linux:
sudo yum install java-1.8.0-openjdk
Step 2 – Check Active Java Version
After successfully installing Java on Amazon Linux using the above steps, Let’s verify the installed version using the following command.
java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
Step 3 – Switch Java Version Use alternatives command-line utility to switch active Java version on your Amazon Linux system. Run below command from the command line and select the appropriate Java version to make it default
alternatives --config java
Install Java on Amazon Linux
After switching let’s check again active Java version:
java -version
openjdk version "11.0.7" 2020-04-14 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10-LTS, mixed mode, sharing)
-
Download Tomcat 9, Go to https://tomcat.apache.org/download-90.cgi
-
Under Core section, right-click on
tar.gz
and choosecopy link address
-
Open AWS Linux 2 Server, in urs/java directory, use
wget [link copied]
or
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50.tar.gz
-
Extract the file just downloaded
tar xvfz apache_File_name.tar.gz
or
tar xvfz apache-tomcat-9.0.50.tar.gz
-
After extract the file, remove it by command
rm -r apache-tomcat-9.0.50.tar.gz
then Type yes
to agree to remove
-
Start the application Server
6.1. Under the Apache directory, go to bin by
cd bin
or Under the root directory, start the server
cd /usr/java/apache-tomcat-9.0.46/bin ./startup.sh
6.2. Connect tomcat
ps -ef | grep tomcat
6.3.
wget http://localhost:8080
At this point, you should get the result like the image below
-
Change the permission to modify the file by running 2 command 7.1.Under Java directory
chmod -R 777 apache-folder
or
cd cd .. cd /usr/java chmod -R 777 apache-tomcat-9.0.46
7.2. Under apache-tomcat-9.0.46 folder
cd apache-tomcat-9.0.46 chmod -R 777 conf
-
Get Access to Manager App on Tomcat 8.1. Under the Apache Folder, modify the context.xml file by command
vi webapps/manager/META-INF/context.xml
In the context.xml file, comment the line <!- - <value .... allow = .../> -->
8.2. Under the Apache Folder, modify the tomcat-users.xml file by command
cd cd .. cd /usr/java/apache-tomcat-9.0.46 vi conf/tomcat-users.xml
Add 2 lines inside block
<role rolename= "manager-gui" /> <user username= "tomcat" password = "s3cret" roles = "manager-gui" />
-
Now open web browers and run
http://localhost:8080/manager/html
1.Set up user data for AMI -> Create a user.sh
file
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
EC2ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
echo '<html> <center><h1> DATABASE</h1></center> <center> <h1>Amazon EC2 instance is: EC2ID </h1></center> <html>' > /var/www/html/index.txt
sed "s/EC2ID/$EC2ID/" /var/www/html/index.txt > /var/www/html/index.html
- Convert .pem to .ppk by puttygen
- Get the instance ssh
ec2-18-222-55-120.us-east-2.compute.amazonaws.com
- In category Connection/Data, type
ec2-user
in Auto-login username - In category Connection/SSH/Auth, choose private key file .ppk from step 1
- Connect to server
- run:
lsblk
Assumming that it showed as below:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 8G 0 disk\
└─xvda1 202:1 0 8G 0 part /
xvdf 202:80 0 1G 0 disk
xvdm 202:192 0 2G 0 disk
- Mount xvdf disk block to
mke2fs /dev/xvdf
sudo !!
mount /dev/xvdf /yourMountFileName
- Go into yourMountFileName\
cd yourMountFileName
vim newFile.txt
3.1 Type something in vim file
:wq
mkdir Home
4. Move newFile.txt to Home \
Source reference: https://gsviec.com/blog/amazon-s3-la-gi-va-tai-sao-ban-nen-dung-no/
-
Create an role in IAM
1.1 Give access to AmazonS3FullAcess
1.2 Give a name for the role: For example: ec2-s3-fullAccess 1.3 NOTE::Make sure your instance EC2 have this IAM role -
Go to
S3 on AWS
-
Create a bucket then naming for it
-
Upload a file from instance EC2 to bucket by CLI - linux
touch demo.txt
vim demo.txt
Press i
to edit your demo file, when you finish press Esc
and type: :wq
cp demo.txt
aws s3 cp demo.txt s3://adam.test.buckket/hello.txt
Use the following command to copy an object from Amazon S3 to your instance.
[ec2-user ~]$
aws s3 cp
s3://my_bucket/my_folder/my_file.ext
my_copied_file.ext
Use the following command to copy an object from your instance back into Amazon S3.
[ec2-user ~]$
aws s3 cp
my_copied_file.ext
s3://my_bucket/my_folder/my_file.ext
NOTE: If we want to make our bucket be public then add this rule to policy
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
}
One more example from amazon
The following example bucket policy shows the preceding policy elements.
The policy allows Dave, a user in account Account-ID, s3:GetObject, s3:GetBucketLocation, and s3:ListBucket Amazon S3 permissions on the awsexamplebucket1 bucket.
{
"Version": "2012-10-17",
"Id": "ExamplePolicy01",
"Statement": [
{
"Sid": "ExampleStatement01",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:user/Dave"
},
"Action": [
"s3:GetObject",
"s3:GetBucketLocation",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::awsexamplebucket1/*",
"arn:aws:s3:::awsexamplebucket1"
]
}
]
}