How to Host Multiple Domains on Single IP with Apache Virtual Hosts


2 views

When hosting multiple domains on a single server with one IP address, Apache's Name-based Virtual Hosting is the most efficient solution. This allows serving different content for each domain while using the same IP address.

  • Apache web server installed (2.4+ recommended)
  • Root or sudo access to server configuration
  • DNS records properly configured for both domains

Here's how to set up virtual hosts in Apache:


# Main configuration file (typically httpd.conf or apache2.conf)
# Ensure this module is loaded
LoadModule vhost_alias_module modules/mod_vhost_alias.so

# Then create separate configuration files for each domain
# Example: /etc/apache2/sites-available/domain1.conf

    ServerName domain1.com
    ServerAlias www.domain1.com
    DocumentRoot /var/www/domain1
    ErrorLog ${APACHE_LOG_DIR}/domain1_error.log
    CustomLog ${APACHE_LOG_DIR}/domain1_access.log combined


# /etc/apache2/sites-available/domain2.conf

    ServerName domain2.com
    ServerAlias www.domain2.com
    DocumentRoot /var/www/domain2
    ErrorLog ${APACHE_LOG_DIR}/domain2_error.log
    CustomLog ${APACHE_LOG_DIR}/domain2_access.log combined

On Debian/Ubuntu systems:

sudo a2ensite domain1.conf
sudo a2ensite domain2.conf
sudo systemctl reload apache2

Both domains should have A records pointing to your server's IP:


domain1.com.    IN  A   192.0.2.1
domain2.com.    IN  A   192.0.2.1

After configuration:

  1. Verify DNS propagation: dig domain1.com +short
  2. Check Apache syntax: apachectl configtest
  3. Examine error logs if sites don't load properly

For more complex setups:


# SSL Configuration (HTTPS)

    ServerName domain1.com
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/key.pem
    # ... other SSL directives ...

When hosting multiple sites on one server:

  • Implement caching (mod_cache)
  • Consider using HTTP/2
  • Monitor resource usage with tools like htop

When hosting multiple websites on a single server, Apache's Virtual Hosts feature becomes essential. It allows you to serve different content for each domain while using the same IP address. This is particularly useful for shared hosting environments or developers running multiple projects on a local machine.

Before proceeding, ensure you have:

• Apache web server installed

• Root or sudo privileges

• Domain names properly configured with DNS records pointing to your server's IP

• Basic understanding of Apache configuration files

Here's how to set up name-based virtual hosts in Apache:


# 1. Create directory structure
sudo mkdir -p /var/www/domain1.com/public_html
sudo mkdir -p /var/www/domain2.com/public_html

# 2. Set proper permissions
sudo chown -R $USER:$USER /var/www/domain1.com/public_html
sudo chown -R $USER:$USER /var/www/domain2.com/public_html
sudo chmod -R 755 /var/www

# 3. Create sample index files
echo "<h1>Welcome to Domain1.com!</h1>" > /var/www/domain1.com/public_html/index.html
echo "<h1>Welcome to Domain2.com!</h1>" > /var/www/domain2.com/public_html/index.html

Create separate configuration files for each domain in Apache's sites-available directory:


# For domain1.com
<VirtualHost *:80>
    ServerAdmin admin@domain1.com
    ServerName domain1.com
    ServerAlias www.domain1.com
    DocumentRoot /var/www/domain1.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# For domain2.com
<VirtualHost *:80>
    ServerAdmin admin@domain2.com
    ServerName domain2.com
    ServerAlias www.domain2.com
    DocumentRoot /var/www/domain2.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

After creating the configuration files, enable them and restart Apache:


sudo a2ensite domain1.com.conf
sudo a2ensite domain2.com.conf
sudo systemctl restart apache2

You can test your configuration in two ways:

1. Edit your local hosts file for temporary testing:

127.0.0.1 domain1.com

127.0.0.1 domain2.com

2. Use curl to verify the responses:

curl -H "Host: domain1.com" http://your_server_ip

curl -H "Host: domain2.com" http://your_server_ip

For more complex setups, consider these additional configurations:


# SSL Configuration Example
<VirtualHost *:443>
    ServerName domain1.com
    DocumentRoot /var/www/domain1.com/public_html
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/key.pem
    SSLCertificateChainFile /path/to/chain.pem
</VirtualHost>

# Custom Error Pages
ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html

If you encounter problems:

403 Forbidden errors: Check directory permissions and ensure the DocumentRoot path is correct

Sites not loading: Verify that a2ensite was run and Apache was restarted

SSL issues: Check certificate paths and permissions

Configuration errors: Use apachectl configtest to check syntax