How to Install Nginx on Ubuntu 22.04 LTS

In this article we are going to cover How to Install NGINX on Ubuntu 22.04 LTS,Adjust the Firewall in Ubuntu 22.04 LTS,Verify Nginx Installation on Ubuntu 22.04 LTS,Managing Nginx processes using systemctl,Configure Nginx Server-Blocks onUbuntu 22.04 LTS,Nginx Configuration files and Directories.

What is Nginx ?

Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption.
Nginx is a web server that is used to serve web pages. It is a popular choice for web servers because it is fast and reliable. Nginx can be used to host websites and applications. It can also be used as a reverse proxy server.

Benefits of using Nginx

Nginx is a high-performance web server with a small footprint. It is efficient and can handle a large number of concurrent connections with ease. Nginx is also highly scalable and can be used to load balance across multiple servers.

Using Nginx can result in significant performance gains for your website or application. Nginx is able to handle more concurrent connections than other web servers, meaning that it can serve more users at the same time without slowing down. Nginx is also much more efficient than other web servers, using less memory and CPU resources.

Additionally, Nginx is highly scalable and can be used to load balance across multiple servers. This means that if you have a large website or application with heavy traffic, Nginx can distribute the load across multiple servers to ensure that your site remains responsive.

Why might someone choose to use Nginx over another web server software ?

Main reasons:

  1. Nginx is known for its performance and efficiency. It can handle a large number of concurrent connections and has a small memory footprint, which makes it a good choice for high-traffic websites.
  2. Nginx is also highly scalable. It can be easily configured to handle a large amount of traffic, and it can be easily scaled up or down as needed.
  3. Nginx is also very versatile. It can be used as a reverse proxy, load balancer, and HTTP cache, which makes it a good choice for a wide range of applications.
  4. Nginx is also easy to configure and maintain. Its configuration files are simple and straightforward, which makes it easy to manage even for users who are not experienced with web server software.

Overall, Nginx is a popular choice for web server software because of its performance, efficiency, scalability, versatility, and ease of use.

Prerequisites:-

  • You should have create VM (ubuntu).
  • Have a user account with sudo privileges 
  • Have a domain name pointing to your public server IP.

Step #1:Install Nginx on Ubuntu 22.04 LTS

First update the system packages on Ubuntu 22.04 using below command

sudo apt update
How to Install Nginx on Ubuntu 22.04 LTS 1

if you want to upgrade your system packages you can use below command however it is optional

sudo apt –y upgrade
How to Install Nginx on Ubuntu 22.04 LTS 2

Install NGINX on Ubuntu 22.04 LTS using apt

sudo apt install –y nginx
How to Install Nginx on Ubuntu 22.04 LTS 3

use below command to check Nginx service status using systemctl

systemctl status nginx
How to Install Nginx on Ubuntu 22.04 LTS 4

We have covered Install Nginx on Ubuntu 22.04 LTS.

Step #2:Adjust the Firewall in Ubuntu 22.04 LTS

If you are installing Nginx on you local system then allow traffic on port 80 in ufw , if you are installing on Cloud Instance then allow port in cloud security group.

sudo ufw allow 80/tcp

OR

sudo ufw app list

you will see list of profiles

OutputAvailable applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

To allow traffic on port 80, enable this by typing:

sudo ufw allow 'Nginx HTTP'

You can verify the change by typing:

sudo ufw status

The output will indicated which HTTP traffic is allowed:

Output
Status: active

Step #3:Verify Nginx Installation on Ubuntu 22.04 LTS

To Verify Nginx was installed correctly, open a web browser and type in the address bar http://server_ip_address

After opening this URL you can see the Nginx default page as in the image below:

VM IP / localhost
How to Install Nginx on Ubuntu 22.04 LTS 5

Step #4:Managing Nginx processes using systemctl

Below are systemctl commands to manage Nginx web server on Ubuntu 22.04 LTS

Use below command to stop Nginx process using systemctl

sudo systemctl stop nginx
How to Install Nginx on Ubuntu 22.04 LTS 6

.To start the Nginx web server when it is stopped

sudo systemctl start nginx
How to Install Nginx on Ubuntu 22.04 LTS 7

To restart Nginx web server using systemctl

sudo systemctl restart nginx
How to Install Nginx on Ubuntu 22.04 LTS 8

If you are only making configuration changes, Nginx can reload without dropping connections.

