Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How To Set Up Nginx Server Blocks on Ubuntu 20.04 - DesignLinux

Jul 10 2020
designlinux 0 Comments

A server block is an Nginx directive that defines settings for a specific domain, allowing you to run more than one website on a single server. For each website, you can set the site document root (the directory which contains the website files), create a separate security policy, use different SSL certificates, and much more.

This article describes how to set up Nginx server blocks on Ubuntu 20.04.

Prerequisites #

Ensure that you have met the following requirements before continuing:

  • Domain name pointing to your public server IP.
  • Nginx installed on your Ubuntu system.
  • You are logged in as root or user with sudo privileges.

In some articles, the term “Server Blocks” is referred to as a “Virtual host”. A virtual host is an Apache term.

Creating the Directory Structure #

The document root is the directory where the website files for a domain name are stored and served in response to requests. You can set the document root to any location you want. In this example, we will use the following directory structure:

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

Each domain hosted on the server will have its document root set to /var/www/<domain_name>/public_html.

Start by creating the root directory for the domain:

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

We’ll also create an index.html file inside the domain document root directory that will be shown when you visit the domain in your browser:

/var/www/example.com/public_html/index.html
<!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>

Since the commands above are executed as a sudo user, the newly created files and directories are owned by root. To avoid any permission issues change the ownership of the domain document root directory and all files within the directory to the Nginx user (www-data) :

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

Creating a Server Block #

On Ubuntu systems, Nginx server block configuration files are located in /etc/nginx/sites-available directory. They can be enabled by creating symbolic links to the /etc/nginx/sites-enabled directory, which Nginx read during the startup.

Open your text editor and create the following server block file:

/etc/nginx/sites-available/example.com
server {
    listen 80;

    server_name example.com www.example.com;

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

    index index.html;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}
  • server_name: The domains that should match for this server block configuration.
  • root: The directory from which Nginx will serve the domain files.
  • access_log, error_log: Specifies the location for log files.

The configuration file can be named anything you want, but usually, it is best to use the domain name.

To enable the new server block file, create a symbolic link from the file to the sites-enabled directory, which Nginx read during startup:

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

Test the Nginx configuration for correct syntax:

sudo nginx -t

If there are no errors, the output will look like this:

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

Restart the Nginx service for the changes to take effect:

sudo systemctl restart nginx

Finally, to verify that the server block is working as expected, open http://example.com in your browser of choice, and you will see something like this:

Conclusion #

We have shown you how to create Nginx server blocks and host multiple domains on a single Ubuntu server. You can repeat the steps outlined above and create additional server blocks for all your domains.

If you are facing any problem, feel free to leave a comment.

nginx ubuntu

Related

Tags: nginx, ubuntu

Secure Apache with Let’s Encrypt on Ubuntu 20.04

Prev Post

How to Install Apache Maven on Debian 10

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 1,253
  • 609,701

DesignLinux.com © All rights reserved

Go to mobile version