How To Install and Configure Nginx on Amazon ec2 RHEL and Ubuntu Instances

Install and Configure Nginx on Amazon ec2 RHEL and Ubuntu Instances

Nginx is a web server like apache. Not only as a web server, but it can also act as a load balancer, reverse proxy, etc. Performance-wise, Nginx is considered to be better than apache. Processing request in Nginx is even based as opposed to the spanning new thread model in apache.

In this tutorial, I will explain how to install and configure Nginx on ec2 RHEL and ubuntu instances.

The process for installing & configuring Nginx on RHEL , Centos and Amazon Linux is the same.

Instance Setup

  1. Launch an RHEL/Centos/Ubuntu instance using the management console. While launching the instance , configure the security group to allow traffic from HTTP 80 port & HTTPS 443.
  2. Connect the instance using putty.
  3. You can also setup password authentication to the ec2 instance

Nginx Setup on RHEL/Centos/Ubuntu

In this guide, we will look in the following.

Install Nginx on RHEL & Centos

Step 1: In RHEL you cannot download and install Nginx directly. You have to setup the epel ( extra packages for enterprise Linux) repo to install Nginx. Install EPEL package repository.

sudo yum install -y

For Centos, execute the following.

sudo yum install -y epel-release

Step 2: Update the package list.

sudo yum update -y

Step 3: Install Nginx

sudo yum install nginx -y

Install Nginx on Ubuntu

Execute the following steps to install Nginx on Ubuntu servers.

sudo apt update
sudo apt install nginx -y

Start and Enable Nginx

Following steps are common for RHEL and Ubuntu systems.

Step 1: Check the version to make sure Nginx is installed.

sudo nginx -v

Step 2: Start and enable Nginx.

sudo systemctl start nginx
sudo systemctl enable nginx

Step 3: Check the status of Nginx to make sure it is running as expected.

sudo systemctl status nginx

Step 4: Visit the nginx page using the Server IP. You should be seeing

For example,


Setting up Multiple Domains Using Nginx Server Blocks

When you have one or more websites to be hosted on the same Nginx server, you need to use the Virtual Hosts configuration.

In this section I will show you how to create a virtual host configuration for a website.

Step 1: Create the website folder and a public folder to put the static assets inside /var/www

Here am going to give the name as Name the folder with your website name. It will be easy to identify if you have many websites running on the same server.

sudo mkdir /var/www/

Step 2: Create a test index.html file if you dont have your own index file.

sudo vi /var/www/

Copy the following contents and save the file.

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Welcome to</title>
    <h1>Welcome To home page!</h1>

Step 3: Change the ownership of the root document to the user which is managing the worker process.

For RHEL/Centos the user is nginx,

sudo chown -R nginx: /var/www/

In most of the RHEL/Centos distributions, SElinux will be set to in enforcing mode. Execute the following commands for SELinux to allow accessing the nginx website files.

sudo setsebool -P httpd_can_network_connect on 
chcon -Rt httpd_sys_content_t /var/www/

For Ubuntu the user is www-data,

sudo chown -R www-data: /var/www/

Step 4: Create a Nginx configuration file with the websites name

For RHEL/Centos,

Create the config gile under /etc/nginx/conf.d/ folder.

sudo vi /etc/nginx/conf.d/

For Ubuntu,

Create the config file under /etc/nginx/sites-available/ folder.

sudo vi /etc/nginx/sites-available/

Copy the following server block configuration the conf file and save it.

Note: Replace example-one with your domain name.

server {
    listen 80;
    listen [::]:80;

    root /var/www/;

    index index.html;


    access_log /var/log/nginx/;
    error_log /var/log/nginx/;

    location / {
        try_files $uri $uri/ =404;

Only on Ubuntu server, execute the following command to create a symbolic link to sites-enabled folder.

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Step 5: Validate the server block configuration using the following command.

sudo nginx -t

If should get the following success message.

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 6: Restart the nginx server.

sudo systemctl restart nginx

If you use the public DNS of the ec2 instance , the server will throw the following error when you restart the Nginx server.

nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 64

Since the DNS name is pretty long , you have to add the server name bucket size to 128 to the example-one.conf file.

server_names_hash_bucket_size 128;

Now you will be able to access the website using your domain name. If you have used the test domain names, add it to your systems /etc/hosts file and try accessing the website.

To add more domains to the nginx server, follow the same steps with a different domain name and config names as explained.

Nginx SSL Setup

Optionally you can setup SSL for your Nginx websites. You can use the free SSL certificates from Letsencrpt for your SSL need.

I have written a tutorial for setting up SSL using Letsencryp. Read the tutorial here -> Nginx Letsencrypt Setup Guide Using Certbot

Load balancing using Nginx

You can use Nginx as a load balancer to balance the load between server fleets. Nginx proxies the incoming requests and sends it to the backend servers. To configure Nginx as a load balancer, you have to add two blocks of code to the nginx configuration file.

Step 1: Open nginx.conf file

sudo vi  /etc/nginx/nginx.conf

Step 2: Add the upstream group under the HTTP section. The upstream group is the group of servers which comes under the load balancer. You can give any user defined name for the group. Here am going to give the name as “web_fleet”.

Note: this configuration should be present in the HTTP section of nginx.conf file.

upstream web_fleet {

Step 3: Now you have to set the vhost configuration to receive traffic from a particular domain name and route it to the upstream servers. Add the following lines followed by the upstream block and save the file.

server {
    listen 80;
    location / {
        proxy_pass http://web_fleet;

Step 4: Restart the Nginx server

sudo systemctl restart nginx

Step 5: Now, if you access your nginx server using the DNS, the request will be routed to the backend server fleet present in the upstream block.

Make sure some service is running on the backend servers you mention in the upstream block.

There are many other parameters and setting associated with the load balancing configuration. You can check the official Nginx documentation for more clarification.

  1. i made some change in theme function file , when i save the file . my website go on error mode of 500. now how can i remove the code i do not have cpanel , this was install on digital ocean using putty NGNIX.

Leave a Reply

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

You May Also Like