Setup and configure NGINX in aws EC2 linux/ubuntu instance

Setup and configure NGINX in aws EC2 linux/ubuntu instance

I spend almost half a day to install and set basic configuration of NGINX in my ubuntu instance of amazon web services. The very common errors like 404, 403 were so annoying that I finished 2 litters of beer. 😛
I finally decided to list down the steps to do the same in a neat and clean way with all the probable common mistakes mentioned.

This article assumes

  • You have already created an aws EC2 ubuntu instance
  • You know how to connect to the instance by ssh

Installing NGINX

Step 1:

Make a ssh connection to your instance

Step 2:

Update your apt-get (cause it may not have details of nginx)

Step 3:

Now run the following to install

Step 4:

Now installing is done by step 3. Let’s start the nginx service with the following command.

Configure NGINX

This configuration guide will show you how to host a certain file.

Create your index.html

//put some dummy text in index and come out

Change the permission

You will find a file named default. If you open it you will see the server blocks.

Configuring your server blocks

Let’s not do any changes in the default file; rather copy a new one from default. By this the default setting for nginx will always be intact. Commands for copying

Now open the file mysite with vim and let’s edit it as following.

Check the root property. The folder www which we created was under a certain username and you must provide the full path to nginx. By default the username of an ubuntu instance of aws is ubuntu. Thus the path will be /home/ubuntu/www/mysite/. Please change if you have a different username.

You don’t need the default_server, remember to remove.

You must change the server_name. Either you can set it as the public DNS of your aws instance (here it’s set to * cause nginx has size limit for server_name), or you can set it to if you have a domain mapped to that aws instance.

Add your server block to sites-enabled

Nginx doesn’t take server configurations from sites-available folder; but from sites-enabled. So navigate to sits-enabled first and check the files there.

In the list, you will find a file default which is not actually a file, but a symlink to the file default in the sites-available folder.

Because of this, whenever someone change the file default in sites-available, it automatically reflected in sites-enabled too.

We also need to do a similar thing with our mysite file, which we created in sites-available folder. Command is as below.

Symlink creation is done. If you run ls -la you will find the following too.

Restart nginx service

After every single change in any nginx related configurations you must restart the service to make the changes reflected.

If the restart is successful, it will show [OK] message in the right side of your terminal.

All done

Hit the public DNS of your instance in the browser. It should work nicely. If it doesn’t below is a section of probable errors in configuring nginx, describing the solutions.

Errors you may get

Even if the steps above looks very simple, but often after following them also you don’t get the desired output. Reason can be various. Few which I fared has been listed below.

Not restarting properly

If nginx is failed to restart, you can find the error logs in the path /var/log/nginx/error.log. Probable reasons can be:

  • Invalid server block. May be some syntax error.
  • server_name is too long.
  • No server name given


  • Wrong path for the property root in the file mysite
  • Remember to provide actual path; not relative path. (/home/ubuntu/www/*)
  • server_name is not correct


  • Didn’t give the read permission to www ?
  • May be because of user. When you ran chmod it gave permission to current user, whereas nginx use a user www-data as user. You can change the user in /etc/nginx/nginx.conf file, but I wont recomment that. Better way to to provide read permission to www-data. Below is the command for that.

About This Author

Hello! I am Paul Shan, a JavaScript Expert, Full Stack and DevOps Engineer cum Consultant based out of Bengaluru, India.

  • This is really useful . Thanks Paul! 🙂

  • 김진창

    when i type ((default -> /etc/nginx/sites-available/default)) in my instance
    i got error “[2] 31331
    gt: error: neither tool nor script specified; option -help lists possible tools
    -bash: /etc/nginx/sites-available/default: Permission denied
    ubuntu@ip-172-31-4-72:/etc/nginx/sites-enabled$ No command ‘default’ found, did you mean:
    Command ‘defaults’ from package ‘gnustep-base-runtime’ (universe)
    default: command not found”
    how can i fix it

    • Preetam Kajal Rout

      It is not a command to type. It is output of “ls -la” command

  • Lokendra Sharma

    thanx dude ,this is the last option which saved me.