Install and Configure Nginx on Amazon ec2 RHEL and Ubuntu Instances

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

Nginx is a web server like apache. Not only as web server, it can 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 spawning 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.

For Centos, execute the following.

Step 2: Update the package list.

Step 3: Install Nginx

Install Nginx on Ubuntu

Execute the following steps to install Nginx on Ubuntu servers.

Start and Enable Nginx

Following steps are common for RHEL and Ubuntu systems.

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

Step 2: Start and enable Nginx.

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

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 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.

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

Copy the following contents and save the file.

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,

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.

For Ubuntu the user is www-data,

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

For RHEL/Centos,

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

For Ubuntu,

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

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

Note: Replace example-one with your domain name.

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

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

If should get the following success message.

Step 6: Restart the nginx server.

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

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

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

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.

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.

Step 4: Restart the Nginx server

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.

Related

ec2 file upload and download

Uploading and downloading files in AWS instance can be done using Filezilla client. If you are a windows user, you can…

Read more
apache mesos on amazon ec2

Mesos is a cluster manager for various applications like Hadoop, spark, docker etc..You can manage clusters for any…

Read more
domain name to ec2 server mapping

If you bought domain names from any provider like Godaddy, Namecheap etc, other than AWS Route53, you might have their…

Read more
About

Cloud Computing Tutorial Blog