How to Host a Website in NGINX Web Server Ubuntu

In this tutorial you will learn how to host a website in nginx server in ubuntu
Prerequisite
Learn how to install NGINX in ubuntu server
1. Create a folder for the website that you want to host in var/www/html
You can run the following command to go to html folder in ubuntu
cd var/www/html
2. Create site directory in html folder
for example i created a directory example.com in html folder. To create a directory in html directory run the following command
sudo mkdir example.com
Output

Now create a sample page in var/www/html/example.com run the following command
sudo nano index.html
Copy and Pase the following code in index.html and press CTRL + X and then type Y and press Enter to save the file
<html> <head> <title>Example Site</title> </head> <body> <h1>Congratulations, Example.com site successfully created</h1> </body> </html>
3. Host a website in nginx
To host a website navigate to nginx installation directory. The default installation directory is etc/nginx. Run the following command to go in the nginx directory
cd etc/nginx
now navigate to the sites-avalable directory which is located in etc/nginx. Run the following command
//Run this command to go in the sites-available directory and press enter cd sites-available //Run this command it will list all the sites available in nginx server ls //Now copy default site, in my case i want to host example.com domain sudo cp default example.com
Output

4. Edit example.com and make changes as mention below
To edit the example.com site run the following command
sudo nano example.com
Now replace the content of example.com with following code
server { listen 80; listen [::]:80; root /var/www/html/example.com; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name example.com; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } }
5. Now enable the site in nginx server
To enable the site in nginx run the following command
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
6. Restart nginx server
Run the following command to restart the nginx server
sudo service nginx restart
Add your server ip address in your domain and wait for few minutes then enter domain name in your browser
i.e example.com
Learn how to install LetsEncrypt SSL centificate NGINX ubuntu