Setting up a simple webserver using Nginx

Author: Sumit Dhiman

Tags: Hosting Web Server Nginx

February 14, 2023


First Step:

First of all, we need to access our server’s shell. So, we will use SSH to do so. type following command in your local computer’s terminal:

ssh <username>@<ip-addresss>

then, enter your password if you havn’t added your ssh key on the remote server.

Second Step:

Now , we will be installing our requirements:

sudo apt install nginx vim

Following command will start the nginx daemon in background and set the service to autostart.

sudo systemctl enable --now nginx

Third Step:

create a file as follows:

sudo vim /etc/nginx/sites-available/mywebsite

You can change mywebsite to any thing you want.

Add following content in your file.

server {
        listen 80 ;
        listen [::]:80 ;
        server_name example.org ;
        root /var/www/mywebsite;
        index index.html index.htm index.nginx-debian.html ;
        location / {
                try_files $uri $uri/ =404 ;
        }
}

Now, we have to add our HTML. Create a index.html file using below command:

sudo mkdir /var/www/mywebsite
sudo vim /var/www/mywebsite/index.html

Add following html syntax in it.

<h1>Your website is running</h1>

Final step:

Run below command to enable your site config:

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

Now, reload the nginx daemon using below command:

sudo systemctl restart nginx

Now, visit your the url as http://<ip-addresss>. And, you will see your website running.