How to Install Nginx on Amazon Linux 2 Instance

How to Install Nginx on Amazon Linux 2 Instance

How to Install Nginx on Amazon Linux 2 Instance

Dear Reader, I hope you are doing fantastic today. In my today’s post, I am here to help you install Nginx on an Amazon Linux 2 EC2 instance. After that, we will configure it as a web server and customize the web page to our own.

Does that sound exciting?

Alright !!!

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Let’s get to know a little bit about Amazon Linux 2 and NGINX.

Amazon Linux 2: 

  • Amazon Linux 2 is a widely used amazon provided free-tier eligible Linux AMI.
  • You can install software on Amazon Linux 2 using the yum package manager.
  • Usually, the stable versions of the software are found on the Amazon Linux core repository and can be installed using the yum install package-name command.
  • looking for newly released software? Look for it in amazon-Linux-extras repository.
  • When writing this post, Nginx is available in amazon-linux-extras repository and we will install it from there only on Amazon Linux 2.

About Nginx: 

Nginx is open-source software that works as-

  • Web Server
  • Reverse Proxy Server
  • Load Balancer
  • Mail Proxy Server
  • HTTP Cache etc.

Nginx is very popular and used widely even by some of the biggies like NetFlix, Dropbox, WordPress.com etc. Although it started as the fastest and most performant web server, it proved to be fit for multi purposes like mail server or load balancer etc.

A Bit of Background about Nginx on Amazon Linux 2

If you hit the command sudo yum install nginx in an Amazon Linux 2 system, you will get a response like below-

How to Install Nginx on Amazon Linux 2 Instance 1

It says nginx is available in amazon-linux-extras repository. Also, the topic name for the same is nginx1.

And the response in the above screenshot also says you can straightaway install nginx in your Amazon Linux 2 system by using the below command-

sudo amazon-linux-extras install nginx1

However, for the sake of the completeness of this tutorial, I will give step by step guide below on how you can find a package like nginx on Amazon Linux 2, install it on the system and customize it to view our own page.

Prerequisite

Steps to Install Nginx on Amazon Linux 2 Instance

Note: If you are looking for how to install Nginx on Amazon Linux 2023, check out this tutorial instead.

These are the steps to install Nginx on an Amazon Linux 2 EC2 instance. I have broken it into very very small steps to keep it very simple.

  1. Create an Amazon Linux 2 EC2 Instance
  2. SSH into Your EC2 Instance
  3. Run System Update
  4. Check If amazon-linux-extras is installed
  5. Look for the Nginx package in the amazon-linux-extras repo
  6. Enable the Topic if not enabled
  7. Install Nginx from amazon-linux-extras repo
  8. Verify the Nginx Installation
  9. Start Nginx Server
  10. Configure Nginx to restart on System Boot
  11. Allow Port 80 on the Security Group
  12. Test the Nginx Web Server
  13. Customize the Web Page

Step 1: Create an Amazon Linux 2 EC2 Instance

Before installing Nginx on your Amazon Linux 2 system, you will need one up and running. If you already have one, feel free to move to the next step.

If not, read my previous tutorial on How to Launch an EC2 Instance with Amazon Linux 2 AMI and create an Amazon Linux 2 EC2 instance.

Step 2: SSH into Your EC2 Instance

Once your instance is up and running, select your instance and click connect as shown below.

How to Install Nginx on Amazon Linux 2 Instance 2

Select the EC2 Instance Connect tab, make sure the user name is ec2-user and then click Connect.

How to Install Nginx on Amazon Linux 2 Instance 3

A new browser window opens and you get connected to your EC2 instance as seen below.

How to Install Nginx on Amazon Linux 2 Instance 4

Very good. Let’s proceed to install Nginx on our Amazon Linux 2 EC2 instance.

Step 3: Run System Update

Just after we connected to our instance, in the terminal we get a response like a package update available, run sudo yum update.

To be frank, before installing any software on your EC2 instance, it’s always recommended to run a system update so that all the packages/software get updated with the latest version.

Run the below command to update all the packages

sudo yum update

Once the update is finished, we can start with the next step.

Step 4: Check If amazon-linux-extras is installed

We are going to install nginx from amzon-linux-extras repo, so we’ll check if it is installed in our Amazon Linux 2 instance. Usually, Amazon Linux 2 system already comes installed with amazon-linux-extras and you can check it with the below command.

which amazon-linux-extras
How to Install Nginx on Amazon Linux 2 Instance 5

As soon as you hit enter, you get the path in which it’s installed. That means amazon-linux-extras is installed in your Linux 2.

Just in case it’s not installed, you can install it using the below command.

