DEV Community

Stephanie Albert
Stephanie Albert

Posted on

DEPLOYING APACHE WEB SERVER TO AWS EC2

INTRODUCTION

Imagine you are working as a system administrator for a small e-commerce company. The company has a website hosted on shared hosting, but due to increased traffic and performance issues, your team has decided to migrate the website to a more scalable infrastructure using AWS.

Your task is to set up a basic EC2 instance that will serve as the new web server for the company’s website. You’ll create and configure the instance, install a web server (Apache), and deploy a simple static HTML page that announces the migration.

Overview

An Amazon EC2 instance is a cost-effective virtual server in the AWS cloud environment. You have full control over your instance, from the time that you first launch an instance until you terminate the instance. You can choose from a variety of operating systems when you launch your instance. Amazon EC2 provides a wide range of instance types. You can customize your instance to meet your needs by choosing a type that offers the compute resources, memory, storage, and network performance required to run your applications, paying only for what you use.

PREQUISITE :

  • AWS Account
  • Key Pair
  • Security Group
  • Web Server(Apache)

Step 1:

Login into to your AWS management console, click on the search bar icon and search for EC2.

Image description
Click on Launch instance

Image description

Step 2:

Launching your instance

Enter the name of your web server and choose the Amazon Machine Image (AMI) of your choice.

Image description
If you don't have a key pair, you can simple create one. A key pair is a security credential that authenticates your access to the instance, usually via SSH. A file will be downloaded to your computer once you hit create key pair. Save that file in a secure folder that you could easily access.

Image description
customize your security group and launch your instance.

Image description
You will receive a confirmation once it is successfully launched.

Image description

Step 3:

Connecting to your instance.

They are various ways to connect to your instance, such as Secure Shell (SSH) for macOS or PuTTy on windows, Remote Desktop Protocol(RDP) for Windows instance, EC2 instance connect via the AWS Console, AWS Systems Manager, among others. In this tutorial we will be using EC2 Instance Connect.

Note: You don't need a key pair when using EC2 instance connect.

Image description

Step 4:

Installing a Web Server and deploying a simple HTML page.

Once you have successfully connected to your instance, create a file that contains commands for installing the web server, copying and setting permissions for the .html file.

sudo su

nano <filename.sh>

Enter fullscreen mode Exit fullscreen mode
sudo apt update && upgrade -y

sudo apt -y install apache2

sudo systemctl start apache2

sudo systemctl enable apache2

echo "<html><body><h1>Yay, you did it!</h1></body></html>" > <file_path>/index.html

sudo cp <file_path>/index.html /var/www/html/index.html

sudo chown -R www-data:www-data /var/www/html

sudo chmod -R 775 /var/www/html
Enter fullscreen mode Exit fullscreen mode

Make sure the file is executable then run your script and replace with your actual path.

ls -l

chmod +x <filename.sh>

./<filename.sh>

Enter fullscreen mode Exit fullscreen mode

Step 4:

Testing!

Once you have run your script and received no error messages, navigate back to the AWS Console, copy the IP address of the instance, and open it in your browser. It should display the page below:

Image description
Congratulations on launching your first EC2 instance!

Conclusion:

By following these steps, you have successfully launched an EC2 instance, connected to it, installed a web server, and deployed a simple web page. This process provides the foundation for hosting websites or applications on AWS, offering scalability and flexibility for your projects.

Top comments (0)