sudo systemctl reload nginx

By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior

sudo systemctl disable nginx

To re-enable the service to start up at boot

sudo systemctl enable nginx  

Step #5:Configure Nginx Server-Blocks on Ubuntu 22.04 LTS

First Create a directory structure as shown below command

sudo mkdir -p /var/www/test.devopshint.info/html

Assign the directory’s ownership to the currently logged-in user. Ensure to be logged in as a regular user rather than a sudo user, it will make it easier for us to edit or create the content in the directories.

sudo chown -R $USER:$USER /var/www/test.devopshint.info/html

Grant the logged-in user the read, write and execute permissions.

sudo chmod -R 755 /var/www/

create a sample index.html file inside the domain root directory for each of our sites that will be displayed when you visit the domain in your browser.

sudo vim /var/www/test.devopshint.info/html/index.html
How to Install Nginx on Ubuntu 22.04 LTS 9
Paste the following HTML content.
<html>
<head>
<title>Welcome to test.devopshint.info! </title>
</head>
<body>
<h1><center> Welcome to Nginx Server Blocks</h1>
<p><center>Congratulations!! You have susccessfully configured your Nginx Server block.</p>
</body>
</html>
How to Install Nginx on Ubuntu 22.04 LTS 10
Note:- To save and exit press Esc :x+ enter

The location for Nginx servers is in the /etc/nginx/sites-available  directory. We will therefore need to create server blocks that will serve the content in the index.html files we created earlier.

sudo vim /etc/nginx/sites-available/test.devopshint.info

Paste in the content below;

server {
listen 80;
listen [::]:80;
root /var/www/test.devopshint.info/html;
index index.html index.htm index.nginx-debian.html;
server_name test.devopshint.info;
location / {
try_files $uri $uri/ =404;
}
}

To save and exit the file press Esc 😡 enter.

Enable Nginx Server Block (Link the Nginx server block to /etc/nginx/sites-enabled/ directory as shown to enable)

sudo ln -s /etc/nginx/sites-available/test.devopshint.info /etc/nginx/sites-enabled/

You should now confirm whether all configurations are in order.

sudo nginx –t
How to Install Nginx on Ubuntu 22.04 LTS 11
To verify changes restart Nginx service and check Nginx is running or not.
sudo systemctl restart nginx
systemctl status nginx
How to Install Nginx on Ubuntu 22.04 LTS 12
Nginx is running on your Ubuntu machine.
Test the Nginx server block we will test by browsing your domain name in your favorite browser.

Add A record in with your domain provider with your VM IP to access your server blocks using domain name

How to Install Nginx on Ubuntu 22.04 LTS 13

Now Access your server block domain on browser as shown below screenshot.

How to Install Nginx on Ubuntu 22.04 LTS 14

Step #6:Nginx Configuration files and Directories

Below are Important Nginx configuration files and directory which you should know

Default Nginx web content:

  • /var/www/html: When we install Nginx be default /var/www/html directory gets created and when you access it on browser then content loads from this directory.

Nginx Server configuration files:

  • /etc/nginx: this is Nginx configuration directory. All of the Nginx configuration files reside here.
  • /etc/nginx/nginx.conf: this is Nginx global configuration file when you modify it , it will apply to all content which used Nginx.
  • /etc/nginx/sites-available/: To enable a website, you must create a symbolic link inside the /etc/nginx/sites-enabled directory pointing to the actual vhost file in /etc/nginx/sites-available . The nginx. conf file reviews the contents of the sites-enabled directory and determines which virtual host files to include
  • /etc/nginx/sites-enabled/: The ../sites-enabled/ folder will include symlinks to the site configuration files located within /etc/nginx/sites-available/
  • /etc/nginx/snippets: Snippets allow you to insert raw NGINX config into different contexts of the NGINX configurations that the Ingress Controller generates.

Conclusion:

In this article we have covered How to Install NGINX on Ubuntu 22.04 LTS,Adjust the Firewall in Ubuntu 22.04 LTS,Verify Nginx Installation on Ubuntu 22.04 LTS,Managing Nginx processes using systemctl, Configure Nginx Server-Blocks onUbuntu 22.04 LTS, Nginx Configuration files and Directories.

Related Articles:

How to Install Nginx Ingress Controller Kubernetes KOPS using Helm 3

Reference:

Nginx official documentation

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap