AWS : Connect EC2 in Terminal (Ubuntu) & Installing Apache2.
Published: 2023-11-20 01:37:52

**Prerequisite :**

1. Make sure the EC2 instance running in AWS.
2. Key-pair to connect EC2

Step#1 :

Open the terminal in Ubuntu OS and move to directory where *.pem file saved in local. Then verify the file permission of the file. 

`devops@kaniyam:~/Desktop/Ponnarasu/Devops$ stat -c %a Ubuntu_Demo_server_key.pem 
644`

Step#2 : 
Change the File permission to 600, which mean, only owner has access to read and write file and other doesn't have access. 

`devops@kaniyam:~/Desktop/Ponnarasu/Devops$ chmod 600 Ubuntu_Demo_server_key.pem 
devops@kaniyam:~/Desktop/Ponnarasu/Devops$ stat -c %a Ubuntu_Demo_server_key.pem 
600
`

Step#3 : 
Connecting EC2 instance in Terminal by SSH.( SSH terminal should be open in EC2 ).

`devops@kaniyam:~/Desktop/Ponnarasu/Devops$ **ssh -i Ubuntu_Demo_server_key.pem  ubuntu@3.98.120.128**
The authenticity of host '3.98.120.128 (3.98.120.128)' can't be established.
ED25519 key fingerprint is SHA256:lNe/eooCjYSwIbu3p5HkbkB+WoW6I897bn0TaCBAYss.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '3.98.120.128' (ED25519) to the list of known hosts.
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 6.2.0-1012-aws x86_64)
`
Step#4 :
Update the Ubuntu OS 

`ubuntu@ip-172-31-1-176:~$ **sudo apt-get update**
Hit:1 http://ca-central-1.ec2.archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://ca-central-1.ec2.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
`

Step#5 :
Set/update the root password using below command.

`ubuntu@ip-172-31-1-176:~$ **sudo passwd root**
New password: 
Retype new password: 
passwd: password updated successfully
`
Step#6 :  Command to find the Public IP in terminal.

`ubuntu@ip-172-31-1-176:~$ **curl ifconfig.me**
3.98.120.128

ubuntu@ip-172-31-1-176:~$ **curl icanhazip.com**
3.98.120.128

ubuntu@ip-172-31-1-176:~$ **curl http://checkip.amazonaws.com**
3.98.120.128
`


**Installing APACHE2 in Instance.** 

`buntu@ip-172-31-1-176:~$ sudo apt install apache2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:`

Start : 
`ubuntu@ip-172-31-1-176:~$ sudo systemctl start apache2
ubuntu@ip-172-31-1-176:~$ sudo systemctl enable apache2
`

Status : 
`ubuntu@ip-172-31-1-176:~$ sudo systemctl status apache2
`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ixe950n58ao4th5wk20f.png)

Open the Apache Webserver in Firefox. 

Failed to Open. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ppqs39erchpu4j508hk9.png)

Cause : 
Only SSH port is on the EC2 Instance, so Apache server not connecting.

Adding http: port in the security group . 

Step#1 : 
Existing Security Group : 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovqkectijnmqkm2avr8d.png) 

Step#2 : 
Add rule by clicking the "Add Rule" button. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3s98rugh9szi2h2e9e4u.png)

Step#3 :
 Choose  the Type as "HTTP" and select source as "0.0.0.0/0" to connect the public IP from anywhere. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6megr4cctdcqmpdy1qx9.png)

After adding the New security rule.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gl5hpmfyqbos9s9ijhtk.png)

Refresh the Apache page : 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i3nn4155kds5ahiq3rbh.png)