This blog will demonstrate the method to set up Nginx server blocks on Ubuntu 22.04. Let’s get started!
How to install Nginx on Ubuntu 22.04
For the purpose of installing Nginx on Ubuntu 22.04, follow the given instructions.
Step 1: Update system packages
First of all, hit “CTRL+ALT+T” and update the system packages:
All packages are updated:
Step 2: Install Nginx
Next, install Nginx on your Ubuntu 22.04 system with the help of the provided command:
Step 3: Check Nginx version
After installing Nginx, verify if it is currently running or not:
The given output indicates that the Nginx service is active and running on our system:
Step 4: Firewall Configuration
Now, enable the Firewall on your system:
Step 5: List installed applications
View the list of installed applications using the following command:
Step 6: Open ports for Nginx
Firstly, we will enable Nginx in “HTTP” by utilizing the provided command:
Or enable it in HTTPS:
Another option is to enable Nginx fully for both HTTP and HTTPS:
Step 7: Check Firewall status
Now, type out the given command to get to know about the Firewall status:
Step 8: Access Nginx
After configuring Firewall, it is time to access Nginx on the browser using the “localhost” or the “server IP”:
At this point, Nginx is working perfectly. So, we will now move ahead to set up server blocks for it.
How to set up Nginx server block on Ubuntu 22.04
For the purpose of setting up the Nginx server block on Ubuntu 22.04, follow the given instructions.
Step 1: Create Directory
In the first step, create a directory for the selected domain. In our case, the domain name will be “example.com”:
Step 2: Set Directory ownership
Next, utilize the “$USER” environment variable for setting the ownership of the created directory. The specified command will set the current logged-in user as its owner:
Step 3: Set File permissions
Then, we will assign the read, write, and execute file permissions to our “example.com” domain directory:
Step 4: Create HTML file
Using “nano” editor, create an HTML file that will be served as the home page of our domain:
Paste the given code in the opened HTML file, press “CTRL+O” for saving the added changes and switch back to the terminal by hitting “CTRL+X”:
Step 5: Set up Nginx server block
Now, we will set up an Nginx server block for our domain in the given directory:
Add the following content to the opened file, press “CTRL+S” to save it, and switch back to terminal:
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
Step 6: Enable Nginx server block
Create a symlink for enabling the created Nginx server block:
Step 7: Nginx testing
Make sure that Nginx is working properly or not:
Step 8: Restart Nginx
Restart Nginx on Ubuntu 22.04 with the help of the provided command:
Step 9: Access Nginx server
Lastly, open your favorite browser, and access the created Nginx server block by surfing the specified domain name:
The given output indicates that we have successfully set up the Nginx server block on Ubuntu 22.04.
Conclusion
To set up Nginx Server Blocks on Ubuntu 22.04, firstly, update the system packages. Then, install Nginx with the “$ sudo apt install nginx -y” command. Then enable Firewall and open ports for Firewall. Next, create a directory for your domain, and change its directory permissions and file permission. Next, create an HTML file, and set up an Nginx server block that can be accessed via the added domain. This blog demonstrated the method of setting up Nginx blocks on Ubuntu 22.04.