This guide shows you how to host several websites on a single Apache server using name-based virtual hosts. You’ll learn how to configure Apache so that it serves the correct content based on the domain name in the request, allowing you to run multiple sites from one IP address. This is the standard, efficient way to host multiple domains on a single server.
What You’ll Need
- A server running Apache (version 2.4 or later is recommended).
- Root or sudo access to the server.
- Your domain names already pointing to the server’s IP address (via DNS A records).
- Separate document roots (folders) for each website’s files.
Step-by-Step
- Create directory structure. For each domain, create a directory that will hold that website’s files. A common pattern is
/var/www/example.com/public_html. Run:sudo mkdir -p /var/www/example.com/public_htmland repeat for each domain. - Add a test file. In each document root, create a simple
index.htmlfile with a unique message (e.g., “Welcome to example.com!”) so you can verify which site is being served later. - Locate the Apache config directory. On Debian/Ubuntu, the main config is at
/etc/apache2/apache2.confand virtual host files are in/etc/apache2/sites-available/. On RHEL/CentOS/Fedora, look in/etc/httpd/conf.d/. - Create a virtual host file (Debian/Ubuntu). Create a new file for your first domain:
sudo nano /etc/apache2/sites-available/example.com.conf. Add a<VirtualHost *:80>block. - Write the basic virtual host configuration. Inside the file, add:
<VirtualHost *:80> ServerAdmin webmaster@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>Replace
example.comwith your actual domain and adjust the paths. - Create virtual host files for each additional domain. Repeat steps 4 and 5 for each domain you want to host. Use a separate file per domain (e.g.,
example.net.conf). - Enable the virtual hosts (Debian/Ubuntu). Use
sudo a2ensite example.com.confand repeat for each site file. Then disable the default site withsudo a2dissite 000-default.confif you don’t want it. - Test the configuration. Run
sudo apache2ctl configtest(orsudo httpd -ton RHEL). You should seeSyntax OK. - Restart Apache. Apply the changes with
sudo systemctl restart apache2(Debian/Ubuntu) orsudo systemctl restart httpd(RHEL). - Verify each site. Visit each domain in a web browser. You should see the test file you created for that domain. If not, check the troubleshooting section below.
Common Mistakes
- Forgetting ServerAlias. If you don’t include
www.example.com, visitors who type the www version will see the wrong site or the default site. - Duplicate ServerName. Make sure each virtual host has a unique
ServerName. Duplicates cause unpredictable behavior. - Wrong file permissions. Apache needs read access to the document root. If you get a 403 Forbidden error, check permissions with
sudo chmod -R 755 /var/www/. - Not disabling the default site. If the default site remains enabled, it will catch requests that don’t match any ServerName, often showing a generic page.
- Editing the wrong config file. On Debian/Ubuntu, editing
apache2.confdirectly for virtual hosts is not the intended way; use thesites-availabledirectory.
Tips
- Use separate log files. Give each virtual host its own
ErrorLogandCustomLog(e.g.,example.com-error.log). This makes debugging much easier. - Start with a test file. Always verify with a unique HTML file before deploying real content to avoid confusion.
- Consider SSL. For HTTPS, you’ll need a separate
<VirtualHost *:443>block for each domain, plus a valid SSL certificate. Services like Let’s Encrypt can automate this. - Use a consistent naming convention. Name your config files after the domain (e.g.,
example.com.conf) so you can easily identify them. - Check DNS propagation. If a site doesn’t load, ensure the domain’s DNS is actually pointing to your server’s IP.
Troubleshooting
- Site shows the default Apache page. This usually means the virtual host wasn’t enabled or the ServerName doesn’t match. Run
sudo apache2ctl -Sto list all virtual hosts and check their names. - Getting a 403 Forbidden error. Check that the document root exists and Apache has read permissions. Also ensure the directory has an
index.htmlorindex.phpfile. - All domains show the same site. This indicates the first virtual host is catching all requests. Make sure each
ServerNameis unique and that you’ve restarted Apache after enabling the sites. - Configuration test fails. Read the error message carefully. Common issues include a missing closing tag or a typo in the file path. Double-check the syntax.
- Changes not taking effect. Always run
sudo systemctl reload apache2(or restart) after modifying any config file. A reload is less disruptive and picks up changes.
Frequently Asked Questions
Can I host hundreds of domains on one server?
Yes, Apache can handle many virtual hosts. Each one is just a config block. However, performance depends on your server’s resources and traffic. For very high traffic, you might need to optimize or use a reverse proxy.
Do I need a separate IP address for each domain?
No. Name-based virtual hosting allows you to serve multiple domains from a single IP address. This is the standard method for most shared hosting.
What about HTTPS/SSL for multiple domains?
You need a separate SSL certificate for each domain. You can configure a virtual host on port 443 for each domain, and use tools like Certbot to automatically obtain and renew certificates.
How do I add a subdomain like blog.example.com?
Create a new virtual host with ServerName blog.example.com and point its DNS record to your server. The process is identical to adding a top-level domain.
What is the difference between name-based and IP-based virtual hosting?
Name-based uses the domain name in the HTTP request to choose the site. IP-based requires a separate IP address for each site. Name-based is more common and efficient.
Why is my site showing a 404 error?
Check that the DocumentRoot path is correct and that the file you’re requesting actually exists in that directory. Also ensure the virtual host is enabled and Apache was reloaded.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Business partnership due diligence checklist what to verify before you sign?
- Does the FCC Protect Internet Privacy?
- Disaster Robbers Codes? Meaning & Origin
- Repair a Disk with Mac Disk Utility
- Avoid Vendor Lock-In: Cloud Provider Strategy
- Secure GitHub Accounts & Repos Against Malicious Code