Getting Started with Nginx on Linux: a Complete Tutorial

Nginx

Nginx pronounced Engine-Ex, is a popular and open-source, lightweight, and high-performance web server software that also acts as a reverse proxy, load balancer, mail proxy, and HTTP cache. Nginx is easy to configure in order to serve static web content or to act as a proxy server.It can be deployed to also serve dynamic content on the network using FastCGI, SCGI handlers for scripts, WSGI application servers or Phusion Passenger modules, and it can serve as a software load balancer. Nginx uses an asynchronous event-driven approach, rather than threads, to handle requests. Nginx’s modular event-driven architecture can provide predictable performance under high loads.
In this tutorial, we are going to get started with Nginx on Linux and use the terminal commands to install and configure a test on it. You will get familiar with all the codes and commands for setting Nginx up and running on your operating system.

What you need to get started:

1. This tutorial is based on Linux. If you are working with Ubuntu 20.04 Linux or Linux Mint, or any other OS of the Linux family, you have a suitable operating system for the following tutorial.
2. A user account with sudo or root privileges.
3. Access to a terminal window/command line

Getting Started with Nginx

1. Installation

First off, you need to update software repositories. This helps make sure that the latest updates and patches are installed. Open a terminal window and enter the following: sudo apt-get update Now, to install Nginx from Ubuntu repository, enter the following command in the terminal: sudo apt-get install nginx If you are on Fedora, you should instead enter this command to install Nginx. sudo dnf install nginx And if you are on CentOS or RHEL, the installation is done using this command: sudo yum install epel-release && yum install nginx finally, we test the installation success by entering: nginx -v If the installation has been successful, You should get a result like this: nginx version: nginx/1.18.0 (Ubuntu)

2. Controlling the Nginx Service

Next, we should get familiar with the controlling commands. Using these commands, you will be able to start, enable, stop and disable the Nginx. First off, we should check the status of Nginx service. To do so, you can use the following command: sudo systemctl status nginx And you can see the result:

Screenshot 2022 08 09 212144 Getting Started with Nginx on Linux: a Complete Tutorial

As you can it is activated and up and running. If it is not activated, you can first start by entering this command in the terminal: sudo systemctl start nginx And then, you will be able to enable it using the following command: sudo systemctl enable nginx If you want to stop the Nginx web service, you can first stop it: sudo systemctl stop nginx And then disable it: sudo systemctl disable nginx Also, if you want to reload the Nginx web service, you can use the following command: sudo systemctl reload nginx And for a hard restart, there is a command as below: sudo systemctl restart nginx

3. UnComplicated Firewall Commands:

Nginx needs access through the system’s firewall. To do this, Nginx installs a set of profiles for the Ubuntu default ufw (Uncomplicated Firewall). To display the available Nginx profiles use this command: sudo ufw app list And you can see the result. Neglect the results other than that of Nginx.

Screenshot 2022 08 09 215647 Getting Started with Nginx on Linux: a Complete Tutorial

To get Nginx access through the default Ubuntu firewall, enter the following: sudo ufw allow 'nginx http' Then you need to refresh the firewall settings by entering: sudo ufw reload For https traffic, enter: sudo ufw allow 'nginx https' And for both you can use: sudo ufw allow 'nginx full'

4. Running a Test

To begin running a test, you should first make sure that the Nginx service is running, by checking the status as mentioned earlier. Open a web browser, and enter the following web address: http://127.0.0.1 And you should be able to see the following result containing a page with a welcome statement.

Screenshot 2022 08 09 201215 Getting Started with Nginx on Linux: a Complete Tutorial

Now, if the system does not have a graphical interface, the Nginx Welcome page can be loaded in the terminal using curl: sudo apt-get install curl By entering the following command, you should be able to see the Welcome page contents in the terminal: curl 127.0.0.1 And the result is as expected:

Screenshot 2022 08 09 194729 Getting Started with Nginx on Linux: a Complete Tutorial

5. Configuring a Server Block

In Nginx, a server block is a configuration that works as its own server. By default, Nginx has one server block preconfigured. It is located at /var/www/html. However, it can be configured with multiple server blocks for different sites.
Note that in this tutorial, we will use test_domain.com for the domain name. This may be replaced with your own domain name.
In a terminal, create a new directory by entering the following command: sudo mkdir -p /var/www/test_domain.com/html Use chmod to configure ownership and permission rules: sudo chown –R $USER:$USER /var/www/test_domain.com
sudo chmod –R 755 /var/www/test_domain.com
Open index.html for editing in a text editor of your choice (we will use the Nano text editor): sudo nano /var/www/test_domain.com/html/index.html You will see the HTML code like below in it. You edit it if you like, but we will keep it this way.

Screenshot 2022 08 09 195642 Getting Started with Nginx on Linux: a Complete Tutorial

Press CTRL+o to write the changes, then CTRL+x to exit.
Now, open the configuration file for editing: sudo nano /etc/nginx/sites-available/test_domain.com Enter the following code in it: server {
listen 80;
root /var/www/test_domain.com/html;
index index.html index.htm index.nginx.debian.html;
server_name test_domain.com www.test_domain.com;
location / {
try_files $uri $uri/ =404;
}
}
So that you have the following result:

Screenshot 2022 08 09 180653 Getting Started with Nginx on Linux: a Complete Tutorial

Press CTRL+o to write the changes, then CTRL+x to exit.
Next, create a symbolic link between the server block and the startup directory by entering the following: sudo ln –s /etc/nginx/sites-available/test_domain.com /etc/nginx/sites-enabled Afterward, you should restart Nginx by running the following command: sudo systemctl restart nginx Then, open /etc/hosts for editing: sudo nano /etc/hosts You will see the following result:

Screenshot 2022 08 09 200352 Getting Started with Nginx on Linux: a Complete Tutorial

Next, enter this command after the last line: 127.0.1.1 test_domain.com www.test_domain.com So that it becomes like this:

Screenshot 2022 08 09 200443 Getting Started with Nginx on Linux: a Complete Tutorial

Now if you open a browser window and navigate to test_domain.com (or the domain name you configured in Nginx). You should see the message you entered in HTML file you opened with nano. Notice that there were already an HTML script in there and we didn’t change it. But anyway, if you have changed the HTML file, you will see the result of your edition different from ours:

Screenshot 2022 08 09 201202 Getting Started with Nginx on Linux: a Complete Tutorial


Conclusion

In this tutorial, we have provided the guidelines for installing, starting, and setting Nginx up and running on Linux. Also we tested the configuration and in the end, we configured an Nginx server block. We hope you enjoyed this quick Nginx configuration tutorial and it has been helpful for you.

Download this Article in PDF format

3d websites

Arashtad Custom Services

In Arashtad, we have gathered a professional team of developers who are working in fields such as 3D websites, 3D games, metaverses, and other types of WebGL and 3D applications as well as blockchain developemnet.

Arashtad Services
Drop us a message and tell us about your ideas.
Fill in the Form
Blockchain Development