You are currently viewing Getting Started with NGINX on Amazon EC2 Linux 2: A Practical Guide

Getting Started with NGINX on Amazon EC2 Linux 2: A Practical Guide

Introduction

Amazon EC2 is a popular cloud computing service that allows users to create and run virtual machines (instances) on demand. NGINX is a powerful web server, reverse proxy, and load balancer that can handle high traffic and complex applications. In this article, we will show you how to install and configure NGINX on Amazon EC2 Linux 2, a modern operating system based on the Linux kernel.

Prerequisites

  • An AWS account with access to EC2
  • A running EC2 instance with Amazon Linux 2
  • SSH access to the instance

Step 1: Install NGINX on Amazon EC2 Linux 2

To install NGINX on Amazon Linux 2, we need to enable the EPEL (Extra Packages for Enterprise Linux) repository, which provides additional packages that are not available in the default repositories. To do this, run the following command:

This will install the EPEL repo and enable it for future use. Next, we can install NGINX from the EPEL repo using the yum package manager:

This will install the latest version of NGINX and its dependencies. To verify the installation, run:

This should output the NGINX version number, such as:

NGINX on Amazon EC2 Linux 2

Step 2: Configure NGINX on Amazon Linux 2

After installing NGINX, we need to configure it to serve our web content and handle requests. The main configuration file for NGINX is located at /etc/nginx/nginx.conf. This file contains global settings and directives for NGINX. We can edit this file using any text editor, such as nano:

The default configuration file looks something like this:

For more information on configuration, see:

* Official English Documentation: http://nginx.org/en/docs/

* Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log  /var/log/nginx/access.log  main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;

include             /etc/nginx/mime.types;
default_type        application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

We can modify this file according to our needs and preferences. For example, we can change the server_name directive to match our domain name or IP address, or we can change the root directive to point to a different directory where our web content is stored. We can also add or remove other directives to enable or disable features, such as SSL, caching, compression, etc.

One important thing to note is that NGINX uses a modular structure for its configuration files. This means that we can create separate files for different server blocks or locations and include them in the main configuration file using the include directive. This makes the configuration more manageable and organized. For example, the default configuration file includes all files in the/etc/nginx/conf.d directory, which can contain configuration files for different virtual hosts or applications. Similarly, the default server block includes all files in the /etc/nginx/default.d directory, which can contain configuration files for specific locations or directives.

To apply the changes to the configuration file, we need to reload or restart NGINX:

or

Step 3: Test NGINX on Amazon EC2 Linux 2

To test if NGINX is working properly, we can visit our EC2 instance’s public IP address or domain name in a web browser. We should see the default NGINX welcome page, which looks something like this:

This means that NGINX is successfully installed and configured on our EC2 instance. We can now replace the default web content with our own, or deploy our web applications using NGINX as a web server or reverse proxy.

Conclusion

In this article, we learned how to install and configure NGINX on Amazon EC2 Linux 2. We also learned how to use the EPEL repo to install additional packages, how to edit the NGINX configuration file and how to use the modular structure to organize the configuration files. We also learned how to test NGINX by visiting our EC2 instance’s public IP address or domain name in a web browser. NGINX is a versatile and powerful web server that can handle various web applications and scenarios.

By using NGINX on Amazon EC2, we can leverage the benefits of both cloud computing and web hosting. We hope this article was helpful and informative for you. If you have any questions or feedback, please feel free to leave a comment below. Thank you for reading!

This Post Has 44 Comments

Leave a Reply