This blog will provide you with detailed instructions on how to setup, install and configure Nginx on Ubuntu 20.04. The installation steps are perfect when you perform the similar commands mentioned in this blog.
First, Update the local packages by using the below command.
#sudo apt-get update
Later, proceed to install Nginx:
#sudo apt-get install nginx
Continue with Yes…
Once the installation is done, then check the status of the Nginx service by the following command:
#sudo service nginx status
Type the following command to check the Nginx version,
#sudo nginx -v
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'
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.
Inside that file, add the following HTML code:
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.
Save and close the file to exit ctrl + x.
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:
By performing the above steps, commands, and procedure, the installation and configuration of Nginx on the ubuntu 20.04 server is easy on the go! However, these simple steps make the configuration work easier.