If you’ve set up Apache virtual hosts but still see the default “It works!” page, your configuration isn’t being applied correctly. This guide walks you through the exact steps to create and enable virtual hosts on a typical Apache installation (Ubuntu/Debian or CentOS/RHEL), so each domain or IP serves its own content. You’ll need root or sudo access to your server and a text editor.
What You’ll Need
- A server running Apache (version 2.4 or 2.2) with sudo/root access.
- Domain names (or subdomains) pointing to your server’s IP address, or you can test with localhost entries.
- Basic familiarity with the command line.
Step-by-Step
- Locate your Apache configuration directory. The exact path depends on your OS. On Debian/Ubuntu it’s usually
/etc/apache2/sites-available/and/etc/apache2/sites-enabled/. On CentOS/RHEL it’s/etc/httpd/conf.d/or/etc/httpd/conf/. Check withapachectl -Vto see the compiled paths (look forHTTPD_ROOTandSERVER_CONFIG_FILE). - Create a virtual host configuration file. Use a text editor to create a new file in the
sites-availabledirectory (Debian/Ubuntu) or inconf.d(CentOS). Name it after your domain, e.g.,example.com.conf. A basic config for a domain looks like this:<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>Adjust the paths and domain to your needs. For multiple sites, create one file per site.
- Create the document root directory. Make the directory you specified in
DocumentRootand add a test file (e.g.,index.html) with some content like “Hello from example.com”. Ensure the directory is readable by Apache (usually thewww-datauser on Debian/Ubuntu,apacheon CentOS). - Enable the virtual host. On Debian/Ubuntu, use
a2ensite example.com.confto enable it. On CentOS, the file inconf.dis automatically loaded, but you may need to restart Apache. If you’re using a singlehttpd.conffile, ensure theIncludedirective points to your conf.d directory. - Disable the default site (if needed). The default site (
000-default.confon Debian/Ubuntu) often catches all requests not matching another vhost. Disable it witha2dissite 000-default.confif you want your new vhost to be the default for port 80. On CentOS, the defaulthttpd.confmight have a<VirtualHost>block; comment it out or remove it. - Test the configuration. Run
apachectl configtest(orhttpd -t) to check for syntax errors. If it says “Syntax OK”, proceed to restart Apache. - Restart Apache. Use
sudo systemctl restart apache2(Debian/Ubuntu) orsudo systemctl restart httpd(CentOS). Alternatively,sudo service apache2 restartworks on older systems. - Test in your browser. Visit
http://example.com. If you see your test file content, the virtual host is working. If you still see the default page, check the troubleshooting section below.
Common Mistakes
- Forgetting to enable the site. On Debian/Ubuntu, creating a file in
sites-availabledoes nothing until you runa2ensite. - Wrong file permissions. The document root must be readable and executable by the Apache user. If you see a 403 Forbidden error, check permissions with
ls -ld /var/www/example.comand adjust withchmodorchown. - Not using
ServerNamecorrectly. If you have multiple vhosts, Apache uses theServerNameto match requests. If you don’t have a matchingServerName, the first defined vhost (often the default) will serve the request. - Editing the wrong config file. On CentOS, putting a vhost in
/etc/httpd/conf/httpd.confdirectly is fine, but make sure you don’t have conflicting directives elsewhere. - Not restarting Apache. Configuration changes only take effect after a reload or restart.
Tips
- Use
apachectl -Sto list all configured virtual hosts and see which one is the default. This helps debug name‑based vhost issues. - For local testing, edit your
/etc/hostsfile to map a domain likeexample.testto127.0.0.1so you can see the vhost without a real domain. - Keep each vhost in its own file with a clear name. This makes it easier to enable/disable and troubleshoot.
- Use
sudo apachectl -tafter any edit – it catches syntax errors before you restart and avoids downtime. - If you’re using SSL later, you’ll need to add a
<VirtualHost *:443>block and configure the certificate paths.
Troubleshooting
If you still see the default page after following the steps, try these in order:
- Check the vhost is actually loaded. Run
apachectl -Sand look for your domain in the output. If it’s not there, your file isn’t being included. Verify the file is in the right directory and has the correct extension (usually.conf). - Verify the default site is not overriding. On Debian/Ubuntu, if
000-default.confis still enabled and your vhost’sServerNamedoesn’t match the request, the default will serve. Disable it witha2dissite 000-default.confand restart. - Check the error log. The Apache error log (
/var/log/apache2/error.logor/var/log/httpd/error_log) will show any configuration errors or file permission issues. Look for lines mentioning your vhost or document root. - Test with curl. Use
curl -H "Host: example.com" http://localhostto see what Apache returns for that domain. If it returns the default page, your vhost isn’t matching; if it returns your content, the issue is DNS or browser caching. - Ensure the document root exists and has an index file. If the directory is empty or the index file isn’t readable, Apache may fall back to the default page. Create an
index.htmlwith some text and set permissions to755for directories and644for files. - Check for typos in
ServerName. A trailing dot or wrong subdomain can cause a mismatch. Use the exact domain that you type in the browser.
Frequently Asked Questions
Why do I still see the default page after enabling my vhost?
The most common cause is that the default site is still enabled and your vhost’s ServerName doesn’t match the request. Disable the default site or ensure your vhost is the first one defined for port 80.
Can I have multiple virtual hosts on the same IP?
Yes, Apache uses name-based virtual hosting. As long as each vhost has a unique ServerName (and optionally ServerAlias), Apache will route requests based on the Host header.
What’s the difference between sites-available and sites-enabled?
On Debian/Ubuntu, sites-available holds all vhost configuration files, and sites-enabled contains symlinks to the ones you want active. This lets you easily enable/disable sites without deleting files.
How do I set up a virtual host for a subdomain like blog.example.com?
Create a new vhost with ServerName blog.example.com and a separate DocumentRoot pointing to the subdomain’s files. The rest of the steps are the same.
Do I need to edit the main httpd.conf file?
Not necessarily. Most Apache installations include a directory like conf.d or sites-available that is automatically included. Placing your vhost there is cleaner and easier to manage.
Why does my vhost work with localhost but not with my domain?
If it works with localhost but not your domain, the issue is likely DNS – your domain isn’t resolving to your server’s IP. Check with dig example.com and ensure your domain’s A record points to the correct IP.
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
- Secure Boot Battlefield 6 ASUS: Fix & Enable Guide
- Best Tech Websites to Follow for Pros & Beginners
- Second Wind Quest Walkthrough: Where Winds Meet6.00 min/km
- Enshrouded Weapon Upgrade Guide: How to Improve Gear5.00 min/km
- Connect Phone to Controller for Cloud Gaming
- Python Search Highlighting: Snippets & Matches