Launch Your First Cloud Server with EC2
Launch a Linux server on EC2, connect via SSH, and serve a live webpage.
Introduction
30 Second Summary
Every website you visit and every app you use runs on a server somewhere in the world. Most people never see those servers. In this project, you get to launch one yourself.
In this project, you will launch a Linux server on AWS, connect to it remotely from your Mac using SSH, and install a web server that hosts a custom webpage accessible from any browser.
What You'll Build
You will launch an Amazon Linux 2023 EC2 instance, connect to it via SSH, and serve a custom webpage using Nginx.
By the end of this project, you'll have:
- A running EC2 instance (t2.micro, free tier) that you launched and configured yourself.
- A key pair and security group protecting your server with secure access rules.
- A live custom webpage served by Nginx, accessible from any browser using your server's public IP address.
- Secret Mission: Create multiple pages and directories served by Nginx with custom URL routes.
Are there any prerequisites?
You need an AWS account and a Mac with Terminal (which comes built in). No additional software is required.
Create a Key Pair for Secure Access
To connect to a cloud server, you need a way to prove your identity. Passwords are one option, but they can be guessed or stolen.
SSH key pairs are a more secure alternative. In this step, you will create a key pair in EC2 and store the private key safely on your Mac so you can use it to log in to your server later.
In this step, get ready to:
- Navigate to the EC2 Key Pairs console in AWS.
- Create an ED25519 key pair named my-ec2-key.
- Move the private key file to a safe location and lock down its permissions.
Navigate to the EC2 Key Pairs console
A key pair consists of a public key (stored by AWS) and a private key (stored on your computer). When you connect to your server, AWS checks that your private key matches the public key it has on file.
- Sign in to the AWS Management Console at https://console.aws.amazon.com.
- In the search bar at the top, type EC2 and click on EC2 in the results.
- Check the region displayed in the top-right corner of the console. Select a region close to you (e.g., US East (N. Virginia)).
- In the left navigation pane, scroll down to Network & Security.
- Click Key Pairs.
Why does region matter?
AWS has data centers around the world grouped into regions. Choosing a region close to you reduces the delay (latency) when you connect to your server.
All the resources you create in this project need to be in the same region. Make a note of which region you chose.
Create your key pair
- Click Create key pair in the top-right corner.
- In the Name field, enter my-ec2-key.
- For Key pair type, select ED25519.
- For Private key file format, select .pem.
- Click Create key pair.
A file called my-ec2-key.pem downloads automatically to your Mac. This is your private key.
What do these settings mean?
- ED25519 is a modern key type that is more secure and generates shorter keys than the older RSA option. AWS recommends it for Linux instances.
- .pem is the file format that works directly with the SSH client built into macOS. Windows users would choose .ppk instead.
Treat this file like a house key
Anyone who has your .pem file can access your server. Never share it, commit it to Git, or upload it anywhere.
Secure your private key file
Your private key downloaded to the Downloads folder, but SSH requires keys to be stored in a specific location with restricted permissions. If the permissions are too open, SSH will refuse to use the key.
- Press Cmd+Space to open Spotlight search.
- Type Terminal and press Enter to open it.
- Move your key file to the ~/.ssh directory and lock its permissions by running these commands:
mv ~/Downloads/my-ec2-key.pem ~/.ssh/my-ec2-key.pem
chmod 400 ~/.ssh/my-ec2-key.pem
What do these commands do?
- mv moves the file from your Downloads folder into ~/.ssh, a hidden directory where SSH looks for keys by default.
- chmod 400 sets the file to read-only for you and no access for anyone else. SSH enforces this restriction as a security measure.
- Confirm the file is in place by running:
ls -la ~/.ssh/my-ec2-key.pem
You should see a line showing -r-------- at the start (read-only permissions) and the filename my-ec2-key.pem at the end.
Seeing 'No such file or directory'?
If the mv command failed, check these common causes:
- The ~/.ssh directory might not exist yet. Create it by running mkdir -p ~/.ssh and then retry the mv command.
- Your browser may have saved the file with a different name (e.g., my-ec2-key.pem.txt). Run ls ~/Downloads/my-ec2* to see the exact filename and adjust your command.
- If your browser saves downloads to a different folder, replace ~/Downloads/ with the correct path.
help me fix a 'No such file or directory' error when moving my .pem key file
Your key pair is created and secured. Next up, you will use it to launch your very own EC2 instance.
Launch Your EC2 Instance
Your key pair is ready. Now it is time to launch the actual server.
An EC2 instance is a virtual computer running in the cloud. You choose the operating system, the hardware size, and the network rules that control who can connect to it.
In this step, get ready to:
- Launch an EC2 instance running Amazon Linux 2023.
- Select the free tier instance type and attach your key pair.
- Create a security group that allows SSH and HTTP traffic.
Start the launch wizard and name your instance
The EC2 launch wizard walks you through every configuration option for your new server. You will start by giving it a name and choosing an operating system.
- From the EC2 dashboard, click Launch instance.
- Under Name and tags, enter my-first-server in the name field.
- Under Application and OS Images (Amazon Machine Image), keep the default Amazon Linux 2023 AMI selected.
You should see a "Free tier eligible" badge next to the AMI. This means running it will not cost you anything within the free tier limits.
What is an AMI?
An AMI (Amazon Machine Image) is a template that contains the operating system and pre-installed software for your server. Think of it as a snapshot that EC2 uses to create your virtual machine.
Amazon Linux 2023 is AWS's own Linux distribution. It is optimized for running on EC2 and comes pre-configured with tools you will need later.
Select the instance type and key pair
The instance type determines how powerful your server is. For this project, you only need the smallest option.
- Under Instance type, select t2.micro from the dropdown.
- Confirm that t2.micro shows a "Free tier eligible" label next to it.
- Under Key pair (login), select my-ec2-key from the dropdown.
Why t2.micro?
The t2.micro instance type gives you 1 virtual CPU and 1 GB of memory. That is more than enough for a simple web server. It is covered by the AWS Free Tier, which gives you 750 hours per month at no cost.
Configure network settings and launch
A security group acts as a firewall for your instance. It controls which traffic is allowed in and out. You need two rules: one to let you connect via SSH, and one to let web browsers reach your server.
- Scroll down to Network settings and click Edit.
- Keep the default VPC selected.
- Under Firewall (security groups), select Create security group.
- You should already see a default SSH rule. Set its Source type to My IP.
- Click Add security group rule.
- For the new rule, set Type to HTTP.
- Set the HTTP rule's Source type to Anywhere (0.0.0.0/0).
- Scroll to the bottom of the page and click Launch instance in the Summary panel on the right.
You should see a success banner that says "Successfully initiated launch of instance". Your server is now booting up in the cloud.
Why is SSH limited to My IP but HTTP is open to everyone?
SSH gives you full control over the server, so you only want your own computer to be able to connect. Limiting it to "My IP" means only your current IP address can reach port 22.
HTTP is for your website, which you want anyone in the world to visit. Setting it to 0.0.0.0/0 means any IP address can reach port 80.
Don't see my-ec2-key in the key pair dropdown?
Make sure you are launching the instance in the same region where you created the key pair. Check the region selector in the top right corner of the AWS console.
If you are in the correct region and still don't see it, go back to Network & Security > Key Pairs in the left sidebar to verify the key pair exists.
Help me find my key pair in the EC2 launch wizard.
Your server is now spinning up in the cloud. Next, you will connect to it remotely from your Mac using SSH and start running commands on your very own Linux machine.
Connect to Your Server via SSH
Your server is launched and running in the cloud. Now you need a way to control it remotely from your Mac.
SSH is a protocol that lets you securely connect to a remote computer and type commands as if you were sitting right in front of it. In this step, you'll use SSH to log into your EC2 instance and explore it with basic Linux commands.
In this step, get ready to:
- Find your instance's Public IPv4 address.
- Connect to your server remotely using SSH.
- Run Linux commands on your cloud server.
Find your instance's public IP address
Every EC2 instance gets a public IP address that acts as its unique address on the internet. You need this address to connect to your server.
- In the left navigation pane of the EC2 console, click Instances under Instances.
- Wait until your my-first-server instance shows Running in the Instance state column with a green status check.
- Click on your instance name to open its details.
- In the instance summary, find the Public IPv4 address field and click the copy icon next to it.
How long does it take to start?
Your instance typically takes 1-2 minutes to go from "Pending" to "Running" with a green status check. If it still shows "Pending", click the refresh icon in the top right of the instances table.
Connect to your server from Terminal
Your Mac has a built-in SSH client in Terminal. You'll use the key pair file you saved earlier to prove your identity to the server.
- Switch to your Terminal window (or open it by pressing Cmd+Space, typing Terminal, and pressing Enter).
- Connect to your EC2 instance by running this command:
ssh -i ~/.ssh/my-ec2-key.pem ec2-user@[[EC2_PUBLIC_IP="your instance's Public IPv4 address"]]
What does this command do?
- ssh starts a secure connection to a remote computer.
- -i ~/.ssh/my-ec2-key.pem tells SSH which private key file to use for authentication.
- ec2-user is the default username on Amazon Linux 2023 instances.
- The IP address after @ tells SSH which server to connect to.
- When you see "Are you sure you want to continue connecting (yes/no/[fingerprint])?", type yes and press Enter.
What is the fingerprint prompt?
The first time you connect to any server, SSH asks you to verify the server's identity. This is a one-time check. After you type yes, your Mac saves the server's fingerprint and won't ask again.
You should see your Terminal prompt change to something like [ec2-user@ip-172-31-xx-xx ~]$. You are now logged into your cloud server.
Connection refused or timed out?
- Double-check that you pasted the correct Public IPv4 address from the EC2 console (not the Private IP).
- Make sure your instance state shows "Running" with a green status check. If it still says "Pending", wait another minute.
- Verify your security group has an inbound SSH rule with Source set to "My IP". If your IP address changed (e.g., you switched Wi-Fi networks), update the rule.
- If you see "Permission denied", confirm the key file is at ~/.ssh/my-ec2-key.pem and has 400 permissions.
Help me troubleshoot my SSH connection to EC2
Explore your cloud server
You are now controlling a computer that lives in an AWS data center. Let's run a few commands to prove it.
- Run these commands to explore your server:
whoami
pwd
ls
cat /etc/os-release
What do these commands show?
- whoami prints your username. You should see ec2-user.
- pwd prints your current directory. You should see /home/ec2-user.
- ls lists files in the current directory (it may show nothing since this is a fresh server).
- cat /etc/os-release displays the operating system info. You should see "Amazon Linux 2023" in the output.
You should see output confirming you are ec2-user on an Amazon Linux 2023 machine. This is your server.
Not seeing the expected output?
- Make sure your Terminal prompt shows [ec2-user@...] and not your Mac's local prompt. If you see your Mac's username, the SSH connection did not succeed.
- If your Terminal is unresponsive, press Ctrl+C and try the SSH command again.
Help me verify I'm connected to my EC2 instance
You now have full remote access to a Linux server running in the cloud. Next up, you'll install a web server on it and serve your first webpage to the internet.
Install and Start Nginx Web Server
You have a running server and you can control it remotely. Now it is time to make it do something useful.
In this step, you will install Nginx, a popular web server that can serve web pages to anyone who visits your server's public IP address.
In this step, get ready to:
- Update your server's system packages.
- Install and start Nginx.
- Verify your server is serving a live webpage.
Update system packages and install Nginx
Before installing new software, it is good practice to update all existing packages on your server. This ensures you get the latest security patches and compatibility fixes.
- In your SSH session (you should still be logged in as ec2-user), update all system packages by running this command:
sudo dnf update -y
This may take a minute. You will see a stream of text as packages are checked and updated.
- Once the update finishes, install Nginx by running:
sudo dnf install -y nginx
What do these commands do?
sudo runs the command as a superuser (administrator). You need elevated permissions to install software on Linux.
dnf is the package manager for Amazon Linux 2023. It downloads and installs software from Amazon's repositories. The -y flag automatically answers "yes" to any confirmation prompts.
You should see output ending with "Complete!" confirming Nginx is installed.
Seeing "No match for argument: nginx"?
Make sure you typed nginx in lowercase. Package names are case-sensitive on Linux.
If the update step failed earlier, re-run sudo dnf update -y first, then try the install command again.
help me troubleshoot why dnf install nginx is failing on my Amazon Linux 2023 EC2 instance
Start Nginx and enable it on boot
Installing Nginx does not automatically start it. You need to start the service and tell Linux to keep it running whenever the server reboots.
- Start Nginx, enable it on boot, and check its status by running these three commands:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
What do these commands do?
systemctl is the Linux tool for managing services (programs that run in the background).
- start nginx launches Nginx right now.
- enable nginx tells Linux to start Nginx automatically whenever the server boots up.
- status nginx shows whether Nginx is currently running.
You should see "active (running)" highlighted in green in the status output. This confirms Nginx is up and serving traffic.
Exit the status view
The status command may take over your terminal with a scrollable view. Press q to exit and return to the command prompt.
Status shows "inactive" or "failed"?
Run sudo systemctl start nginx again and check for error messages in the output.
If you see a "port already in use" error, another service may be occupying port 80. Run sudo systemctl status httpd to check if Apache is running, and stop it with sudo systemctl stop httpd if it is.
help me troubleshoot why Nginx is not starting on my EC2 instance
Verify your webpage is live
Your web server is running. The security group you created in Step 2 already allows HTTP traffic on port 80. That means anyone on the internet can now visit your server's IP address and see a webpage.
- Open a web browser on your Mac (Safari, Chrome, or Firefox).
- In the address bar, type http:// followed by your instance's Public IPv4 address (the same one you used for SSH) and press Enter.
You should see the default Nginx welcome page. It says "Welcome to nginx!" with some basic information about the server.
What just happened?
Your browser sent an HTTP request to your EC2 instance's public IP address on port 80. The security group allowed the traffic through. Nginx received the request and returned its default welcome page.
You are now serving a live website from your own cloud server. Anyone with the IP address can see this page.
Page won't load or times out?
Make sure you are using http:// and NOT https://. Your server does not have an SSL certificate, so HTTPS will not work.
Check that your security group has an inbound rule allowing HTTP (port 80) from 0.0.0.0/0. Go to EC2 > Instances, click your instance, then the Security tab to review the rules.
Confirm Nginx is still running by going back to your SSH terminal and running sudo systemctl status nginx.
help me troubleshoot why I cannot see the Nginx page in my browser
You now have a fully functioning web server in the cloud. Next up, you will replace the default page with your own custom HTML.
Customize Your Webpage
Your server is live and serving a webpage. Right now it is showing the default Nginx welcome page, which proves the web server works but does not feel like yours yet.
In this step, you will replace the default page with a custom HTML page that you write yourself. By the end, anyone in the world can visit your server's IP address and see your personalized webpage.
In this step, get ready to:
- Back up the default Nginx page so you can restore it if needed.
- Create a custom HTML page with your own content and styling.
- Verify your custom page is live and accessible from any browser.
Navigate to the Nginx directory and back up the default page
Nginx serves files from a specific directory on your server. Before you overwrite anything, it is good practice to back up the original file so you can restore it later.
- In your SSH session on the EC2 instance, navigate to the Nginx HTML directory and create a backup of the default page by running these commands:
cd /usr/share/nginx/html
sudo cp index.html index.html.backup
What do these commands do?
- cd /usr/share/nginx/html moves you into the directory where Nginx looks for files to serve.
- sudo cp index.html index.html.backup creates a copy of the default page named index.html.backup. You need sudo because this directory is owned by the system, not your user.
- Confirm the backup exists by running:
ls
You should see both index.html and index.html.backup listed in the output.
Permission denied?
Make sure you included sudo before the cp command. Without it, your regular user does not have permission to write to this system directory.
Help me fix a permission denied error when copying files on my EC2 instance.
Create your custom HTML page
Now you will replace the default Nginx page with your own custom HTML. You will use nano, a simple text editor that runs directly in your terminal.
- Open index.html in the nano editor by running:
sudo nano index.html
Your terminal transforms into a text editor showing the contents of the default Nginx page.
- Delete all the existing content by holding Ctrl+K until every line is gone. This key combination deletes one line at a time.
- Paste the following HTML content into the empty editor:
<!DOCTYPE html>
<html>
<head>
<title>My First Cloud Server</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; background-color: #f0f8ff; }
h1 { color: #232f3e; }
p { color: #333; line-height: 1.6; }
.highlight { background-color: #ff9900; color: white; padding: 5px 10px; border-radius: 4px; display: inline-block; }
</style>
</head>
<body>
<h1>Hello from my EC2 Server!</h1>
<p>This page is being served by <span class="highlight">Nginx</span> running on an Amazon Linux 2023 instance in the cloud.</p>
<p>I launched this server, connected via SSH, and installed a web server. All from my Mac.</p>
</body>
</html>
What does this HTML do?
- The <style> block adds inline CSS that centers the page, sets a light blue background, and styles the text.
- The .highlight class creates an orange badge (AWS's signature color) around the word "Nginx".
- The page tells anyone who visits exactly what you accomplished: launched a server, connected via SSH, and installed a web server.
- Save the file by pressing Ctrl+O, then press Enter to confirm the file name.
- Exit nano by pressing Ctrl+X.
You are back at the regular terminal prompt. Your custom HTML is now saved as the new index.html.
Accidentally exited without saving?
If you pressed Ctrl+X before Ctrl+O, nano will ask "Save modified buffer?". Press Y then Enter to save. If you already exited without saving, just run sudo nano index.html again and paste the HTML a second time.
Help me save a file in nano on my EC2 instance.
Verify your custom page is live
- Switch to your web browser on your Mac.
- Refresh the page that shows your instance's Public IPv4 address (the same tab where you saw the default Nginx page earlier).
You should now see your custom page with the heading "Hello from my EC2 Server!" on a light blue background, with the word Nginx in an orange badge.
Still seeing the default Nginx page?
- Try a hard refresh: press Cmd+Shift+R to bypass the browser cache.
- Make sure you saved the file in nano (Ctrl+O then Enter) before exiting.
- Confirm the file has your HTML by running cat /usr/share/nginx/html/index.html in your SSH session. You should see your custom HTML, not the default Nginx content.
Help me troubleshoot why my custom page is not showing.
✔️ Awesome, I see my custom page!
Your custom webpage is live on the internet, served from a cloud server you built from scratch.
ⓧ I'd like to double check the full code
Here is the complete index.html file that should be at /usr/share/nginx/html/index.html on your server:
<!DOCTYPE html>
<html>
<head>
<title>My First Cloud Server</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; background-color: #f0f8ff; }
h1 { color: #232f3e; }
p { color: #333; line-height: 1.6; }
.highlight { background-color: #ff9900; color: white; padding: 5px 10px; border-radius: 4px; display: inline-block; }
</style>
</head>
<body>
<h1>Hello from my EC2 Server!</h1>
<p>This page is being served by <span class="highlight">Nginx</span> running on an Amazon Linux 2023 instance in the cloud.</p>
<p>I launched this server, connected via SSH, and installed a web server. All from my Mac.</p>
</body>
</html>
If your file does not match, run sudo nano /usr/share/nginx/html/index.html again, delete all content with Ctrl+K, and paste the correct HTML above.
You now have a fully personalized webpage running on your own cloud server. Anyone with your IP address can see it. Next up, a secret mission that takes your server skills even further.
Secret mission
Serve Multiple Pages with Custom Routes
Your server can do more than serve a single page. In this challenge, you will create multiple HTML pages and organize them into directories. Nginx serves them automatically without any extra configuration.
Clean Up Your Resources
Clean Up Your Resources
Decide whether to keep your resources running, pause them to come back later, or delete them entirely.
Your EC2 instance is covered by the AWS Free Tier (750 hours/month on legacy accounts, or free tier credits on newer accounts). However, if you leave it running and forget about it, it could eventually cost money after your free tier expires.
Cost warning
Your t2.micro instance is free while you have free tier eligibility. If your free tier expires (12 months after account creation for legacy accounts), a running t2.micro instance costs approximately $8.50/month.
If you are not planning to use this instance again soon, stop or terminate it to avoid unexpected charges.
Resources you used:
- 1 EC2 instance (t2.micro) running in your AWS account.
- 1 key pair (my-ec2-key) stored in EC2.
- 1 security group with SSH and HTTP inbound rules.
- 1 .pem file stored locally at ~/.ssh/my-ec2-key.pem.
Keep everything running
No action needed. Choose this if you are still actively building or want your website to stay live.
- Your website remains accessible at your instance's public IP address.
- Monitor your free tier usage in the AWS Billing console to make sure you stay within the free limits.
- Your key pair and security group remain active but cost nothing on their own.
Pause - I'll come back to this later
Stop your instance to save free tier hours while keeping your data intact. You can start it again later, but the public IP address will change.
- Go to the EC2 console at https://console.aws.amazon.com/ec2/.
- Click Instances in the left navigation pane.
- Select your my-first-server instance by checking its box.
- Click Instance state, then Stop instance.
- Confirm by clicking Stop.
- Your instance state changes to Stopped. Your data and configuration are preserved.
Delete - I don't want to use this again
Terminate your instance to permanently delete the server and all data on it. The key pair and security group remain but cost nothing.
- Go to the EC2 console at https://console.aws.amazon.com/ec2/.
- Click Instances in the left navigation pane.
- Select your my-first-server instance by checking its box.
- Click Instance state, then Terminate instance.
- Confirm by clicking Terminate.
- To also delete the key pair: go to Network & Security > Key Pairs, select my-ec2-key, click Actions > Delete.
- To delete the local key file, open Terminal and run:
rm ~/.ssh/my-ec2-key.pem
- To delete the security group: go to Network & Security > Security Groups, select the security group you created, click Actions > Delete security groups.
That's a Wrap!
That's a Wrap!
Nice work! You just launched, connected to, and configured your own Linux server in the cloud. That is a real DevOps skill you will use in every project from here on.
Here's what you accomplished:
- Launched an EC2 instance running Amazon Linux 2023 and connected to it remotely via SSH from your Mac's Terminal.
- Installed and started Nginx to serve a custom webpage from your cloud server, accessible from any browser.
- Configured security groups to control exactly who can access your server over SSH and HTTP.
- Secret Mission: Created multiple pages and directories served by Nginx with custom URL routes.
You are now ready to tackle the 6 Day DevOps Challenge. The first project uses EC2 in the same way you just practiced here.
Ready to quiz yourself?