Share your love
How To Install Nginx Web Server on Ubuntu 22.04
In this guide, we want to teach you How To Install Nginx Web Server on Ubuntu 22.04.
NGINX is a web server but is commonly used as a reverse proxy. It can be scaled efficiently as a web server as well as a reverse proxy. It does not allow you to allocate a process to a particular connection, but it creates a process pool that can be easily shared among multiple connections within the network. Whenever a request is made, a resource will be allocated to the process resulting in better resource utilization that can easily handle extensive connections.
NGINX also helps in setting up a secure connection between your data centers and the outside network. It also works well as an HTTP load balancer that allows you to use multiple different load-sharing mechanisms.
Steps To Install Nginx Web Server on Ubuntu 22.04
Before you start to install Nginx, you need to log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.
Also, you need a domain name that is pointed to your server’s IP address.
Install Nginx on Ubuntu 22.04 From APT Repository
By default, Nginx is available in Ubuntu’s default repository. First, update the APT packages with the following command:
sudo apt update
Now install the Nginx on Ubuntu 22.04 with the following command:
sudo apt install nginx
Configure Firewall For Nginx on Ubuntu 22.04
When your installation is finished you need to adjust the firewall. We assumed that you have enabled UFW from the requirements.
List the available application through the firewall with the following command:
sudo ufw app list
In your output you will see:
Output
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
Here you only need to allow traffic on port 80. you should enable the Nginx HTTP with the following command:
sudo ufw allow 'Nginx HTTP'
Now verify the changes with the command below:
sudo ufw status
Your output should similar to this:
Output
Status: active
To Action From
-- ------ ----
Nginx HTTP ALLOW Anywhere
Nginx HTTP (v6) ALLOW Anywhere (v6)
Let’s see how to check your Nginx web server.
Check Nginx web server Status
At this point, the Nginx web server on Ubuntu 22.04 should be up and running.
To make sure that your service is up and running run the following command:
systemctl status nginx
In your output you will see:
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) Docs: man:nginx(8) Main PID: 1864 (nginx) Tasks: 3 (limit: 2282) Memory: 6.1M CGroup: /system.slice/nginx.service ├─1864 nginx: master process /usr/sbin/nginx -g daemon on; master_process > ├─1865 nginx: worker process └─1866 nginx: worker process
As you can see the service is active and running on your server.
Access Default Nginx Landing Page
Also, you can access the default Nginx landing page. For this, you need your server’s public IP address.
To get your IP address you can use the following command:
hostname -I
Or you can use the curl tool to get your IP:
curl -4 icanhazip.com
Now that you have your server IP address, type it into your web browser to access the default Nginx landing page on Ubuntu 22.04.
http://your_server_ip
If you see this page your Nginx web server is working correctly on Ubuntu 20.04 and you can start to manage your web server.
Manage the Nginx process
Now that you have your Nginx web server up and running, let’s review some basic management commands.
To stop the Nginx on Ubuntu 22.04 run the following command:
sudo systemctl stop nginx
You can start your service when it is stopped:
sudo systemctl start nginx
Also, you can stop and start your service again with the following command:
sudo systemctl restart nginx
If you have made configuration changes you need to reload your service after that:
sudo systemctl reload nginx
Nginx is configured to start automatically at boots by default. If you don’t want that to happen run the following command:
sudo systemctl disable nginx
To re-enable the service to start up at boot, use the following command:
sudo systemctl enable nginx
At this point, you can set up Nginx server blocks for your domain on Ubuntu 22.04.
Let’s see how it works.
Set up Nginx server blocks on Ubuntu 22.04
The Nginx server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server.
Here our domain name is nginx.orcacore.net. remember to replace it with your own domain name in the commands.
First, create a directory for your domain with the following command:
sudo mkdir -p /var/www/nginx.orcacore.net/html
Now you need to assign ownership of the directory with the $USER
environment variable b using the following command:
sudo chown -R $USER:$USER /var/www/nginx.orcacore.net/html
Make sure the permissions of your web roots are correct with the following command:
sudo chmod -R 755 /var/www/nginx.orcacore.net
Here you need to create a sample index.html page. we use vi text editor to create that file you can use your favorite text editor:
sudo vi /var/www/nginx.orcacore.net/html/index.html
Then, add the sample HTML code into the file:
<html> <head> <title>Welcome to nginx.orcacore.net!</title> </head> <body> <h1>Success! The nginx.orcacore.net server block is working!</h1> </body> </html>
save and close the file when you are finished.
Note: If you are interested, you can learn more about the vi by visiting the vi text editor article.
At this point, you need to create a server block with the correct directives with the following command:
sudo vi /etc/nginx/sites-available/nginx.orcacore.net
Paste the following configuration block into the file:
server { listen 80; listen [::]:80; root /var/www/nginx.orcacore.net/html; index index.html index.htm index.nginx-debian.html; server_name nginx.orcacore.net www.nginx.orcacore.net; location / { try_files $uri $uri/ =404; } }
When you are done, save and close the file.
Now you need to enable the file by creating a link from it to the sites-enabled directory with the following command:
sudo ln -s /etc/nginx/sites-available/nginx.orcacore.net /etc/nginx/sites-enabled/
Also, you need to adjust a single value in the /etc/nginx/nginx.conf
file.
Open the file with the following command:
sudo vi /etc/nginx/nginx.conf
Then, find the server_names_hash_bucket_size line and uncomment it by removing the “#” from it.
... http { ... server_names_hash_bucket_size 64; ... } ...
When you are finished save and close the file.
Test for no syntax error with the following command:
sudo nginx -t
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Then, restart the Nginx to enable your changes on Ubuntu 22.04:
sudo systemctl restart nginx
At this step, Nginx should now be serving your domain name. type your domain name in your web browser:
http://your-domain-name
You should see something like this:
Conclusion
At this point, you learn How to install Nginx Web Server on Ubuntu 22.04. and Also, you can set up the Nginx server blocks.
Hope you enjoy it.
You may be interested in this article on the orcacore website: