When hosting multiple websites on a single web server, it's essential to set up virtual hosts. Virtual hosts allow you to assign different domain names to separate directories on your server, enabling you to serve multiple websites from a single machine. This tutorial will guide you through the process of setting up virtual hosts on your web server.

Prerequisites

Before setting up virtual hosts, ensure that you have the following components in place:

  1. A web server installed on your machine (e.g., Apache, Nginx).
  2. Domain names or subdomains pointing to your server's IP address.
  3. Access to your server's configuration files.

Step 1: Configuring DNS Records

To begin, you need to configure your DNS records to point the desired domain names or subdomains to your server's IP address. This step may vary depending on your domain registrar or DNS provider.

Reading more:

  1. Log in to your domain registrar or DNS provider's website.
  2. Locate the DNS management section for the domain you wish to set up as a virtual host.
  3. Add an "A" record for each domain or subdomain and enter your server's IP address.
  4. Save the changes and allow some time for the DNS records to propagate.

Step 2: Creating Directories

Next, you need to create directories on your server to store the website files for each virtual host. These directories should be located in a common root directory accessible by your web server.

  1. Connect to your server via SSH or use a file manager provided by your hosting provider.
  2. Navigate to the root directory where your website files are stored (e.g., /var/www/html).
  3. Create a directory for each virtual host using the desired domain name or subdomain (e.g., /var/www/html/example.com, /var/www/html/subdomain.example.com).

Step 3: Configuring Web Server

The configuration steps differ depending on the web server you are using. Below, we'll cover the setup for Apache and Nginx, two popular web servers.

Reading more:

Apache Configuration

  1. Locate the Apache configuration files directory. On Ubuntu, it is typically found at /etc/apache2/sites-available.
  2. Create a new configuration file for each virtual host using the .conf extension (e.g., example.com.conf, subdomain.example.com.conf).
  3. Open the configuration file in a text editor and add the following content:
    ServerName example.com
    DocumentRoot /var/www/html/example.com
    
    <Directory /var/www/html/example.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
  1. Save the file and exit the text editor.
  2. Enable the virtual host by creating a symbolic link to the sites-enabled directory:
  1. Restart Apache for the changes to take effect:

Nginx Configuration

  1. Locate the Nginx configuration files directory. On Ubuntu, it is typically found at /etc/nginx/sites-available.
  2. Create a new configuration file for each virtual host using the .conf extension (e.g., example.com.conf, subdomain.example.com.conf).
  3. Open the configuration file in a text editor and add the following content:
    listen 80;
    server_name example.com;
    root /var/www/html/example.com;
    
    location / {
        index index.html index.htm;
    }
    
    error_log /var/log/nginx/example.com_error.log;
    access_log /var/log/nginx/example.com_access.log;
}
  1. Save the file and exit the text editor.
  2. Enable the virtual host by creating a symbolic link to the sites-enabled directory:
  1. Restart Nginx for the changes to take effect:

Step 4: Testing

After configuring the virtual hosts, it's time to test whether they are functioning correctly.

  1. Open a web browser and enter the domain name or subdomain of one of your virtual hosts (e.g., http://example.com, http://subdomain.example.com).
  2. If you see the website content you placed in the corresponding directory during Step 2, congratulations! Your virtual host is set up correctly.
  3. Repeat the process for each virtual host to ensure they all work as intended.

Conclusion

Setting up virtual hosts on your web server allows you to efficiently host multiple websites on a single machine. By following the steps outlined in this tutorial, you can configure virtual hosts using popular web servers such as Apache or Nginx. Remember to configure DNS records to point your domain names or subdomains to your server's IP address, create directories for each virtual host, and modify the web server's configuration files accordingly. With virtual hosts properly set up, you can serve multiple websites reliably and efficiently.

Reading more:

Similar Articles: