Add Multiple Domains (Virtual Hosts) on Apache Ubuntu
Introduction
Apache is a popular web server that can host multiple websites on a single server. This can be achieved using Apache Virtual Hosts, where each domain can have its own document root and configuration.
For this example, we will configure two domains:
domain1.tlddomain2.tld
Each domain will point to a separate document root.
Prerequisites
Before proceeding, make sure you have:
- An Ubuntu server
- Apache Web Server installed
- Root or sudo access
- Two domains pointing to the server IP address
- Basic knowledge of Linux commands
If Apache is not installed, refer to:
Install Apache on Ubuntu
Install Apache with php-fpm on Ubuntu 16
How To Install the Apache Web Server on Ubuntu 18.04
How to install/compile Apache with PHP via source installation
Implementation
Step 1: Create Document Root Directories
Create separate directories for both domains:
mkdir-p /var/www/domain1.tldmkdir-p /var/www/domain2.tld
Change the ownership to the Apache web server user:
chown-R www-data:www-data /var/www/domain1.tldchown-R www-data:www-data /var/www/domain2.tld
You can then upload the respective website files into each directory.
Step 2: Create Virtual Host Configuration
Create a separate Apache Virtual Host configuration for each domain.
cp /etc/apache2/sites-available/000-default.conf \/etc/apache2/sites-available/domain1.tld.confcp /etc/apache2/sites-available/000-default.conf \/etc/apache2/sites-available/domain2.tld.conf
Step 3: Configure Domain 1
Open the configuration file:
vi /etc/apache2/sites-available/domain1.tld.conf
Configure the Virtual Host as follows:
<VirtualHost *:80>
ServerAdmin admin@domain1.tld
ServerName domain1.tld
ServerAlias www.domain1.tld
DocumentRoot /var/www/domain1.tld
</VirtualHost>
Here:
ServerNamespecifies the primary domain.ServerAliasallows additional names such aswww.domain1.tld.DocumentRootspecifies where the website files are stored.
Step 4: Configure Domain 2
Open the second configuration file:
vi /etc/apache2/sites-available/domain2.tld.conf
Add:
<VirtualHost *:80>
ServerAdmin admin@domain2.tld
ServerName domain2.tld
ServerAlias www.domain2.tld
DocumentRoot /var/www/domain2.tld
</VirtualHost>
Now each domain has its own Virtual Host and document root.
Step 5: Enable the Virtual Hosts
Enable both websites using a2ensite:
a2ensite domain1.tld.confa2ensite domain2.tld.conf
You can disable the default Apache site if it is not required:
a2dissite 000-default.conf
Step 6: Check Apache Configuration
Before restarting Apache, always check the configuration for syntax errors:
apachectl -t
If everything is correct, you should see:
Syntax OK
Step 7: Restart Apache
Restart Apache to apply the configuration:
systemctl restart apache2
Alternatively:
service apache2 restart
Step 8: Test the Domains
If the DNS records are already configured, access the domains from your browser:
http://domain1.tldhttp://domain2.tld
Apache will identify the requested domain and serve the corresponding website.
The traffic flow will look like:
Internet
|
Apache Server
|
+----------+----------+
| |
domain1.tld domain2.tld
| |
/var/www/domain1.tld /var/www/domain2.tld
Testing Before DNS Configuration
If the domains are not yet pointing to the server, you can test the Virtual Host configuration using the local /etc/hosts file.
On a Linux or macOS system, open:
sudovi /etc/hosts
Add:
xx.xx.xx.xx domain1.tldxx.xx.xx.xx domain2.tld
Replace xx.xx.xx.xx with the IP address of your Apache server.
You can then access the domains from your browser and verify that each domain displays its respective website.
Verification
You can also check the Virtual Hosts recognized by Apache:
apachectl -S
This command displays the configured Virtual Hosts and helps identify which domain is associated with which configuration file.
For example:
*:80
domain1.tld (/etc/apache2/sites-enabled/domain1.tld.conf:1)
domain2.tld (/etc/apache2/sites-enabled/domain2.tld.conf:1)
This is particularly useful when troubleshooting multiple Virtual Host configurations.
Common Issues
Domain Shows the Wrong Website
Check the following:
ServerNameServerAliasDocumentRoot- DNS records
- Enabled Virtual Host configurations
You can use:
apachectl -S
to identify which Virtual Host Apache is using.
Apache Configuration Error
Run:
apachectl -t
Check the reported configuration file and line number, correct the issue, and test again.
Domain Is Not Reachable
Verify that the domain’s DNS record points to the correct server IP:
domain1.tld → Server IPdomain2.tld → Server IP
Also make sure HTTP port 80 is allowed through the server firewall or cloud security rules.
Conclusion
Apache Virtual Hosts make it possible to host multiple domains on a single Ubuntu server while keeping each website’s files and configuration separate.
The basic process is:
- Create a document root for each domain.
- Create a Virtual Host configuration for each domain.
- Configure
ServerName,ServerAlias, andDocumentRoot. - Enable the Virtual Hosts.
- Test the Apache configuration.
- Restart Apache.
- Verify the domains.
