• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Set Up Nginx Server Blocks on Ubuntu 22.04

Written by Admin, Updated On January 21, 2023
http, nginx, ubuntu, web-server
How to Install Set Up Nginx Server Blocks on Ubuntu 22.04

Nginx server blocks allows you to run multiple websites on a single machine. You can specify the separate document root for each domain. In addition, you also can set a separate security policy and use different SSL certificates for each site and much more. In this tutorial, you will learn how to set up Nginx Server Blocks on a Ubuntu 22.04 Jammy Jellyfish.

Create Nginx Server Blocks On Ubuntu 22.04#

Nginx server blocks are same as virtual hosts as in Apache.

Prerequisites#

  • Logged in on a Ubuntu server with a root or user with sudo privileges.
  • Nginx must installed and configured, as shown in How to Install Nginx on Ubuntu 22.04 Server.
  • A domain name should pointing to your server IP address.

Create the Directory Structure#

Firstly, we will create a directory structure to store the site data and serve response to visitor’s requests.

The top level directory is considered as DocumentRoot directory. You can set any location as document root as per your choice. It is best practice to define in directory structure. We will store all document root at /var/www directory.

/var/www/
 ├── example1.com
 │   └── public_html
 ├── example2.com
 │   └── public_html

After that, we will create separate directory for each domain inside /var/www directory. Within this directory, we’ll create a public_html directory as domain document root directory to store website data.

sudo mkdir -p /var/www/example.com/public_html

Next, we will create a index.html file within the domain document root directory for testing purpose. By default, this page will display when visitors visit your website.

You can create a new index.html file using your favorite text editor type:

sudo nano /var/www/example.com/public_html/index.html

Add the following lines into it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Welcome!!</title>
  </head>
  <body>
    <h1>Great! example.com set up completed!</h1>
  </body>
</html>

Here, we need to change ownership of document root directories to avoid permission issue later for regular user.

sudo chown -R www-data: /var/www/example.com

Creating a Server Block#

In Debian systems, default location for Nginx server blocks configuration files is at /etc/nginx/sites-available directory. Those can be used by enabling through symbolic links to the /etc/nginx/sites-enabled/ directory.

Create a new file for example.com using your choice text editor by typing :

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

Now, add the following lines in to this file:

server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/public_html;

    index index.html;

    server_name example.com www.example.com;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    location / {
        try_files $uri $uri/ =404;
    }
}

You can define any name to your configuration file but it’s good practice to give file name same as domain name.

Next, We need to enable nginx server block by making symbolic link at /etc/nginx/sites-enabled/ directory. Run the below command to create symbolic link:

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

To test the Nginx configuration for correct syntax by typing:

sudo nginx -t

It will show following output if there is no error.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

You must restart the Nginx service to take effect. Execute below command:

sudo systemctl restart nginx

Finally, to verify the server block is working as expected open http://example.com to your web browser and it should show you as following :

nginx-server-block-welcome-page

Conclusion#

We have shown how to create Nginx server blocks to host multiple website on a single Ubuntu 22.04 server. Repeat the steps to create multiple server blocks for all your domains.

If you have any question, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Set Up Apache Virtual Hosts on Ubuntu 22.04
Next Article   How to Setup a Firewall with UFW on Ubuntu 22.04

Related Posts

  • How to Install WordPress with Nginx on Debian 11

    How to Install WordPress with Nginx on Debian 11

    March 22, 2023
  • How to Install Apache, MySQL, PHP (LAMP) on Ubuntu 22.04

    How to Install LAMP on Ubuntu 22.04

    March 20, 2023
  • How to Install LEMP Stack on Ubuntu 22.04

    How to Install LEMP Stack on Ubuntu 22.04

    March 18, 2023

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook