Create Ubuntu Server on AWS EC2 Instance
Last Updated :
12 Jul, 2024
In this article, we'll take you through the entire process of creating an Ubuntu server on an AWS EC2 instance from scratch. Whether you're new to Amazon Web Services (AWS) or just looking to set up a new server, this step-by-step tutorial will cover everything you need to know. We'll start right from the beginning and guide you all the way to launching your instance.
Amazon EC2, or Elastic Compute Cloud, is a cloud computing service provided by AWS. It allows you to run your applications on EC2 servers without needing to manage the underlying hardware. You can set up EC2 instances securely using VPCs, subnets, and security groups. Additionally, you can adjust the size and capacity of your EC2 instances based on your application's needs by using autoscaling groups. This lets you scale up or down depending on the traffic your application receives.
Steps to Create Ubuntu Server on EC2 Instance
There are the following steps to create an Ubuntu server on an AWS EC2 instance:-
Step 1: First, log in to your AWS account. If you don't have one, you can create it on the official AWS website. Once logged in, search for “EC2” in the search bar, which will take you to the EC2 dashboard where you can see an overview of your instances. Click the “Launch Instance” button on this page to start configuring your EC2 instance.
Configure InstanceStep 2: Now, choose an Amazon Machine Image (AMI), specifically Ubuntu, and select the instance type. We will select the General Purpose t2.micro instance because it is eligible for the free tier.
Step 3: In the Key pair (login) section, you can select an existing key pair or create a new one.
Step 4: In the Network section, use the default VPC and select an existing security group or create a new one. We will add HTTP, HTTPS, and SSH ports for the EC2 instance. HTTP and HTTPS will allow web requests from anywhere in the world, and SSH will enable connection to the EC2 instance from a local machine.
Step 5: In the Configure Storage section, configure the storage size and type. Remember that only up to 30 GB of gp3 storage is eligible for the free tier.
Step 6: Click the “Launch Instance” button to launch the EC2 instance.
Finally, your Ubuntu server on the AWS EC2 instance is now created. You can deploy your applications on it and connect to it from your local machine using SSH.
Configuring Ubuntu Server
Accessing Instance via SSH:
To connect an AWS Ubuntu EC2 Instance with your local machine using SSH, follow these steps:
Step 1: Install the SSH Client on Your Local Machine
Ensure that you have an SSH client installed on your local machine. For most Unix-based systems (Linux, macOS), an SSH client is already available. For Windows, you might use tools like PuTTY or Windows Subsystem for Linux (WSL).
Step 2: Go to the Directory Where the Generated Key Pair is Stored
Navigate to the directory where your key pair was downloaded.
Step 3: Ensure Your Key is Not Publicly Viewable
Run the following command to set the appropriate permissions for your key pair:
chmod 400 "<Key-pair-name.pem>"
Note: Replace "<Key-pair-name>" with the name of your downloaded key pair file.
Step 4: Connect to Your EC2 Instance Using SSH
- Go to the AWS Management Console and select the instance you want to connect to.
- Click on the "Connect" button.
- In the "Connect to instance" dialog, select the "SSH client" tab.
- Copy the example command shown
- Run the copied command in your terminal to connect to your EC2 instance.
The Example will looks like this:
ssh -i "ubuntuGFG.pem" ubuntu@ec2-52-54-84-138.compute-1.amazonaws.com
Note: Replace "ubuntuGFG.pem" with the name of your key pair file if it is different. The username ubuntu is typically used for Ubuntu instances on AWS. Adjust the command if your instance uses a different default username (e.g., ec2-user for Amazon Linux).
Initial Server Setup:
Once you have connected to the instance using SSH, you should update the packages and set up a firewall for security. Creating a new user is not strictly necessary but recommended.
1. Update Packages
First, update the package list and upgrade all installed packages to their latest versions.
sudo apt update
sudo apt upgrade -y
2. Set Up a Firewall
Use ufw (Uncomplicated Firewall) to set up a firewall. By default, allow only SSH (port 22) and then enable the firewall.
sudo apt install ufw -y
sudo ufw allow OpenSSH
sudo ufw enable
You can also allow other ports as needed. For example, to allow HTTP (port 80) and HTTPS (port 443):
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
3. Create a New User with Sudo Privileges
Create a new user and add them to the sudo group to grant them administrative privileges.
Create a new user:
sudo adduser newusername
Follow the prompts to set the password and other details for the new user.
Add the new user to the sudo group:
sudo usermod -aG sudo newusername
Deploying and Managing Applications:
Installing and Configuring Software:
To install the required common software packages and services like Apache, MySQL, and PHP, follow these steps:
1. Install Apache
sudo apt update
sudo apt install apache2 -y
Once installed, you can enable Apache to start on boot and verify that it is running:
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2
Note: You can verify that Apache is working by visiting your server's public IP address in your web browser. You should see the default Apache welcome page.
2. Install MySQL
sudo apt install mysql-server -y
sudo mysql_secure_installation
3. Install PHP
sudo apt install php libapache2-mod-php php-mysql -y
Similar Reads
How to Create AWS Instance Scheduler ?
Sometimes the AWS EC2 instances are created unnecessarily, causing an unwanted bill where the resources were not used and we still have to pay for them. In such scenarios, an instance scheduler comes in handy, to avoid the hassle of redundant and extra instances and to help save money. In this artic
5 min read
How To Create Redhat EC2 Instance in AWS
provisioning the Red Hat Enterprise Linux (RHEL) instances on Amazon Web Services (AWS) offers a powerful and versatile solution for hosting and running applications, overseeing the jobs, and utilizing the abilities of both platforms. Red Hat Enterprise Linux is a main Linux distribution eminent for
5 min read
How to Install AWS CLI on Ubuntu?
The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts. Here are the step-by-step instructions to install AWS CLI on UbuntuStep 1:
3 min read
What is AWS EC2 Instance Storage?
EC2 Instance storage is the temporary block storage service offered by AWS. EC2 instance storage, in itself, is not a storage service but a part of the EC2 service. These storage devices physically lie on the same host that provides the EC2 instance and are useful for storing temporary data associat
11 min read
How to Set Up Apache Web Server in AWS EC2 Linux (Ubuntu) Instance?
In this article, we will look into the process of setting up Apache Web Server in AWS EC2 Linux Instance.This tutorial has been done on a system running Windows 10 Home (Version 20H2).Implementation:The steps taken to complete this tutorial are being stated below:Step 1: Go to portal.aws.amazon.com
4 min read
Jenkins Master And Slave Configuration Using AWS EC2 Instance
Optimizing the jenkins performance enhances the scalability by configuring the master-slave architecture using AWS EC2 Instances. Distributed workloads efficiently secures seamless CI/CD process for your projects. In this Article you will guide how to configure jenkins master-slave nodes using AWS E
5 min read
Run Commands on EC2 Instance Remotely
This article explores the process of running commands on EC2 instance remotely. AWS Systems Manager is a Management Tool that enables you to gain operational insights and take action on AWS resources safely and at scale. AWS Systems Manager is an always free tier product. The EC2 instance you create
4 min read
Mount AWS S3 Bucket On Amazon EC2-Instance
AWS S3 storage service is used for object storage these objects can be termed a file and it's metadata that briefs about it. Well, to store the files and documents we need to create a S3 bucket. It's like a container that stores the files. These files can be pictures, txt file, documents, static web
3 min read
How to Create AWS EC2 using Terraform?
AWS EC2 (Elastic Compute Cloud) is a web service provided by Amazon Web Services (AWS) that allows users to launch and oversee virtual servers, known as examples, in the cloud. It gives an adaptable and versatile foundation for running different sorts of applications and jobs. With Terraform EC2, cl
13 min read
Creating an EC2 Instance with AWS CLI: A Simple Tutorial
Amazon EC2 (Elastic Compute Cloud) is a core service in AWS that allows users to launch and manage virtual machines in the cloud. While the AWS Management Console is commonly used to create and manage EC2 instances, the AWS Command Line Interface (CLI) offers a powerful way to automate the process,
7 min read