Setup Nginx step by step

The last time, we have installed a linux system and have done some preparation to make it ready to use. Now we will install a Nginx on it, and then your private server will become a web server and you can broadcast your ideas to the whole internet :)

Still, I will use Ubuntu 16.4.1, but solutions here still adoptable to the other linux distro.

1. Install Nginx.

Linux has some fantastic package management system. And apt-get is the Ubuntu’s answer for this. You can use the default Ubuntu’s command to install nginx without any problem.

sudo apt-get update
sudo apt-get install nginx

We first update our repository list, then we update it.

2. Let the firewall run.

Remember the last time when we setup a linux system? Firewall is important, it can make your server more secure, and after we install the nginx, we definitely will install one. Linux itself use a packet filtering system named netfilter, and you need to use commands like iptables to manipulate it to create your rules. While this takes time to get familiar with. Ubuntu has a ufw (uncomplicated firewall) to greatly simplify the procedures.

You can use the list command to check the application profiles.

sudo ufw app list

And you will find that Nginx is on the list since it is installed on the system. Now let’s enable it.

sudo ufw allow 'Nginx HTTP'

Let’s check the result:

sudo ufw status

You should find the action for Nginx is displayed as ALLOW.

3. Set up the domain.

3.1 if you have a domain.

Now you can go to your domain provider to set your domain to the IP of your server. And wait a few minutes for the DNS to refresh. Then when you type your domain in the browser. You will see the welcome page of Nginx.

3.2 if you don’t have a domain name.

IP is OK too, just type it in the browser, and you will see the result too :)

4. Important directory of Nginx.

There are something you need to remember before using is the following place:

  • /var/www/html : This is the default folder to put the webpage.
  • /etc/nginx : This is folder to maintain the configuration files for nginx.
  • /etc/nginx/nginx.conf : The global Nginx configuration file.
  • /etc/nginx/sites-available : This is where you put the configuration for your site. If you have multiple sites, you will need create multiple files in this folder.
  • /etc/nginx/sites-enabled : After you create the above configuration files, link them to this folder and nginx will recognize it and server it.

5. Config your first site.

5.1 go to the default folder

cd /var/www/html

5.2 create a simple web page

sudo vim index.html

After the interface has shown, press i to enter interactive mode to enter your page. Let’s make it simple.

<h1>I love nginx!</h1>

When you finish, press esc, then type :wq. This will save your page and back to the terminal.

6. Config the sites.

6.1 Create a configuration file to host the previous page

Go to the configuration folder first.

cd /etc/nginx/sites-available

Create the configuration files

sudo vim mysite

Then type the following, be careful, not to miss a single symbol.

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


index index.html index.htm;

root /var/www/html;

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

It is easy to understand, right?

  • The server block simply defines a server
  • then the following two lines says it should listen to port 80, this is the default port for HTTP service.
  • server_name indicates which domain name it should bound to.
  • index indicates the default page when people try to visit your domain.
  • root indicates the folder which contains the pages. Here we use the default one.
  • location will map the request to your actual folder.

When you finish, press ESC, then type :wq to save and exit VIM.

7. Make it alive.

Now as we said before, you should link your configuration file to the sites-enable folder to make it alive. You can use this command:

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

Then remove the default site, don’t need to worry, you just remove the link file, the actual file still sits in your sites-available folder:

sudo rm /etc/nginx/sites-enabled/default

8. Config the Nginx server

Calm down, we are nearly there. :)
Since it is a fresh new nginx, we need to configure something.

Let’s start.

sudo vim /etc/nginx/nginx.conf

When the contents displays, press i to enter interactive mode. Now pay attention, don’t mess up the original file, just do what I tell you to do.

8.1 Modify the user

The first line, it indicates the user that installs this nginx, you should change it to the current login user since you use this user to install nginx.

user albertgao;

8.2 Uncomment the below line

You just need to delete the

server_names_hash_bucket_size 64;

Now, press ESC, then type :wq to save and exit VIM.

8.3 Test your settings.

Use this command:

sudo nginx -t

It should show something like below:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

8.4 More commands:

There are more commands for you if you need them in the future. They are very much self-explained, so I won’t go through them one by one.

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl enable nginx
sudo systemctl disable nginx

Otherwise, you are making mistake in the previous steps, just re-check the nginx.conf.

9. Now let’s setup the permission of your HTML folder

Linux is famous for its permission system. We just set to the nginx user to you, and we need to set the container folder /var/www/html to you, so the nginx can read it since you can only access anything belongs to you.

sudo chown -R $USER:$USER /var/www/html

But not enough, we only set the subfolder, we need to setup the permission for the parent folder too.

sudo chmod -R 755 /var/www

9.1 Restart the nginx and enjoy!

The command is simple:

sudo service nginx restart

Now, just type your domain name in the browser, you should see the default index.html we’ve just created.

9.2 Story ends here.

Now it is a long journey. But the logic is very clear:

  • Install nginx.
  • config the site
  • config nginx
  • prepare the permission
    And you are good to go :)

Thanks for reading!

Follow me (albertgao) on twitter, if you want to hear more about my interesting ideas.