How to setup Nginx Server on Ubuntu 20.04

How to set up Nginx Server on Ubuntu

Introduction

In this tutorial, we will learn how to setup Nginx Server on Ubuntu 20.04 and host a website with the help of Nginx.

Nginx is a popular web server used for web hosting. Nginx is more effective and efficient as compared to the Apache webserver. The configuration of Nginx is a little bit different from Apache. Nginx offers a user-friendly environment for web-hosting.

Prerequisites

  • Ubuntu 20.04
  • Root User Access or User with sudo privileges

Setup the Server

Update the server and install the latest packages, also install the required packages.

  • To update the server package list.
apt update -y
  • Upgrade the packages of the server.
apt upgrade -y
  • Install Vim editor.
apt install vim -y

Install Nginx Web Server

First, we need to install the Nginx package on the server. To install the Nginx packages run the following command.

  • Install the nginx package
apt install nginx -y
  • Enable the Nginx service
sudo systemctl enable nginx
  • Restart the Nginx service
sudo systemctl start nginx
  • Check the Status of Nginx service
sudo systemctl status nginx

Checking your Web Server

After the installation, we need to check the Nginx is working perfectly or not. To check the Nginx simply enter the Domain name or server’s Public IP address in the URL section of the browser.

If you don’t know the IP address of the server. To check the IP address run the following command.

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

You can also run the below command if the above command does not work.

curl -4 icanhazip.com

After checking the IP address enter the IP address in the browser’s URL section. You will the default page of Nginx if everything is working perfectly.

Manage the Nginx Process

You can manage the Nginx service with the help of the following commands.

  • Start the Nginx service
sudo systemctl start nginx
  • Check the status of the Nginx Service
sudo systemctl status nginx
  • Stop the Nginx service
sudo systemctl stop nginx
  • Restart the Nginx service
sudo systemctl restart nginx
  • Enable the Nginx service
sudo systemctl enable nginx
  • Disable the Nginx service
sudo systemctl disable nginx

Virtual hosing with Nginx

  • Create a directory structure for the domain which you want to host.
sudo mkdir -p /var/www/linuxpanda

As per our desire, we can create the directory with any name and anywhere in the file system.

Replace linuxpanda with the desired directory name.

  • Create a sample file named as index.html in the linuxpanda directory.
vim /var/www/linuxpanda/index.html
  • Add the following content in the file. Save and exit from the file.
<html>
    <head>
        <title>Welcome to Linuxpanda.com</title>
    </head>
    <body>
        <h1>Success! Your Nginx server is successfully configured</h1>
        <h1> Linuxpanda.com is working perfectly</h1>
    </body>
</html>
  • Change the ownership of the linuxpanda directory.
sudo chown -R www-data:www-data /var/www/linuxpanda
  • Set permissions the following permissions.
chmod -R 755 /var/www
  • To serve the content, you need to create a configuration file for the domain which we are hosting. So that it can point to our custom web root.
sudo vim /etc/nginx/conf.d/linuxpanda.conf
  • Paste in the following configuration in the newly created server block.
server {
        listen 80;
        listen [::]:80;

        root /var/www/linuxpanda;
        index index.html index.htm index.nginx-debian.html;

        server_name Linuxpanda.com www.Linuxpanda.com;

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

Save and Exit from the file.

  • After creating the configuration we need to check the Nginx configuration is error free or not. To check the error run the following command.
sudo nginx -t

If the configuration has errors then it will show the errors, otherwise, it will show the following output which shows that the configuration is error-free.

Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  • Restart the Nginx service to save the changes.
sudo systemctl restart nginx
  • Open the browser and enter the domain name which you have hosted on the server. You will the following page after successfully configured the nginx.

Remove / Uninstall Nginx

  • Stop the Nginx service.
sudo systemctl stop nginx
  • Disable the Nginx service
sudo systemctl disable nginx
  • Delete the directory related to Nginx.
sudo rm -rf /etc/nginx
sudo rm -rf /var/log/nginx
sudo rm -rf /var/cache/nginx/
  • Remove the created Nginx service script under systemd
sudo rm -rf /usr/lib/systemd/system/nginx.service

Conclusion

In this tutorial, we have learned how to setup Nginx Server on Ubuntu and Host the website on Ubuntu 20.04.

If you guys face any issue or problem then mention your quires in the comment section.

Read Also: How to Add and Delete Users on CentOS 8