
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. πβοΈ
βοΈ 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 22from My IP, not0.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.67The 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.pemImportant: 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 -yapt update refreshes the package information, while apt upgrade installs available package updates.Then install Nginx:
1
sudo apt install nginx -yStart and enable it:
1
2
sudo systemctl start nginx
sudo systemctl enable nginxCheck:
1
sudo systemctl status nginxIf 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
/varThese 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:
| Directory | Purpose |
|---|---|
| /home | Normal users' files |
| /etc | System/application configuration |
| /var | Logs, caches and changing application data |
| /usr | Programs, libraries and documentation |
| /var/www/html | Common default Nginx web content location |
| /var/log | System and application logs |
| /tmp | Temporary files |
| /root | Root user's home directory |
| /boot | Bootloader 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.htmlAdd:
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 nginxThis 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
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.htmlNow 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
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article