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 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 requests 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 set password authentication to the ec2 instance

Nginx Setup on RHEL/Centos/Ubuntu

In this guide, we will look at the following.

Install Nginx on RHEL & Centos

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

sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

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

The 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,

http://[server-ip]
nginx home page

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 example-one.com. 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/example-one.com/public_html

Step 2: Create a test index.html file if you don’t have your own index file.

sudo vi /var/www/example-one.com/public_html/index.html

Copy the following contents and save the file.

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

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

For RHEL/Centos the user is nginx,

sudo chown -R nginx: /var/www/example-one.com

In most of the RHEL/Centos distributions, SElinux will be set to enforcing mode. Execute the following commands for SELinux to allow access to 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/example-one.com

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

For RHEL/Centos,

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

sudo vi /etc/nginx/conf.d/example-one.com.conf

For Ubuntu,

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

sudo vi /etc/nginx/sites-available/example-one.com.conf

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

Note: Replace example-one with your domain name.

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

    root /var/www/example-one.com/public_html;

    index index.html;

    server_name example-one.com www.example-one.com;

    access_log /var/log/nginx/example-one.com.access.log;
    error_log /var/log/nginx/example-one.com.error.log;

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

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

sudo ln -s /etc/nginx/sites-available/example-one.com.conf /etc/nginx/sites-enabled/example-one.com.conf

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 set up SSL for your Nginx websites. You can use the free SSL certificates from Letsencrpt for your SSL needs.

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 them 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 that comes under the load balancer. You can give any user-defined name for the group. Here am going to give the name “web_fleet”.

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

upstream web_fleet {
    server 54.136.14.10:80;
    server 64.156.14.11:80;
    server 94.176.14.12:80;
}

Step 3: Now you have to set the host 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;
    server_name example-one.com www.example-one.com;
    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 settings associated with the load-balancing configuration. You can check the official Nginx documentation for more clarification.

0 Shares:
2 comments
  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