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

Nginx is a web server like apache. 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 Nginx on RHEL , Centos and Amazon Linux is the same.

Instance Setup

1. Launch an RHEL instance using the management console. while launching the instance , configure the security group to allow traffic from HTTP 80 port.

2. Connect the instance using putty.

If you have apache server installed and running already, you have to stop the apache service. If you don’t , it will create conflicts.

Nginx Setup On RHEL

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 .

2. Download the Epel repo rpm for RHEL 6 64 bit


3. Install the rpm using the following command

rpm –ivh  epel-release-6-8.noarch.rpm

4. Check if the repository is enabled

yum repolist

5. Install Nginx using the following command.

yum install Nginx

6. Start the Nginx server

service start Nginx    or
/etc/init.d/nginx start

7. By default, RHEL allows traffic only to port 22. Even if you have port 80 opened in security groups you won’t be able to access the web server. So you have to accept port 80 connection in the server IP tables.

8. Open IP tables using the following command

vi /etc/sysconfig/iptables

9. Add the following line to the file

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

10. save the file and restart the iptables

service iptables restart

10. Use the public IP or the DNS in your browser. You will see a Nginx welcome message.

Setting up Virtual Hosts: (server blocks)

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 The folder name can be any user defined name.

mkdir –p /var/www/

2. Create an index.html inside the public folder and put any basic HTML page.

<h1> this is a test for RHEL virtual host setup</h1>

3. Change the owner for your website files to have read-write permissions.

chown  ec2-user:ec2-user  /var/www/

4. Allow read access to /var/www for everyone to access the website publicly.

chmod 755 /var/www

5. To add a virtual host, edit the virtual.conf file using the following command

vi /etc/nginx/conf.d/virtual.conf

6. Make the configuration changes like the following and save the file.

server {
    listen       80;
  # listen       somename:8080;
    location / {
        root  /var/www/;
        index  index.html index.htm;

Above, for server name I have put the public DNS of my amazon machine. Since I don’t have a custom domain , I am testing the virtual host setting using the public DNS.

7. Restart the Nginx server

service nginx restart

8. If you use the public DNS , 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

9. Since the DNS name is pretty long , you have to add the server name bucket size to 128 to the Nginx.conf file.
10. Open the nginx.conf file

vi /etc/nginx/nginx.conf

11. Add the following to the HTTP part of the file.

server_names_hash_bucket_size 128;

12. If you restart the Nginx server, you won’t be getting the bucket size error.

13. Check the virtual host setting by visiting the public DNS or the IP. You will see the demo HTML page you put in the /var/www/ folder.

14. If you want to add more websites, you just have to add one more virtual host configuration like we did in the virtual.conf file.

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

1. Open nginx.conf file

vi  /etc/nginx/nginx.conf

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 {

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.

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

Here the server name I have given is the public DNS of the Nginx EC2 server. Here is where you provide your custom domain. For testing purpose, you can use the public DNS of your EC2 server.

4. Save the nginx.conf file and restart the Nginx server

Service nginx restart

5. Now, if you access your nginx server using the public DNS, the request will be routed to the backend server fleet present in the upstream block. You can test this by installing apache server in any server and give the servers IP address inside 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.

Ubuntu Setup

1. launch an ubuntu instance with port 80 open in the security group.

2. Connect the instance using putty

3. Issue the following command to become the root user.

sudo su 

4. Install Nginx from the apt repository

apt-get install nginx

5. After installation starts the Nginx service using any one of the following commands.

/etc/init.d/nginx start   or
service nginx start 

6. Copy and paste the public IP or the DNS name of your Ubuntu server and hit enter. You will see a welcome message from the Nginx server.

Setting up server blocks (virtual hosts):

If you want to host more than one website on your server ,you should set up virtual hosts for each website.

1. Create a directory for your website inside /var/www folder.

mkdir -p /var/www/

The public folder will contain all the static pages of your website. should be the actual custom domain you point to the server IP.
2. Change the owner to ubuntu using the following command.

chown -R ubuntu:ubuntu /var/www/

3. Change the read permission of the www folder, so that everyone can access the website.

chmod -R /var/www

4.Put a sample html page inside the public folder

nano /var/www/

Enter any sample HTML code

<h1> This is the virtual host for</h1>

5.Copy the default virtual host file to

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/

6. Open the file under sites-available folder and copy the following contents.

server {
        listen   80; ## listen for ipv4; this line is default and implied
        root /var/www/;
        index index.html index.htm;

        # Make site accessible from http://localhost/

7. create a link between sites-available and sites-enabled file

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

8. restart the Nginx server

service nginx restart

Load balancing configurations are the same as RHEL for Ubuntu.

Share the article and leave a comment for queries.


Pin It on Pinterest

Share This