How to Install Nginx on Amazon Linux 2023 Instance
Dear Reader, I hope you are doing well. In one of my previous posts, I explained how to install Nginx on amazon Linux 2 instance. In today’s post, I will talk about how you can install Nginx on your Amazon EC2 instance running on Amazon Linux 2023 or AL2023 AMI.
Amazon Linux 2023 is the next generation of Amazon Linux from Amazon Web Services (AWS). It provides a stable, and high-performance execution environment to develop and run cloud applications. With Amazon Linux 2023 (AL2023), you get an application environment that offers long term support with access to the latest innovations in Linux.
Source: AWS
The basic instruction stays the same in both Amazon Linux 2 and Linux 2023, just that in Amazon Linux 2, you install it from amazon-linux-extras and in Al2023 you do it from the core package repository.
Additionally, we use dnf instead of yum command for software installation in AL2023 or Amazon Linux 2023. Although both yum and dnf works, it is preferable and recommended to use dnf. Here is why if you want to dig deeper into them.
Anyway, let’s get started.
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.
Before installation of Nginx on our Amazon Linux 2023 instance, let’s get to go about Nginx a bit.
An Overview of Nginx
Nginx is an open-source web server that can also be used 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.
Prerequisite:
You need to have an active AWS account running Amazon Linux 2023 instance. Here are two tutorials that can help you meet this prerequisite if not already met.
Steps to Install Nginx on Amazon Linux 2023 Instance
- Connect to AL2023 Instance
- Search for Nginx Package
- Install Nginx on Al2023
- Verify the Nginx Installation
- Modify the Security Group of the Instance
- Access Nginx Default Page
- Customize the Web Page
- Access the Customized Web Page
Step 1: Connect to AL2023 Instance
We have an up-and-running Amazon Linux 2023 or AL2023 EC2 instance. Let’s connect to it to be able to run commands so that we can install Nginx on it.
You can SSH into the instance from your local system using the below command –
ssh -i /path/my-key-pair.pem ec2-user@instance-public-ip
I mostly prefer the EC2 instance connect option to connect from the browser itself.
Select your EC2 instance and click on Connect as shown below-
Verify that ec2-user is shown in the user name field and click on Connect
Within a matter of seconds, you are connected to your instance as seen below.
Step 2: Search for Nginx Package
Time to search for the Nginx package in Amazon Linux 2023. You can also see the official Amazon Linux 2023 Packages page for all the available packages in Amazon Linux 2023 repository.
Or you can search in the terminal like below-
sudo dnf list | grep nginx
All the matching packages are shown as you can see in the above screenshot. nginx.x86_64 is what we are interested in. Let’s proceed with the installation.
Step 3: Install Nginx on Al2023
We will roughly use the below set of commands for the installation of nginx till making sure it’s available to serve a web request.
sudo dnf update //To Install Latest Update
sudo dnf install -y nginx // Install Nginx
sudo systemctl start nginx.service //Start Nginx Server
sudo systemctl status nginx.service // Check Server Status
sudo systemctl enable nginx.service // Enable Auto Server Start on Reboot
Command Explanation and Step by Step Execution:
1. sudo yum update
Update your Linux 2023 system with the latest software packages. It is almost always recommended to update your system with the latest packages.
Run the below command for the same.
sudo yum update
The above screenshot says that the system is already up to date. Nothing to do.
2. Install Nginx using sudo yum install -y nginx
Run the below commands to install Nginx on your Linux 2023 instance.
sudo yum install -y nginx
Nginx and its dependencies are installed as you can see in the above screenshot. And you can verify the nginx version using nginx -v. It’s quite the latest version at the time of writing this.
3. Start Nginx Server
We have installed the Nginx server however it is in the stopped state. Let’s start it so that it can serve web requests.
sudo systemctl start nginx.service
4. Enable Nginx Server Start on Reboot
Since we are using Nginx as a web server, we don’t want to manually start the server every time it reboots. Using the below command will start the nginx server automatically on system reboots.
sudo systemctl enable nginx.service
Step 4: Verify the Nginx Installation
You can check the installed nginx version using nginx -v command.
nginx -v
You can also check the server running status using the below command-
sudo systemctl status nginx.service
As you type the command and hit enter, you see the below message-
And it shows that it’s in running status.
Congratulations !!! You have successfully installed nginx on your amazon linux 2023 or AL2023 EC2 instance. Additionally, you started the server and enabled it to start automatically on reboot.
Ideally, our Nginx server is ready to serve requests. But if you access it right now by hitting the public IP, you will see an error like below.
This site can’t be reached
54.206.60.162 took too long to respond.
And the ohh so obvious reason is that our EC2 instance security group doesn’t allow web traffic at the moment. Let’s do that in the next step.
Step 5: Modify the Security Group of the Instance
In the EC2 details screen, go to the Security tab and click on the link for the security group.
Click on Edit Inbound Rule
Click Add rule
Add an HTTP rule as shown below and click Save rules.
Step 6: Access Nginx Default Page
Once the inbound rule is saved, try hitting the public IP of the instance again.
And Yaaaayyyyy the installation is successful and we are able to access the default Nginx web server. However, this is not enough, and you need to customize it to serve your own web page. Right?
Let’s do that in the next step.
Step 7: Customize the Web Page
Nginx or any web server serves pages from a location called the document root.
Let’s check the nginx configuration to find out the document web root. You can find the configuration file in the /etc/nginx folder.
cd /etc/nginx
Do an ls and see what is present there.
Check the content of nginx.conf to find out the current configuration.
cat nginx.conf
Scroll down to see the server block that listens on port 80. It looks like this.
As you can see the root or place from which Nginx serves pages on web requests is /usr/share/nginx/html. If you go inside this folder, you will see all the default pages created by Nginx.
We can customize it by adding our own pages. The starting point of which is index.html. Let’s edit this to add our own content.
Type sudo nano index.html and paste the content from the below snippet or any HTML of your choice.
<!DOCTYPE html>
<html>
<body>
<h1>Hello World !!</h1>
<p>Welcome to CloudKatha from Nginx on Amazon Linux 2023</p>
</body>
</html>
Press CTRL + X to Save the file and exit.
Step 8: Access the Customized Web Page
We have customized the webpage as per our needs. Let’s access it now to see how it looks.
If you hit the public IP again or just refresh the page, you will see-
Conclusion
In this how-to guide, you learnt How to Install Nginx on Amazon Linux 2023 or AL2023 EC2 Instance.
We roughly did these things in this tutorial-
- Connected on an up-and-running Amazon Linux 2023 EC2 instance using the EC2 Instance Connect option.
- We searched Nginx Package and Installed it.
- 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
- Accessed the customized web page
Were you able to install and configure Nginx on your Amazon Linux 2023 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-
One thought on “How to Install Nginx on Amazon Linux 2023 Instance”