sudo yum install -y amazon-linux-extras

Step 5: Look for the Nginx package in the amazon-linux-extras repo

You can run the list command on the amazon-linux-extras repo to find the available Nginx packages.

amazon-linux-extras list | grep nginx

Once you run list command, it shows all the available versions of nginx in the repo as you can see below-

How to Install Nginx on Amazon Linux 2 Instance 6

And here I am says ‘nginx1

Amazing !!!

Step 6: Enable the Topic if not enabled

As you can see the nginx1 is already enabled and you can install it right away.

otherwise, you can enable the topic for example nginx1 by using the below command.

sudo amazon-linux-extras enable nginx1

Step 7: Install Nginx from amazon-linux-extras repo

Type

sudo amazon-linux-extras install nginx1

And hit enter.

How to Install Nginx on Amazon Linux 2 Instance 7

Type y when prompted for installation and hit enter.

And in a matter of seconds, nginx gets installed on the system.

Note: You’ll have to use sudo(root privilege ) to run the above command. The reason is that whenever you try to install, remove or change any software, you must have root privilege to do such tasks.

Step 8: Verify the Nginx Installation

nginx -v
How to Install Nginx on Amazon Linux 2 Instance 8

Congratulations !!!

You have successfully installed Nginx on your Amazon Linux 2 EC2 instance.

Step 9: Start Nginx Server

At the moment nginx is installed in our amazon linux 2 system however not started. Run the below command to start the nginx server.

sudo systemctl start nginx.service

Step 10: Configure Nginx to restart on System Boot

Since you are configuring it as your web server, you would like Nginx to be started automatically in case the instance reboots.

You can configure it by the below command.

sudo systemctl enable httpd.service

Step 11: Allow Port 80 on the Security Group

Nginx is installed and started in our Linux 2 instance. Therefore, ideally, we are really to test it by hitting the public IP of the instance. But hey, if you hit the public IP of your EC2 instance, your request will just time out like below.

Access Error

And the reason behind this is the security group of our EC2 instance doesn’t allow web/HTTP traffic hence the request failed.

Let’s configure it.

Click on your instance Id. In the details screen, click on the security tab and then click on security group Id.

Click Edit inbound rules

Then, click on Add rule and add an HTTP rule like the below-

How to Install Nginx on Amazon Linux 2 Instance 10

Click Save rules.

Once the rule is successfully added, you can hit the public IP again to see the webpage

Step 12: Test the Nginx Web Server

Just hit the public IP from your browser. Make sure that oath has HTTP and not HTTPS as we have not yet configured HTTPS for our website.

How to Install Nginx on Amazon Linux 2 Instance 11

Very nice, you can access your Nginx web server successfully.

Let’s customize it as per our own needs.

Step 13: Customize the Web Page

In the last screenshot, you saw the default web page served by nginx. Let’s customize it by creating our own index.html in the web server’s document root.

Nagivate to /etc/nginix

cd /etc/nginx

And check the content of the nginx configuration file nginx.conf

cat nginx.conf

You will notice, the document root is : /usr/share/nginx/html

How to Install Nginx on Amazon Linux 2 Instance 12

If you navigate inside the document root, you can replace the content of the index.html

Type sudo nano index.html and paste the content of the below block into it.

<!DOCTYPE html>
<html>
<body>
<h1>Hello World !!</h1>
<p>Welcome to CloudKatha from Nginix</p>
</body>
</html>

Save the file and exit.

Hit the public Ip again and Yaaayyyyyyyy

How to Install Nginx on Amazon Linux 2 Instance 13

Here comes a customized web page from Nginx.

Nginx provides quite a lot of options to configure file location and other settings for HTTP and HTTPS. You can read more about Nginx here and experiment with it.

Conclusion

In this how-to guide, you learnt How to Install Nginx on Amazon Linux 2 Instance.

We roughly did these things in this tutorial-

  • Learnt a bit about Amazon Linux 2 and Nginx.
  • Launched an Amazon Linux 2 EC2 instance.
  • Connected our EC2 instance using the EC2 Instance Connect option.
  • Installed Nginx
  • Started Nginix server and enabled start on reboot
  • Configured security group to allow HTTP traffic
  • Tested Nginix default Web page
  • Checked Nginx configuration in /etc/nginx
  • Configured the web page with our own

Were you able to install and configure Nginx on your Amazon Linux 2 using this tutorial? Let me know in the comment section.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate us-

Suggested Read:

2 thoughts on “How to Install Nginx on Amazon Linux 2 Instance

Leave a Reply

Your email address will not be published. Required fields are marked *