Deploying a static website with AWS EC2 using Nginx
Tags: aws, nginx, css, ec2
Published: 2024-07-01 13:26:55

[Amazon Web Services (AWS)](https://aws.amazon.com/) is one of the most popular cloud computing platforms worldwide. It offers a comprehensive suite of services that enable developers and businesses to build, deploy, and scale applications with ease. 

One of the key advantages of AWS is its flexibility, allowing users to choose from a variety of services to suit their specific needs. This guide will focus on the AWS EC2 service, teaching you how to leverage its virtual machine capabilities to create your own server environment specifically designed to host your static website using the efficient and lightweight Nginx web server.

## Prerequisites

To get started with this tutorial, you must have the following:

- **An AWS account**: If you don't have one already, you can create a free tier account [AWS website](https://aws.amazon.com/).
- Basic understanding of HTML and CSS

## Creating the Static website

To start this project, you need to create the static website you want to serve with your NGINX server. This tutorial uses a simple HTML and CSS webpage.

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Information</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            padding: 0;
            text-align: center;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            border: 1px solid #ccc;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Hello there, its great that you are checking out my article!</h1>    
    </div>
</body>
</html>
```

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXfntwJAUEoVN99FZK6qIvZXAXonAMFhrHL_qje7kyoBOZ39WwqzY-sS8aQ94js9XDNQwH38WyP_MGYSL1Vp6BoN3Q77WUu3-64EqinEHthZ2RBxVm2By5wAyejQXc0Q1I_4V6JIeWA0GfZlKgEKCcZnt_yU?key=ns8a3bbSBgFGW8-8bXzdug)

## Creating an EC2 instance in your AWS console 

An [Amazon Web Service EC2 instance](https://aws.amazon.com/ec2/) is one of the most popular AWS services worldwide.  It is a virtual server in the AWS Cloud that provides the computing resources your applications and services need to run, such as CPU, memory, storage, and networking.

To create an EC2 instance, follow these steps:

1. Log in to your [AWS console](https://aws.amazon.com/).
2. Next, click the search icon at the top, type in EC2, and select it from the menu. This action redirects you to the **Resources** page. 
![](https://lh7-us.googleusercontent.com/docsz/AD_4nXdIS8nHuT4JKjd8AgX4S3I20Wd1G6IcT-WHucso08HRWDaLh-msIhFkTIBePgGppmhSGhZ5gdP9B33y26A5qVd6ljgVtgRAaMNNS9lp4okr3EXCR6OHD0-Ni-8Qyxycr_0NAQ4dkr1x9TDQFpQnwScIaFs?key=ns8a3bbSBgFGW8-8bXzdug)

3. In the Resources page, click on **Instances (running)** to get redirected to the Instances page on your AWS console. On the **Instances** page, click the **Launch instance** button to define configuration settings for your EC2 instance.


4. On the **Instances** page, click the **Launch Instance** button and configure your instance as follows:
    - **Name and Tags:** Give your EC2 instance a recognizable name.
    - **Amazon Machine Image (AMI):** An AMI is a template used to create virtual servers in Amazon EC2. It contains the operating system, software packages, and configurations needed for launching instances.

This tutorial will use the default Ubuntu AMI.

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXfy4aJa1S9XwPEawicHb8TituCAt3oPo_S7MBEwIN8sNw37K6scc-TlmjPc3W46eDEN7esuaX9vvYH-ViTfeWqM7MQy33IfYLrsYrqonCCnbHWwk-kzoJzoP0eDub1NnK8RxyovaU8Ff4LbIru-ojKYoqby?key=ns8a3bbSBgFGW8-8bXzdug)

- **Instance Type:** Select an instance type. This tutorial will use the default t2.micro instance type, which offers 1 vCPU and 1 GiB memory. If you prefer a different instance type with greater system capabilities, simply click the dropdown and choose from the various available options.
![](https://lh7-us.googleusercontent.com/docsz/AD_4nXeTGujte1pljMbmewumiIR4i45vjCSws5Sy3sRMqat-buQD4mbZFTGhvY_HXOoiWTin8Kr-W5avrqNDCRmrSbhzyz3cvqO57oMDDb9y2S9casxziFMPXz97vIPrtY9Be5sPCBXAuLZoyxwSiix7buJL45yc?key=ns8a3bbSBgFGW8-8bXzdug)

- **Key Pair (Login):** Key pairs provide a secure method for connecting to your instance. To create a new key pair, click the **Create new key pair** link, enter a name for the key pair, and click **Create key pair** once more to download your newly created key pair.
![](https://lh7-us.googleusercontent.com/docsz/AD_4nXfjCFpVd2g5mYLnboeeOU4XFnXK8HLBxkD-1iSq16KGaDqZsbPHtSj-hT3N_nuAghpTtqUlakcx1EXA9nxCgFxKEIWnwnvE_aBB1bcShp9h5Yr18zYmvAA5am_pLAwrn-hGMFwELvO3thSJ_LM0hazVUzE?key=ns8a3bbSBgFGW8-8bXzdug)

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXdMhtEixG6JdQYw4yDZius4MdVlNvX__vF-XgtxkW6Vso7rvb7KQe-SXulyz9D6ZzR5q1eUlTG-0goy5aC8hniUZbYpRnA2J6vWiCowljut9b7bFTS8ZTt76XC1WTSOC982id9O3xeTQcPZUri8AT8r26nL?key=ns8a3bbSBgFGW8-8bXzdug)

- **Network Settings:** Network settings define the firewall rules that limit or allow website traffic to your instance. In this section, check the boxes for **Allow SSH traffic from Anywhere**, **Allow HTTPS traffic from the Internet**, and **Allow HTTP traffic from the Internet.**
![](https://lh7-us.googleusercontent.com/docsz/AD_4nXdmCSnlA1lP0RRHGwqaiGQXLyuf3JAwgprmTSGfacnaPdZ3gqlU7pE0NBmuvvG7hgYJ01AFCVhAZL-pN-kmgIxzrB6t64IDkQROxFdrFhWq38vrcEWpWw21LZEu-GYf4b46Q2eczP70icJWDxxznn2ZpCQ9?key=ns8a3bbSBgFGW8-8bXzdug)

 5.**Launch your Instance:** Once you've reviewed your configuration, click the **Launch Instances** button to create your virtual machine on AWS.

**Logging into your AWS virtual machine**
After creating your AWS EC2 instance, click on the instance ID to navigate to the Instances page.

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXfYZKm-HVHSSRaPwpfTxuRzngVOrL9g2eMrg0Sz9tcKPXfzHQYW3k8c1LEmknxE2TkqDwruwhqFwT1nwwnO8BtSe6LpMaicMfMi6SpwX9D6li1eZp0Y9as6kYxhbejyS8Nql9VzS_102Wn00jsO3azc1BrO?key=ns8a3bbSBgFGW8-8bXzdug)


On this page, select your instance by checking the checkbox next to it, then click the **Connect** button at the top to initiate the connection.

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXfPqwpTRYueFZ0P28u40ENa7ACITUUUXwMWlY9R23TgM14VnxeTL-6LQ1V8UzKB0YULzjFOYA7LMbOiW0u6hjzfsMGaI6lN3vZAAJO6P1eOdGL-HIQfjp2_VWLhQ3cv4EsuQL_vLcAhxgMAYPbHYw9aCdkJ?key=ns8a3bbSBgFGW8-8bXzdug)


This action will take you to the **Connect to instance** page, where you should click the **Connect** button to establish a connection to your EC2 instance. 

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXc5KxK0BvbP5_-arRRjOEP21ccJ7JBOFY3E_6KDuNdAx0MjNigFwJ3P7gGl1WAkbvIPRvkjOkyYsnO7F7zCavg-W31WmqsDniFglmn4VLANVL40xEQbtvrrWnZP7bVxMAtMgV4KlSIeZ0m-X26amJzr3Pc?key=ns8a3bbSBgFGW8-8bXzdug)


Once connected, the EC2 instance will open, and you will see the Ubuntu terminal.

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXeLx_lMB_OqokAsV30RYo3z_7YiLp1vvuwsg0K0duEF3DAur4D8koEPtcPV8F7QqBI7AuDX5vL-Dnma_HFt787biCwRGq5JX7P-CE-slkUwJvSWgUUEqpGx4__qqssi_enbHJLkfNpKTueJQpbTRAhU4NYX?key=ns8a3bbSBgFGW8-8bXzdug)


Now that your EC2 instance is all setup, the next step is to configure the NGINX web server to handle web requests and serve your static website.

## Installing the Nginx server

[Nginx](https://nginx.org/en/) is an open-source web server designed to handle HTTP requests. It processes requests from web browsers and delivers the corresponding web content.
Nginx excels at efficiently delivering static content; it can handle many connections at once, making it suitable for high-traffic websites. Additionally, Nginx is great as a reverse proxy, forwarding requests to other servers or services running on your EC2 instance. To learn more about Nginx, please check out the official Nginx documentation.
 
To create an Nginx server in your EC2 instance. Run the following commands in your virtual machine Ubuntu terminal:

1.Run this command to switch to the root user and gain the elevated privileges needed to download Nginx:

```
sudo -i
```

2.After logging in as the root user, use these commands to update the package index and install NGINX in your system:

```
apt-get update
apt-get install nginx
```

3.Next, check the status of the Nginx with this command:

```
service nginx status
```
Running the service nginx status command provides information about the status of the NGINX service, including whether it is running or stopped. If the NGINX server is running correctly, you will see the corresponding status in your terminal.

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXesbbj2KIVCpeMIbQX48Fu4lBxuIaGMKQW35KrEYH74vvC906KWeDq0T9hY7zW_MAihT-Ha8rnLoAHYvIS2ACdA6A1tj9LuBRJYaBMf-2G5uHBWljLuizb2zmmkT_E9sCftpqisUQ2-pa5YL7sLDXpcta79?key=ns8a3bbSBgFGW8-8bXzdug)

4.To view the default webpage hosted by the NGINX server, copy the Public IP address of your EC2 instance and paste it into your web browser. 
![](https://lh7-us.googleusercontent.com/docsz/AD_4nXe8Qn7kSd07ZlVuhvo4Q6Je4jmsUPtaa31aTraw8TO-uFD63KMgxd7dCa3TXe2B7stneEn7e19AT5FtTLvrQocna3wjMXwrtl31IsLX0HGHVjtjeUyOsmoLJeojUgoyyoI-zFJch-DV4K9TIVbE0Ui0lHAn?key=ns8a3bbSBgFGW8-8bXzdug)


You should see a static website like this:

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXeA-xp2ncTAwGKudcSORYTbbIvql-nUPf6NyNaHGEgnXm24bAIRYn_1JD_JhyBlkj6FIoUFxpe5AWYuzFEhcW-TorW7O0QBsv3SHm-SHiXRMXC-SwWE-y5LD1CEetCOXqVlZMg3M3SrfcDBuRrKtE2MrTX9?key=ns8a3bbSBgFGW8-8bXzdug)

## Serving the static website 

To serve your static website, replace the HTML code on the index page of the NGINX server with your own website’s HTML code.
To find the index webpage in the NGINX server, navigate to the `/var/www/html` directory and list the files located there using this command:

```
cd  /var/www/html/
ls
```

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXfue6OTBeCm2VL5A4yCrKZxiS9QXkE8XQw9LQ-06ggH_Xil4uEy15_RcYJpLVE7RR2nBcGw0XSP2AoO0miU3GetugZ3RIrZ5Js_X8Vxi1QPYRKXz0TTYKMgltpAtUWd4hkfIDsQjiLMfQxnM_cfMFVmEnoA?key=ns8a3bbSBgFGW8-8bXzdug)


In this directory, you will see the `index.nginx-debian.html` file, which is the index page of the NGINX server.  To view the content of this file, run the following command:

```
cat index.nginx-debian.html
```

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXe1DMkQTzae0fmaMrvrvCuOAiCmOmLMMPfwW2Gq1XL095_l4gr_v0IUweL_eyRfLjdMjNdSxch6v1GtJT6xSIXgbWSLaQjCXfcqGVLcnMks9WF_8F13zSRHth4Ec_NAWCxmkRuK7NOGktBpDfyfG83NPnsz?key=ns8a3bbSBgFGW8-8bXzdug)


The next step is to replace the HTML code in the `index.nginx-debian.html` file with the HTML code of your static website.

To do this, open the `index.nginx-debian.html` file in an editor of your choice. This guide will use the nano editor:

```
nano index.nginx-debian.html
```
Next, use the arrow keys to navigate and edit the file. When you are done with your edits, press Ctrl + O (the letter 'O', not zero) to save the file. Nano will then prompt you for the file name, index.nginx-debian.html. Press Enter to confirm and save the file.

To exit Nano, press Ctrl + X.

Refresh your Public IP page in your web browser to see your static website.
Congratulations, you have successfully deployed your static website on an AWS EC2 instance using NGINX.

![](https://lh7-us.googleusercontent.com/docsz/AD_4nXetL5fCi3oaDLeiSEs8Q0BQZyL9HD2i6m2r3pBFRC-hlBZI5ZbLUGkkaydCSkeqFiKcdiuukWRFG5vOJxdRs3QrI2VwxZOaearimzsEUpU2HcyhWtobRyh1dO9var5ET29jXNxChTBeX3-H-mg33-8ZQkY?key=ns8a3bbSBgFGW8-8bXzdug)



## In summary

This article introduces AWS EC2 instances, one of Amazon Web Services' most popular offerings. It guides you through creating and configuring your own EC2 instance, connecting to it, and setting up NGINX to serve your static website. This hands-on approach equips you with the fundamentals of web hosting and cloud infrastructure management.

However, this is just the beginning. AWS offers a wide range of services that are worth exploring. As you follow my journey, we will cover some of these services. Additionally, you can dive deeper into NGINX for more complex configuration options and learn about security best practices for managing EC2 instances.

Happy coding!