How to Make NginX Server Block (virtual host) on Ubuntu 20.04

Tutorial How to Create an NginX Server Block (virtual host) on Ubuntu 20.04 – In general, shared hosting providers provide services in the form of cPanel which makes it easy for us to configure website configurations including managing subdomains, add-ons domains in it. So how to create a server block (virtual host) for several domains, as well as subdomains on a virtual private server using the Nginx webserver?

What are Server Blocks?

Server Block, often referred to as a virtual host on Nginx is a feature of the Nginx web server that allows us to host multiple websites on one server. In contrast to setting up and configuring servers for each domain, Server Block makes hosting a number of websites on one server an alternative to save time and money.

What is needed to create a Server Block on NginX?

Making NginX Server Block

Before starting to create a virtual host, as usual we need access to the server via SSH. After logging into the server via SSH, make sure and make it a habit to update the package index. So that all installed packages are updated to the new version.

sudo apt update -y && sudo apt upgrade -y

After updating the package index we can start creating Server Blocks. Make sure Nginx webserver is installed or use the command below if you haven’t already installed NginX webserver:

sudo apt-get install nginx

Note: by implementing this server block (virtual host), the server’s ability becomes more burdened because it works with more than one website.

Creating a Directory Structure

Server Block Nginx can develop multiple websites on one server. Since each website has individual site documentation, we need to create an individual directory structure to store the data.

Each Server Block must have a directory in the document root (directory /var/www).

This tutorial shows how to create a Server Block for two servers (domainname1.com and domainname2.com). We can adjust the configuration and customize the resources of each Server Block.

Here we will create 2 new 2 directories for 2 domains. Here we create a new directory on /var/www.

sudo mkdir -p /var/www/namadomain1.com/html
sudo mkdir -p /var/www/namadomain2.com/html

Where is the directory html This is the place where we store the content files from the website.

Here we create 2 simple “Hello World” html files in that directory.

For the first domain:

nano /var/www/namadomain1.com/html/index.html
<html>
	<body>
		<h1>Hello World</h1>
		<p>Test NginX Webserver Domain 1</p>
		<p>by Bagus Judistirah</p>
		<p>https://judisweb.com</p>
	</body>
</html>

Save the file and exit nano editor.

For the second domain:

nano /var/www/namadomain2.com/html/index.html
<html>
	<body>
		<h1>Hello World</h1>
		<p>Test NginX Webserver Domain 2</p>
		<p>by Bagus Judistirah</p>
		<p>https://bjxbro.online</p>
	</body>
</html>

Save the file and exit nano editor.

Because in creating the folder we use root then we need to change the ownership permission of the folder to www-data.

sudo chown –R www-data:www-data /var/www/namadomain1.com
sudo chown –R www-data:www-data /var/www/namadomain2.com

Configuring the NginX Server Block

Before we prepare the server block for the two domains, we need to prepare 2 directories:

  • sites-available: Website directory available to store Server Block
  • sites-enabled: Directory that will tell Nginx which links to publish and which blocks to share content with visitors.

Check the directory that is in the location /etc/nginx

cd /etc/nginx/
/etc/nginx$ ls -l
PicsArt 11 06 10.26.40
NginX Directory

If there is no directory sites-available and sites-enabled on NginX directory then we need to create it using command mkdir:

sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled

Then open the NginX configuration file and edit it as below using the nano editor:

sudo nano /etc/nginx/nginx.conf

Next in the http block, add the following line:

include /etc/nginx/sites-enabled/*;
server_names_hash_bucket_size 64;

If the line already exists, usually use the comment “#” and remove the mark. On line server_names_hash_bucket_size increase how much memory is reserved for checking multiple domain names.

PicsArt 11 06 11.20.53
NginX conf

Note: include /etc/nginx/sites-enabled/*.conf is a configuration file for each domain name ending with the extension .conf or NginX will process domain configuration only with files with extension .conf. By default we are allowed to not use the extension (include /etc/nginx/sites-enabled/*), so either directly use the domain name or NginX processes all the configuration files in that directory.

Creating NginX Server Block Files

The Nginx webserver package comes with a default Server Block with the name default. Since we need to create Server Blocks for each domain, the easiest way to do this is to copy an existing template and modify its specifications.

Creating NginX Server Block files for domainname1.com:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/namadomain1.com.conf

Creating NginX Server Block files for domainname2.com:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/namadomain2.com.conf

After copying the default templates for the 2 Server Block files, next we edit the files with the editor:

sudo nano /etc/nginx/sites-available/namdomain1.com.conf

Server Block Nginx configuration by default:

# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or WordPress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ .php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

There are 3 lines that we need to edit in the NginX Server Block file:

  • server_name must match the domain name of the first website. Be sure to include addresses with and without www. prefix. That way, the server will recognize both types of requests from visitors and redirect them to the same content.
  • Change directory root to match domainname1.com
  • Add command try_files with a 404 error for example when the server receives a request for inaccessible files and directories

Look for “Virtual Host configuration for example.com” and change the 3 lines like server_name, root, try_files:

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {
       listen 80;
       listen [::]:80;
#
       server_name namadomain1.com www.namadomain1.com;
#
       root /var/www/namadomain1.com;
       index index.html;
#
       location / {
               try_files $uri $uri/ =404;
       }
}
Nginx Block Server
Nginx Block Server

Save the file and exit the editor.

Repeat the above steps to edit the Server Block file domainname2.com.

Verify NginX syntax:

sudo nginx -t

Activating NginX Server Block

To enable file server block (virtual host) create symbolic link from directory sites-available to sites-enabled with the command:

sudo ln -s /etc/nginx/sites-available/namadomain1.com /etc/nginx/sites-enabled/namadomain1.com

Repeat with domainname2.com, then restart NginX to reload the configuration.

sudo systemctl restart nginx

After restarting the nginx service, open a web browser and then type the addresses of the two domains into the web browser.

Reference: nginx.org

Conclusion

After reading this tutorial How to Create an NginX Block Server (virtual host) on Ubuntu 20.04, we have understood the basic principles of installing and setting up an Nginx blovk server for hosting multiple domains on Ubuntu 20.04.

Hope it is useful.




Leave a comment

Your email address will not be published.