Nginx is one of the world’s most popular web servers and hosts some of the largest and most visited websites on the internet. It is a lightweight option that can serve as a reverse proxy or web server.
In this post, we’ll examine how to install and configure Nginx on your Ubuntu 22.04 server, change the firewall, deal with the Nginx cycle, and set up server blocks for facilitating more than one space from a solitary server.
On your server, you should have a regular, non-root user with sudo privileges set up before you begin this guide. Follow our Ubuntu 22.04 Initial server setup guide to learn how to set up a regular user account.
To get started, log in as your non-root user when you have an account available.
Step 1: Update the System
You need to update the currently repository by using the given command.
sudo apt-get update
After complete update command, We will ready the install nginx web server on ubuntu machine.
Step 2: Install Nginx Web Server
You need need to execute the given command, We have access to all of the most recent listings for packages. After that, we can set up nginx.
sudo apt-get install nginx -y
Step 3: Verify the Nginx Service Status
After installation of the package, We need to check the
nginx.service by execution following commands.
sudo systemctl status nginx.service
Step 4: Testing Nginx Web Server
We are good to go to check the nginx test webpage in Brower, We just need to get machine IP and we need to open browser with http://localhost or http://ip_address to get test page.
Note: We need to open the port 80 or port 443 from firewall if we have enabled UFW firewall or other security things.
Click here to get http://localhost to view the nginx test web page.
Step 5: Controlling Nginx Service
We can control nginx service using
systemctl command line tools.
To get status your nginx web server, execute :
sudo systemctl status nginx.service
To get start your nginx web server, execute :
sudo systemctl start nginx.service
To get restart your nginx web server, execute :
sudo systemctl restart nginx.service
To get stop your nginx web server, execute :
sudo systemctl stop nginx.service
Nginx can frequently reload without dropping connections if you only make changes to the configuration, To get reload your nginx web server, execute :
sudo systemctl reload nginx.service
In order to enable the nginx service’s boottime startup by execute given command:
sudo systemctl enable nginx.service
Nginx is set up to start automatically when the server boots by default. You can disable this behavior if you don’t want it to happen by execute given command:
sudo systemctl disable nginx.service
Step 6: Nginx Configuration & Logs Path
You should take a few minutes to familiarize yourself with a few important directories and files now that you know how to manage the Nginx service itself.
/var/www/html: The actual web content is served from the
/var/www/html directory, which by default only contains the default Nginx page you saw earlier. Changing the Nginx configuration files can change this.
/etc/nginx: The directory for Nginx configuration. Here are all of the Nginx configuration files.
/etc/nginx/nginx.conf: The main Nginx configuration file. This can be modified to make changes to the Nginx global configuration.
/etc/nginx/sites-available/: the directory that can hold per-site server blocks. The configuration files in this directory will not be used by Nginx unless they are linked to the sites-enabled directory. Typically, this directory is used for all server block configuration, which is then enabled by linking to the other directory.
/etc/nginx/sites-enabled/: The directory that stores enabled per-site server blocks. Links to configuration files in the sites-available directory are typically used to create these.
/etc/nginx/snippets: This directory contains fragments of the Nginx configuration that can be used elsewhere. Snippets are a good way to refactor configuration segments that could be repeated.
/var/log/nginx/access.log: Unless Nginx is configured to do otherwise, this log file records each request to your web server.
/var/log/nginx/error.log: This log will contain any Nginx errors.
We have successfully installed and configured Nginx web server on ubuntu 22.04 LTS, if you still have questions, please post them in the comments section below.