Hosting & Domaining Forum

Domain Name Discussion => General Domain Discussion => Topic started by: towertech on Dec 14, 2022, 10:01 AM

Title: Bind two domain names to different folders
Post by: towertech on Dec 14, 2022, 10:01 AM
I have a VPS along with two domains. The Debian server has Apache, MySQL and PHP set up on it, using one IP address. I have complete access to the server including the root. Additionally, I can change the DNS records and add standard functionality through the registrar panel, affording me full control over everything.

Within the /var/www folder on the server exists several directories. My question is, how do I associate each domain with its respective folder on the web server in such a way that entering one domain will lead the user to one folder, while entering the other domain leads them elsewhere?
Title: Re: Bind two domains to different folders
Post by: RZA2008 on Dec 14, 2022, 10:46 AM
VirtualHosts is the term for this. It is configured in /etc/apache2/sites-available.

Listen 80
<VirtualHost *:80>
    dоcumentRoot "./var/www/domain1"
    ServerName www.domain.com
</VirtualHost>

<VirtualHost *:80>
    dоcumentRoot "/var/www/domain2"
    ServerName www.domain2.com
</VirtualHost>

In Apache, VirtualHosts allow hosting multiple websites on a single server. In the example above, we configure two different domains on the same IP address and port 80. The first virtual host is responsible for www.domain.com, while the second one is for www.domain2.com. Each virtual host has its own root directory and server name, which allows Apache to properly route requests to the corresponding sites.
Title: Re: Bind two domains to different folders
Post by: missveronica on Feb 18, 2023, 01:53 AM
Copy the default virtual host configuration file to create a new file for our domain:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Next, open the new virtual host file in a text editor with root privileges:

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

The new file will contain basic directives for a virtual host listening on port 80 for HTTP traffic. We need to modify some of these directives to customize the virtual host for our domain.

First, change the ServerAdmin directive to an email address that is available to the site administrator:

ServerAdmin admin@example.com

Add two directives. The ServerName directive specifies the domain name that should match this virtual host definition. The ServerAlias directive defines additional names, such as www, that should match as well:

ServerName example.com
ServerAlias www.example.com

Next, change the dоcumentRoot directive to point to the location of the root directory for this domain:

dоcumentRoot /var/www/example.com/public_html

After making these changes, the virtual host file should look similar to this:

/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    dоcumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

To enable the new virtual host, run the following command:

sudo a2ensite example.com.conf

Repeat the previous steps to configure another virtual host. Copy the virtual host file and edit it to include the proper directives for the second domain.

Finally, restart Apache to apply the changes:

sudo systemctl restart apache2

You can test the functionality of this process by temporarily changing the hosts file on your local computer.
Title: Re: Bind two domain names to different folders
Post by: untoneRox on Nov 23, 2023, 04:35 AM
When setting up virtual hosts in Apache, it's important to ensure that the ServerName and ServerAlias directives match the domain names you want to associate with each folder.

For example, if you have two domains, www.domain1.com and www.domain2.com, you would create two separate virtual host configuration files as follows:

For www.domain1.com:

<VirtualHost *:80>
    ServerName www.domain1.com
    ServerAlias domain1.com
    dоcumentRoot /var/www/domain1
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


For www.domain2.com:

<VirtualHost *:80>
    ServerName www.domain2.com
    ServerAlias domain2.com
    dоcumentRoot /var/www/domain2
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


After creating these virtual host files, you'll need to enable them by creating symbolic links to the sites-enabled directory as mentioned earlier:

sudo a2ensite domain1.conf
sudo a2ensite domain2.conf


Don't forget to restart Apache for the changes to take effect:

sudo systemctl restart apache2


With these configurations in place, visitors entering www.domain1.com will be directed to the /var/www/domain1 folder, while those entering www.domain2.com will be directed to the /var/www/domain2 folder on your web server.

-----------------

Let's delve into a more detailed explanation.

Step 1: Create Virtual Host Configuration Files
Navigate to the /etc/apache2/sites-available directory on your Debian server. Here, create a separate virtual host configuration file for each of your domains. You can use a text editor such as nano or vim to create these files. For example, to create a virtual host configuration file for www.domain1.com:

sudo nano /etc/apache2/sites-available/domain1.conf


Inside this file, you would define the virtual host configuration as follows:

<VirtualHost *:80>
    ServerAdmin webmaster@domain1.com
    ServerName domain1.com
    ServerAlias www.domain1.com
    dоcumentRoot /var/www/domain1
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


Similarly, you would create a virtual host configuration file for www.domain2.com:

sudo nano /etc/apache2/sites-available/domain2.conf


And inside this file:

<VirtualHost *:80>
    ServerAdmin webmaster@domain2.com
    ServerName domain2.com
    ServerAlias www.domain2.com
    dоcumentRoot /var/www/domain2
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


Step 2: Enable the Virtual Hosts
After creating the virtual host configuration files, enable them by creating symbolic links in the sites-enabled directory using the a2ensite command:

sudo a2ensite domain1.conf
sudo a2ensite domain2.conf


Step 3: Restart Apache
To apply the changes, restart the Apache web server with:

sudo systemctl restart apache2


By following these steps, visitors entering www.domain1.com will be directed to the /var/www/domain1 folder, and those entering www.domain2.com will be directed to the /var/www/domain2 folder on your web server.
Title: Re: Bind two domain names to different folders
Post by: atulpandey0021 on Sep 07, 2025, 09:20 AM
To set up virtual hosts in Apache, navigate to /etc/apache2/sites-available and create separate .conf files for each domain, like site1.conf and site2.conf. In each, define <VirtualHost *:80> blocks with ServerName matching your domains, setting dоcumentRoot to /var/www/folder1 and /var/www/folder2 respectively. Enable sites with a2ensite, reload Apache, and ensure DNS points A records to your VPS IP.
I'd say leverage mod_vhost_alias for simplicity in subdomain deployment. Remember, SSL via Let's Encrypt is crucial for prod environments.