How to Install and Configure Nginx on Ubuntu 20.04 || RS Digitech

04 May 2021

blog details

How to Install and Configure Nginx on Ubuntu 20.04 || RS Digitech

By: RSDigitech | Published At : 04 May 2021    


How to install and configure Nginx server on Ubuntu 20.04

This blog will provide you step-by-step instructions on how to set up Nginx on Ubuntu 20.04. The installation is straightforward, just type the following commands as shown here:

First, Update the local packages by using the below command:

#sudo apt-get update

Later, proceed to install Nginx:

#sudo apt-get install nginx

package update

 Continue with Yes…

Once the installation is done, then check the status of the Nginx service by the following command:

#sudo service nginx status


cli screen

Type the following command to check the Nginx version,

#sudo nginx -v

nginx version check

 

Configure firewall

Enable Nginx HTTP and HTTPS using UFW firewall. Make use of the following command to enable it soon:

sudo ufw allow 'Nginx Full'

sudo ufw allow 'Nginx HTTP'

sudo ufw allow 'Nginx HTTPS'

Create and configure server blocks

For hosting multiple websites on the server, start to configure the server block for each website first.

Usually, you can see server blocks defined in .conf files located at /etc/nginx/conf.d.

The default server root directory is /usr/share/nginx/html/ and this works for a single website. Hosting multiple websites on a server here is unmanageable. So, create a directory structure with /var/www for host_name.

Create the directory for host_name as follows, using the -p flag to create any necessary parent directories:

# mkdir -p /var/www/host_name/html

Now, assign the ownership of the directory with the $USER environment variable, which represents your current system user:

#chown -R $USER:$USER /var/www/your_domain/html

Next, we will create an index.html file to test the server block configuration.

#vi /var/www/website_name/html/index.html

Inside that file, add the following HTML code:

 update index file content

 Now, Create a server block.

Instead of using the default server block, we can create a new server block in /etc/nginx/sites-available/ by using as shown below command:

# vi /etc/nginx/sites-available/host_name

Post-editing the file, it will be like this now.

post editing in file


 Save and close the file to exit ctrl + x.

Enable Nginx Server Blocks

We need to enable our Nginx server blocks, we can do this by creating symbolic links from these files to the sites-enabled directory, which Nginx reads from during start-up.

We can create these links by typing:

$ sudo ln -s /etc/nginx/sites-available/host_name.com/etc/nginx/sites-enabled/

Next, test to make sure that there are no syntax errors in any of our Nginx files by using the below command:

$ sudo nginx -t

If there are no problems found, enables the changes by restarting Nginx:

# sudo service nginx restart

Now, we should test that our server blocks are functioning correctly. We can do that by using visiting the domains in our web browser:

http://host_name.com

Technologywe use

  • PHP
  • Angular
  • Android
  • Laravel
  • Salesforce
  • Tibco
  • Mulesoft