Categories: DevOps

How to Install Nginx on an Amazon Linux 2 Instance

Introduction

This comprehensive guide will walk you through the process of installing Nginx on an Amazon Linux 2 instance step by step. Nginx is a robust web server and reverse proxy that may increase the performance and stability of your web applications dramatically. By following this instruction, you will be able to successfully install Nginx and guarantee that it runs well on your Amazon Linux 2 environment.

Prerequisites

Let’s make sure we have all of the prerequisites in place before we begin the installation process:

Amazon Linux 2 Instance: Ensure that an Amazon Linux 2 instance is operational in your AWS account.

SSH Access: You should be able to connect to your Amazon Linux 2 instance using SSH. This will enable us to connect to the server and run the required instructions.

Step 1: Update System Packages

Let’s start by making sure our system packages are up to date. Run the following commands in your terminal or SSH into your instance:

sudo yum update -y

This will update all the installed packages to their latest versions.

Step 2: Install Nginx

Now that our system has been upgraded, we can begin installing Nginx. Execute this command:

sudo amazon-linux-extras install nginx1.12

By using amazon-linux-extras, we can easily install the latest stable version of Nginx available for Amazon Linux 2.

Step 3: Start Nginx Service

We can now start the Nginx service and have it run automatically when the system boots. Execute these commands:

sudo systemctl start nginx
sudo systemctl enable nginx

Nginx should now be up and running on your Amazon Linux 2 instance. You can verify its status by executing:

sudo systemctl status nginx

Step 4: Configure Nginx

Nginx is installed with a simple configuration that is suitable for the majority of use cases. However, depending on your application requirements, you may need to make certain adaptations. The configuration files for Nginx can be found in the /etc/nginx/ directory.

To get started, let’s build a simple Nginx server block (virtual host) that will serve an example website. Using your favourite text editor, create a new configuration file as follows:

sudo nano /etc/nginx/conf.d/mywebsite.conf

Add the following configuration to the file:

server {
    listen 80;
    server_name mywebsite.com www.mywebsite.com;

    location / {
        root /var/www/html;
        index index.html;
    }
}

This basic configuration instructs Nginx to listen on port 80 for requests on mywebsite.com and www.mywebsite.com, to serve content from /var/www/html, and to use index.html as the default file.

Remember to replace mywebsite.com with your actual domain name and use FTP or SCP to upload your website files to /var/www/html

Step 5: Test Nginx Configuration

Before we implement the modifications, let’s check the Nginx configuration for syntax issues. Execute this command:

sudo nginx -t

If the test is successful, you’ll see a message confirming that the configuration is valid.

Step 6: Reload Nginx

We must reload Nginx to apply the configuration modifications. Carry out the following command:

sudo systemctl reload nginx

Now, Nginx is ready to serve your website based on the new configuration.

Related Articles

Comprehensive Guide: How To Backup And Restore MongoDB Database

NoSQL Vs SQL: Understanding The Differences And Choosing The Right Database For Your Needs

A Simple Solution For Displaying Cookie Consent Messages On Websites

MongoDB Interview Questions And Anwers

Understand The Background Of Free AI Tool Now

The Requested Url /Phpmyadmin/ Was Not Found On This Server

Conclusion

Congratulations! Nginx has been installed and configured successfully on your Amazon Linux 2 instance. Nginx is now ready to handle incoming web requests and efficiently serve your website content. Remember that for additional customization and optimisation, you can always turn to the official Nginx manual.

Developer Diary

Share
Published by
Developer Diary

Recent Posts

Git Tag Cheat Sheet

Introduction Git tags are an essential feature of version control systems, offering a simple way…

2 months ago

Understanding Web Storage: Cookies, Local Storage

Introduction The methods that browsers employ to store data on a user's device are referred…

2 months ago

Setting up OpenVPN Access Server in Amazon VPC – AWS

Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…

3 months ago

Enhance Error Tracking & Monitoring: Integrate Sentry with Node.js & Express.js

Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…

3 months ago

Comparing Callbacks, Promises, and Async/Await in JavaScript

Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…

5 months ago

How To Secure Nginx with Let’s Encrypt on Ubuntu EC2 Instance

Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…

6 months ago