keepcalculator

06 Numbers and Powers

Apache Virtual Hosts: Fix Default Page

Card No. 06·31465 · Updated · Standard formula, shown below

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

  1. 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 with apachectl -V to see the compiled paths (look for HTTPD_ROOT and SERVER_CONFIG_FILE).
  2. Create a virtual host configuration file. Use a text editor to create a new file in the sites-available directory (Debian/Ubuntu) or in conf.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.

  3. Create the document root directory. Make the directory you specified in DocumentRoot and add a test file (e.g., index.html) with some content like “Hello from example.com”. Ensure the directory is readable by Apache (usually the www-data user on Debian/Ubuntu, apache on CentOS).
  4. Enable the virtual host. On Debian/Ubuntu, use a2ensite example.com.conf to enable it. On CentOS, the file in conf.d is automatically loaded, but you may need to restart Apache. If you’re using a single httpd.conf file, ensure the Include directive points to your conf.d directory.
  5. Disable the default site (if needed). The default site (000-default.conf on Debian/Ubuntu) often catches all requests not matching another vhost. Disable it with a2dissite 000-default.conf if you want your new vhost to be the default for port 80. On CentOS, the default httpd.conf might have a <VirtualHost> block; comment it out or remove it.
  6. Test the configuration. Run apachectl configtest (or httpd -t) to check for syntax errors. If it says “Syntax OK”, proceed to restart Apache.
  7. Restart Apache. Use sudo systemctl restart apache2 (Debian/Ubuntu) or sudo systemctl restart httpd (CentOS). Alternatively, sudo service apache2 restart works on older systems.
  8. 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-available does nothing until you run a2ensite.
  • 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.com and adjust with chmod or chown.
  • Not using ServerName correctly. If you have multiple vhosts, Apache uses the ServerName to match requests. If you don’t have a matching ServerName, 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.conf directly 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 -S to 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/hosts file to map a domain like example.test to 127.0.0.1 so 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 -t after 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:

  1. Check the vhost is actually loaded. Run apachectl -S and 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).
  2. Verify the default site is not overriding. On Debian/Ubuntu, if 000-default.conf is still enabled and your vhost’s ServerName doesn’t match the request, the default will serve. Disable it with a2dissite 000-default.conf and restart.
  3. Check the error log. The Apache error log (/var/log/apache2/error.log or /var/log/httpd/error_log) will show any configuration errors or file permission issues. Look for lines mentioning your vhost or document root.
  4. Test with curl. Use curl -H "Host: example.com" http://localhost to 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.
  5. 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.html with some text and set permissions to 755 for directories and 644 for files.
  6. 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

The whole Numbers and Powers drawer →