Setup and configure NGINX in aws EC2 linux/ubuntu instance

Paul Shan Sat Oct 03 2015

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. :P
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)

sudo apt-get update

Step 3:

Now run the following to install

sudo apt-get install nginx

Step 4:

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

sudo service nginx start

Configure NGINX

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

Create your index.html

sudo mkdir www
cd www
sudo mkdir mysite
cd mysite
sudo vim index.html

//put some dummy text in index and come out

Change the permission

sudo chmod -R 755 www
cd /etc/nginx/sites-available/

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

sudo cp default mysite

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

server {
        listen 80;
        listen [::]:80;
        root /home/yourusername/www/mysite;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name *;

        location / {

                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules

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.

cd ..
cd sites-enabled
ls -la

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.

default -> /etc/nginx/sites-available/default

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.

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

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

mysite -> /etc/nginx/sites-available/mysite

Restart nginx service

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

sudo service nginx restart

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.

sudo chown www-data: www
// www is your folder name

Written By

Paul Shan


  • E


  • R

    React JS

  • C


    Cascading style sheets only

  • S


    Search engine optimization

  • E


  • C


    wordpress, drupal, jumla, magento and more

Show All


advanced js
Web development
vue css
social share buttons
react datetime picker
jquery chart library
instant search using vue
event loop
best practices

Show All