News
Faster Speeds: Bandwidth for vStack Servers in Kazakhstan Increased to 200 Mbps
JH
Joe Harris
October 28 2020
Updated June 1 2025

How to Set Up Nginx Virtual Hosts on Ubuntu 22.04: Step-by-Step Guide

Linux NGINX Ubuntu Web server

Using another OS?

Select the desired version or distribution.

Nginx is a powerful web server designed to efficiently manage multiple domains on a single server and IP address using virtual hosts. Virtual hosting allows you to host several websites on one server, each with its own domain name and configuration. In this step-by-step tutorial, we will guide you through the process of setting up Nginx virtual hosts to easily manage multiple websites on your server.

In the Serverspace you can create a server with already installed app "Nginx".

Nginx configuration files

First, you need to install the Nginx package.

apt install nginx

All configuration files for Nginx virtual hosts are stored in the /etc/nginx/sites-available/ folder. The best way is to create a separate file for each web site on the server. Let’s create the first configuration for domain-name.com.

nano /etc/nginx/sites-available/domain-name.com

Now insert this configuration there.

server {
listen 80; # Specify the listening port
listen [::]:80; # The same thing for IPv6
root /var/www/domain-name.com/html; # The path to the website files
index index.html index.htm; # Files to display if only the domain name is specified in the address
server_name domain-name.com; # Domain name of this site
location / {
try_files $uri $uri/ =404;
}
}

Save and close this file.
Create a folder for the website and place its files there.

mkdir -p /var/www/domain-name.com/html

And set permissions for the folder.

chmod -R 755 /var/www

Enabling the Nginx virtual host

You need to create a symbolic link to the configuration in the sites-enabled directory to enable the virtual host.

ln -s /etc/nginx/sites-available/domain-name.com /etc/nginx/sites-enabled/

Now check the configuration for errors.

nginx -t

And restart the service.

systemctl restart nginx

Now you have a working virtual host for a single domain. You can access it by domain name if the DNS server is configured correctly. Any number of domains can be added to the server in this way.

Disabling Nginx virtual hosts

To disable a virtual host, remove the symbolic link from the sites-enabled folder. To disable returning a standard web page when accessing the server's IP address, you can simply delete the link to the default configuration.

rm /etc/nginx/sites-enabled/default

Restart the service after that.

systemctl restart nginx

This way you can disable any configuration you need. And enable it by adding a symbolic link again, as we did earlier.

FAQ: Common Questions About Nginx Virtual Hosts Setup

  • Q: What are Nginx virtual hosts?
    A: Virtual hosts allow Nginx to serve multiple websites on a single server and IP address by using separate configuration files for each domain.
  • Q: How do I enable or disable a virtual host?
    A: Enable a virtual host by creating a symbolic link from /etc/nginx/sites-available/ to /etc/nginx/sites-enabled/. To disable it, simply remove the symbolic link and restart Nginx.
  • Q: Do I need to restart Nginx after changes?
    A: Yes, after enabling, disabling, or modifying virtual host configurations, always run nginx -t to check for errors, then restart Nginx with systemctl restart nginx to apply changes.
  • Q: Can I host unlimited domains on one server?
    A: Yes, by creating separate configuration files for each domain and enabling them via symbolic links, you can host as many domains as your server resources allow.
  • Q: What permissions should I set for website files?
    A: Set permissions to 755 for the website directory to ensure Nginx can read the files properly while maintaining security.
Vote:
4 out of 5
Аverage rating : 4.2
Rated by: 9
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.