Posted On: September 19, 2022 | min read
Hello geeks! In this blog we will walk through the installation process of the NGINX server for machines running on Ubuntu 20.04 and we will try to make our website go live and running right away following some simple steps. But before technical stuff, let’s first start with some basic understanding of what NGINX or Apache Server is.
Nginx and Apache are the two most common open source web servers in the world. At present over 50% of all internet traffic is served by these servers all together. Both options can manage a variety of workloads and collaborate with other programmes to build a full web stack. In this blog we will only learn about NGINX. In our upcoming blogs we will be learning more about Apache.
Let’s Begin…
What is N G I N X – I N T R O D U C T I O N
Nginx is a web server that also functions as a load balancer, reverse proxy, mail proxy, and HTTP cache. Igor Sysoev built this fantastic server, which was made available to the public in 2004. Nginx is free and open-source software distributed under the conditions of the 2-clause BSD license.
Some of the biggest and busiest websites on the internet are hosted by Nginx, one of the most well-liked web servers in the world. Being both a web server and a reverse proxy, it is a lightweight option.
We’ll go over installing Nginx on your Ubuntu 20.04 server, configuring the firewall, managing the Nginx process, and configuring server blocks for hosting many domains from a single server in this article.
Let’s jump to the technical part…
Nginx may be installed using the apt packaging system because it is present in Ubuntu’s default repositories.
We will update our local package index so that we have access to the most recent package listings because this is our first encounter with the apt packaging system in this session. After that, we can set up nginx:
sudo apt update
sudo apt install nginx
Nginx and all necessary dependencies will be installed to your server by apt after you approve the procedure.
The firewall programme must be changed to permit access to the service before Nginx testing. After installation, Nginx registers with ufw as a service, making granting access to Nginx simple.
Type the following to see a list of the settings that ufw can work with for applications: sudo ufw app list
[ OUTPUT ]
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
The result shows that Nginx supports the following three profiles:
It is advised that you activate the most stringent profile that still permits the preset traffic. We simply need to let port 80 traffic for the time being.
You may activate this by keying in:
You can enable this by typing:
sudo ufw allow ‘Nginx HTTP’
You can verify the change by typing:
sudo ufw app list
The output will indicated which HTTP traffic is allowed:
After the installation is complete, Ubuntu 20.04 launches Nginx. The web server ought to be operational by now.
By typing systemctl status nginx we can verify that the service is active with the systemd init system.
[ OUTPUT ]
nginx.service – A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-04-20 16:08:19 UTC; 3 days ago
Docs: man:nginx(8)
Main PID: 2369 (nginx)
Tasks: 2 (limit: 1153)
Memory: 3.5M
CGroup: /system.slice/nginx.service
├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2380 nginx: worker process
By going to your server’s IP address, you may visit the default Nginx landing page to verify that the programme is functioning as intended.
Now type your server’s IP it into the address bar of your browser like follows:
http://your_server_ip
You ought to see the standard Nginx landing page:
Now that your web server is operational, let’s go through some fundamental administrative commands.
Great till now! You should be prepared to set up the website to host several domains now that you are familiar with the fundamental administration commands.
Server blocks, which are akin to virtual hosts in Apache, may be used with the Nginx web server to encapsulate configuration information and host several domains from a single server. Your domain will be the domain name we create; however, you should substitute your own domain name here.
On Ubuntu 20.04, Nginx has one server block enabled by default that is set up to serve files from the /var/www/html directory. While this is effective for a single site, hosting many sites might make it cumbersome. Instead of making changes to /var/www/html, let’s build a directory structure within /var/www for our domain site and leave /var/www/html in place to act as the default directory if a client request doesn’t match any other sites.
Use the -p parameter to establish any required parent directories when creating the directory for your domain:
sudo mkdir -p /var/www/your domain/html
Next, use the $USER environment variable to specify who owns the directory:
sudo chown -R $USER:$USER /var/www/your domain/html
If you haven’t changed the umask value, which determines the default file permissions, the permissions of your web roots should be accurate. You may enter the following command to verify that your permissions are valid and provide the owner read, write, and execute access to the files while only providing groups and other users read and execute access:
sudo chmod -R 755 /var/www/your domain
Next, using nano or your preferred editor, make a sample index.html page as follows:
sudo nano /var/www/your domain/html/index.html
Add the following example HTML inside:
/var/www/your domain/html/index.html
<html>
<head>
<title>NGINX Tutorial</title>
</head>
<body>
<h2>Wonderful! Your website is live and hosted on the NGINX server :)</h2>
</body>
When prompted to save, type Y and then press Enter. This will save and shut the document.
Create a server block with the appropriate directives so that Nginx may deliver this content. Let’s create a new configuration file at /etc/nginx/sites-available/your domain rather than just altering the default one:
sudo nano /etc/nginx/sites-available/your domain.
The configuration block below should be pasted in; it is identical to the default but adjusted for our new directory and domain name:
server {
listen 80;
listen [::]:80;root /var/www/your_domain/html;
index index.html index.htm index.nginx-debian.html;server_name your_domain www.your_domain;
location / {
try_files $uri $uri/ =404;
}
}
You’ll see that we changed the server name to our domain name and the root configuration to point to our new directory.
Now that the file has been enabled, let’s link to the sites-enabled directory, which Nginx reads from when it starts up:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
It should be noted that Nginx employs symbolic links, also known as symlinks, to keep track of which of your server blocks are active. Making a symlink is similar to making a shortcut on your computer’s hard drive, allowing you to retain the server block in the sites-available directory and erase the shortcut from the sites-enabled directory at a later time if you decide to enable it.
You can learn more about how Nginx interprets these directives here. Two server blocks are now activated and set up to react to requests depending on their listen and server name directives:
Responses to queries for both your domain and www.your domain are provided by your domain.
By default, all requests on port 80 that do not match the other two blocks will be reacted to.
It is important to change a single variable in the /etc/nginx/nginx.conf file in order to prevent a potential hash bucket memory issue that may result from adding extra server names. Open the document:
Do a sudo nano on /etc/nginx/nginx.conf
Find the server_names_hash_bucket_size directive and remove the # symbol to uncomment the line. If you are using nano, you can quickly search for words in the file by pressing CTRL and w.
http {
…
server_names_hash_bucket_size 64;
…
}
When you’re done, save and close the file.
Next, check all of your Nginx files to ensure there are no syntax errors:
sudo nginx -t
Restart Nginx to apply your modifications if there are no issues:
sudo systemctl restart nginx
Your domain name should now be served by Nginx. You may verify this by going to http://your domain, where you ought to see the following:
After learning how to control the Nginx server, you should spend some time getting acquainted with a few crucial folders and files.
/var/www/html: The real web content is delivered from the /var/www/html directory, which by default simply contains the default Nginx page you previously viewed. By making changes to the Nginx configuration files, this may be adjusted.
Server Logs
/var/log/nginx/access.log: Every request to your web server is recorded in this log file unless Nginx is configured to do otherwise.
/var/log/nginx/error.log: Any Nginx errors will be recorded in this log.
After setting up your web server, you have a variety of options for the material you want to provide and the technologies you want to employ to give users a richer experience. You can also secure your Linux NGINX Server.
April 11, 2023
Admin wrote
July 8, 2022
Admin wrote
Subscribe to our newsletter