AWS Builder Center
Building an Ubuntu EC2 VM from Scratch Step by Step β€” SSH, Linux & Nginx also Learned Installation Using Script πŸš€β˜οΈ

Building an Ubuntu EC2 VM from Scratch Step by Step β€” SSH, Linux & Nginx also Learned Installation Using Script πŸš€β˜οΈ

My AWS learning journey is now moving from understanding EC2 to actually configuring and using an EC2 Linux server. In this article, I’ll walk through the complete process of launching an Ubuntu EC2 instance, connecting to it through SSH, using it from CMD/Bash/VS Code, updating packages, installing Nginx, understanding the Ubuntu filesystem, and finally automating the setup with EC2 User Data. πŸš€β˜οΈ

Cloud Data Engineer

☁️ 1. Launch an Ubuntu EC2 Instance

From the EC2 console, select Launch Instance.
For a learning environment, I would typically choose:
  • AMI: Ubuntu Server LTS
  • Instance type: A small instance appropriate for your account's current Free Tier eligibility
  • Key pair: Create/select an SSH key pair
  • VPC: Your intended VPC
  • Subnet: Public subnet if directly connecting over the internet
  • Public IPv4: Enabled for a simple internet-accessible lab
  • Security Group: Allow SSH TCP 22 from My IP, not 0.0.0.0/0
  • HTTP TCP 80: Allow from the internet only if this VM will serve a public website
The Ubuntu AMI uses ubuntu as the default SSH username. (AWS DocumentationΒ )

πŸ” 2. Connect Using SSH

Once the instance is running, copy its Public IPv4 address or Public DNS.
From Linux, macOS, Windows CMD, or PowerShell:
1
ssh -i "my-key.pem" ubuntu@<PUBLIC-IP>
For example:
1
ssh -i "my-key.pem" ubuntu@54.123.45.67
The private key must correspond to the key pair selected when the instance was launched. (AWS DocumentationΒ )
On Linux/macOS, protect the private key:
1
chmod 400 my-key.pem
Important: Never upload your private .pem key to GitHub or share it publicly.

πŸ’» 3. Connect Through VS Code

VS Code makes remote development much easier.
Install the Remote - SSH extension, then:
Ctrl + Shift + P β†’ Remote-SSH: Connect to Host
Add your Ubuntu host:
1
ssh -i "C:\path\my-key.pem" ubuntu@<PUBLIC-IP>
VS Code can then connect to the EC2 server and let you open the remote filesystem, edit files, use the terminal, and work with the server almost like a local development machine. (Visual Studio CodeΒ )

πŸ”„ 4. Update Ubuntu Packages

After connecting:
1
2
sudo apt update
sudo apt upgrade -y
apt update refreshes the package information, while apt upgrade installs available package updates.
Then install Nginx:
1
sudo apt install nginx -y
Start and enable it:
1
2
sudo systemctl start nginx
sudo systemctl enable nginx
Check:
1
sudo systemctl status nginx
If port 80 is allowed in the Security Group, open:
1
http://<PUBLIC-IP>
You should see the Nginx welcome page.

πŸ“ 5. Understanding Ubuntu's Predefined Folders

When you run:
1
ls /
you'll see directories such as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/bin
/boot
/dev
/etc
/home
/lib
/mnt
/opt
/root
/run
/sbin
/srv
/tmp
/usr
/var
These aren't random folders. Linux follows established filesystem conventions so applications, administrators, and system tools know where different types of files belong. (UbuntuΒ )
Some important ones:
DirectoryPurpose
/homeNormal users' files
/etcSystem/application configuration
/varLogs, caches and changing application data
/usrPrograms, libraries and documentation
/var/www/htmlCommon default Nginx web content location
/var/logSystem and application logs
/tmpTemporary files
/rootRoot user's home directory
/bootBootloader and kernel-related files
For example, Nginx configuration can be managed under /etc/nginx/, website content can be placed under /var/www/html/, and Nginx logs can be found under /var/log/nginx/.
The key lesson is:
Don't modify system folders randomly. Know what a directory is responsible for before changing its contents.

πŸ› οΈ 6. Modifying a Website

You can create a simple webpage:
1
sudo nano /var/www/html/index.html
Add:
1
2
<h1>Hello from my AWS Ubuntu EC2 Server!</h1>
<p>My first cloud web server.</p>
Save it and refresh the browser.
For configuration changes, edit the appropriate file under /etc, then validate and reload the relevant service.
For example:
1
2
sudo nginx -t
sudo systemctl reload nginx
This is much safer than randomly changing files under /usr or /lib.

πŸš€ 7. Automate Everything with EC2 User Data

Now comes my favorite part: automation.
Instead of manually connecting and running every command, EC2 allows us to provide a User Data script during launch. AWS supports shell scripts for Linux instances, and they normally run during the initial launch. User Data scripts run as root, so sudo isn't required inside the script. (AWS DocumentationΒ )
In Advanced Details β†’ User Data, enter:
1
2
3
4
5
6
7
8
9
10
#!/bin/bash

apt update -y
apt upgrade -y
apt install nginx -y

systemctl enable nginx
systemctl start nginx

echo "<h1>Hello from AWS EC2 Ubuntu!</h1>" > /var/www/html/index.html
Now the workflow becomes:
Launch EC2 β†’ Ubuntu starts β†’ User Data runs β†’ Packages update β†’ Nginx installs β†’ Website starts
After connecting, if you want to troubleshoot User Data execution, AWS documents the cloud-init output at:
1
/var/log/cloud-init-output.log

🎯 My Biggest Takeaway

This exercise connected everything for me:
AWS EC2 β†’ VPC β†’ Security Group β†’ SSH β†’ Ubuntu β†’ Linux Filesystem β†’ Nginx β†’ Automation
The real power of cloud isn't just creating a virtual machine.
It's being able to secure it, configure it, automate it, and turn the same process into a repeatable deployment.
That's the mindset I'm developing as I continue my AWS Solutions Architect journey. β˜οΈπŸš€
#AWS #AWSBuilderCenter #AWSCommunity #AmazonEC2 #Ubuntu #Linux #Nginx #CloudComputing #SolutionsArchitect #AWSCloud #AWSNetworking #SSH #VSCode #CloudArchitecture #DevOps #LearningInPublic #AWSJourney
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article