• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Sunday, May 18, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Control Panels

How To Configure Nginx Server Blocks on an Ubuntu 18.04 VPS or Dedicated Server

How VPS by How VPS
November 2, 2018
in Control Panels
0
How To Configure Nginx Server Blocks on an Ubuntu 18.04 VPS or Dedicated Server
0
SHARES
25
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Introduction
  2. Before You Start
  3. Step 1 – Creating A Directory Structure
  4. Step 2 – Creating A Nginx Server Block
  5. Conclusion
  6. Check out these top 3 VPS services:
    1. Was this article helpful?

Introduction

Nginx Server Blocks are powerful applications that permit users to host multiple domains on one server. They are incredibly useful if you have multiple websites and are not ready to go through the time-consuming process of creating a web server for each.

The server blocks utilize the server_name and listen directives to bind to transmission control protocol (TCP) ports. They make it easy to create a unique security policy, specify the document root, and employ a unique SSL certificate for each website.

This tutorial will show you how to configure Nginx server blocks on your Ubuntu 18.04 server.

Ready? Let’s go!

Before You Start

  • An Ubuntu 18.04 VPS or dedicated server
  • A user with root permissions
  • Nginx installed in your Ubuntu 18.04 VPS or dedicated server
  • You also need a domain pointing to the public server Internet Protocol (IP) address. In our tutorial, we’ll utilize example.com as the demo domain name

Step 1 – Creating A Directory Structure

If everything is in place, it’s time to craft a directory structure for our installation. The spine of this structure will be a document root; a location where all files for your specific domain are maintained and presented when a corresponding request is made.

We’ll create a document root using the directory structure below:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html
├── domain3.com
│   └── public_html

Feel free to set your document root to a location of choice.

Here, we’ll fashion a unique directory for every domain to be hosted on the server in the location /var/www. Then, within each of the directory created, we’ll set a sub directory public_html, which will be used to archive the files for the domain.

As aforementioned, we’ll utilize the domain example.com. Now, run the command below to create a root directory for the domain.

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

Next, inside the new root directory (public_html) of your domain, create a file called index.html.

Execute the command below to open the file index.html, and add a demo file:

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

Add the content below in the file:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

Save the changes and exit the nano editor.

In this tutorial, we’ve been running commands using sudo privileges and all the newly fashioned directories and files belong to a sudo user. We need to modify the ownership of these directories and files to avert potential permission issues. We’ll use chown command to modify the ownership of our document root to www-data (Nginx user).

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

That’s it! Everything is set and there should be no permission issues.

Step 2 – Creating A Nginx Server Block

By default, Ubuntu systems stores the configuration files for Nginx server blocks in the directory; /etc/nginx/sites-available. These configuration files are enabled via symlinks to the directory; /etc/nginx/sites-enabled/.

To create Nginx server block, first, use your favorite editor to open the configuration file; /etc/nginx/sites-available/example.com. Copy and paste the content below into the new file:

server {
    listen80;
    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;
    }
}

The above configuration file (example.com) can be given any other name, but it’s always wise to use your domain as the name for the file.

Now, we need to activate the server block configuration file we just created. This can be achieved by creating a symlink or symlink from the configuration file to a directory sites-enabled. Execute the command below to create the symlink:

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

Next, run the command below to authenticate Nginx configuration:

$ sudo nginx -t

If the service syntax is okay, then you should get an output like the one below:

nginx: the configuration file /etc/nginx/nginx.confsyntax is ok
nginx: configuration file /etc/nginx/nginx.conftest is successful

Run the command below to restart your Nginx service and implement all the changes.

$ sudo systemctl restart nginx

To verify if the Nginx server block is functioning correctly, open http://example.com via your favorite web browser. If all went well you should get

How To Configure Nginx Server Blocks on an Ubuntu 18.04 VPS or Dedicated Server

Conclusion

That is it! You have successfully set up a new Nginx server block on your Ubuntu 18.04 server. In case, you want to create more server blocks for all your websites, simply follow the steps outlined above for each domain.

Check out these top 3 VPS services:

0

Was this article helpful?

Submit
Previous Post

Tips to Create ISO from CD, Watch User Activity and Check Memory Usages of Browser

Next Post

How to Setup Fail2ban on your Ubuntu 18.04 VPS Server or Dedicated Server

Next Post

How to Setup Fail2ban on your Ubuntu 18.04 VPS Server or Dedicated Server

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet