How To Set Up Nginx Server Blocks on Ubuntu 24.04
Nginx is a versatile and powerful web server renowned for its low resource consumption, rich feature set, and stability. Setting up Nginx server blocks on Ubuntu 24.04 begins with installing Nginx and creating a directory structure for storing website files. Each server block can be configured with its domain or subdomain, allowing users to host multiple websites on a single server.
Nginx facilitates handling load balancing and client requests that are utilized as a reverse proxy. It makes a good choice for improving the performance as well as scalability of a website.
This article will teach the procedure of setting up Nginx server blocks on Ubuntu 24.04, based on the below-supported content:
How to Install Nginx on Ubuntu 24.04
Nginx is a high-performance web server software that is a popular choice for hosting websites on Linux systems, including Ubuntu. To install the Nginx server on Ubuntu, follow the below steps:
Step 1: Update Package Lists
Begin by updating the package lists to make sure the recent version of the repository listings:
sudo apt update
Step 2: Install Nginx
If users haven’t pre-installed Nginx, they can install the Nginx package using the “apt” command with the superuser:
sudo apt install nginx
Step 3: Adjust the Firewall
If you have the UFW firewall enabled, allow traffic on Nginx. Nginx has three basic settings to choose from:
1. Full Access: Lets regular web traffic through on port 80 and secure traffic on port 443.
2. HTTP Only: Only allows regular web traffic on port 80.
3. HTTPS Only: Only allows secure traffic on port 443.
These settings help control what type of web traffic can find the website.
You can enable one of them using the following command:
sudo ufw allow 'Nginx HTTP'
To check the Firewall status, use the “ufw” utility along with the “status” option as below:
sudo ufw status
Step 4: Managing Nginx (Optional)
Nginx is widely used for hosting websites and is the preferable choice. For managing Nginx services, follow the below commands:
| Managing Nginx Services | Commands |
| Start the Nginx Service | sudo systemctl start nginx |
| Stop Nginx Service | sudo systemctl stop nginx |
| Restart Nginx Service | sudo systemctl restart nginx |
| Enable Nginx to Start on the Boot | sudo systemctl enable nginx |
| Enable Firewall | sudo ufw enable |
| To Enlist Installed Applications | sudo ufw app list |
Step 5: Verify the Installation
After the installation, users can verify that Nginx is running by typing:
sudo systemctl status nginx
Step 6: Access Nginx Page
Users can also access the Nginx web page by using the “localhost” or “IP address”. The current IP address of the Ubuntu machine is “10.0.2.15”:
That is all from the installation of Nginx on Ubuntu 24.04.
How To Set Up Nginx Server Blocks on Ubuntu 24.04
Setting up Nginx server blocks on Ubuntu 24.04 involves a series of steps ensuring each domain hosted on the server has a unique environment. For setting up Nginx server blocks on Ubuntu 24.04, follow the below steps:
Step 1: Create Directory Structure
Each website you host needs its folder where its files reside. Creating a folder structure within “/var/www/” for each domain is preferable.
Once Nginx is installed, the first step is to create a folder structure that holds the website files. This is typically done in the “/var/www/” directory, with a separate “html” subdirectory for each domain to serve as the document root:
sudo mkdir -p /var/www/linuxgenie.com/html
Note: Replace “linuxgenie.com” with the actual domain name.
Step 2: Assign Ownership
The ownership of the directory must be assigned to the Nginx user, usually “www-data”. It avoids permission issues:
sudo chown -R $USER:$USER /var/www/linuxgenie.com/html
Step 3: Set File Permission
Now, users are required to set read, write, and execute permission with the “chmod -R” command by mentioning the path:
sudo chmod -R 755 /var/www/linuxgenie.com
Step 4: Create an HTML file
After setting file permission, users need to create an HTML page. For instance, create an “index.html” file under the “/var/www/linuxgenie.com/html/” directory:
sudo nano /var/www/linuxgenie.com/html/index.html
Paste the below HTML script in the created configuration file:
<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Linuxgenie empowers the Ubuntu Users</title> </head> <body> <h1> Setting Up Nginx Server Blocks on Ubuntu 24.04 </h1> </body> </html>
Finally, save and exit the file.
Step 5: Create Server Blocks
Server blocks are the Nginx equivalent of virtual hosts in Apache. They allow users to specify configurations for individual domains. Here’s an example of a server block configuration:
sudo nano /etc/nginx/sites-available/linuxgenie.com
Paste the below script in the server block configuration file. It listens on the default port 80 for requests to “linuxgenie.com” and serves files from the specified root directory:
server {
listen 80;
listen [::]:80;
root /var/www/linuxgenie.com/html;
index index.html index.htm index.nginx-debian.html;
server_name linuxgenie.com www.linuxgenie.com;
location / {
try_files $uri $uri/ =404;
}
}
After that, save and exit the configuration file.
Note: These files define the configuration for each domain, including the server name, document root, and locations for access and error logs.
Step 6: Enable Server Blocks
After creating server blocks, users need to enable them by creating links in the “sites-enabled” folder via the “ls” command:
sudo ln -s /etc/nginx/sites-available/linuxgenie.com /etc/nginx/sites-enabled/
Note: Users can repeat this for each server block if they have created it.
Step 7: Test and Restart Nginx
Always authenticate the recent configuration for any issue before starting Nginx. It is possible through the “nginx” command with the “t” option:
sudo nginx -t
Step 8: Restart Nginx
If the test is successful, users can restart Nginx to apply the changes with the “systemctl” command:
sudo systemctl restart nginx
Note: Ensure that your firewall is configured to allow traffic on the necessary ports. For Nginx, usually, ports 80 (HTTP) and 443 (HTTPS) are required.
Step 9: Verify Nginx Setup
Finally, verify that your websites are accessible by navigating to the localhost/domain_names/IP_ address in a web browser:
By following these steps, Nginx server blocks have been set up, and host multiple websites on the Ubuntu 24.04 server.
Conclusion
Setting up Nginx server blocks on Ubuntu involves a few key steps: creating the directory structure for your domains, configuring the server block files, and enabling them. The process starts with creating directories in “/var/www” for each site, setting the correct permissions, and then proceeding to configure the server blocks within the Nginx configuration files located in the “/etc/nginx/sites-available” folder.
Once configured, create symbolic links in the “/etc/nginx/sites-enabled” folder to enable them. Finally, testing the configuration to ensure everything is set up correctly is crucial before going live.













