Skip to content
Log In Sign Up info@muvcloud.com

How Apache Chooses a Site

Every request carries a Host header with the domain the visitor typed. Apache compares it with the ServerName and ServerAlias lines of the virtual hosts that listen on that IP address and port, and the first match wins. If nothing matches, Apache serves the first virtual host defined for that address and port, which acts as the default site. On Debian and Ubuntu that is 000-default.conf, because the files load in alphabetical order.

Step 1: Create the Site Folder

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R deploy:deploy /var/www/example.com
echo "example.com works" > /var/www/example.com/public_html/index.html

Replace deploy with the user who uploads the site, and run the last line as that user. Apache only needs to read the files: folders 755 and files 644 are enough.

Step 2: Write the Virtual Host on Debian and Ubuntu

Create /etc/apache2/sites-available/example.com.conf:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
  • ServerName: the main domain of the site.
  • ServerAlias: other names for the same site, separated by spaces. A wildcard such as *.example.com works.
  • DocumentRoot: the folder whose files are served.
  • Directory block: rules for that folder. -Indexes stops Apache from listing a folder that has no index file. AllowOverride None means .htaccess files are not read; see what is .htaccess if your application needs them.
  • ErrorLog and CustomLog: a pair of logs for this site only. APACHE_LOG_DIR is /var/log/apache2.

Step 3: Enable the Site and Test

sudo a2ensite example.com.conf
sudo apachectl configtest
sudo systemctl reload apache2

a2ensite creates a link in sites-enabled; a2dissite removes it. configtest should print Syntax OK. A warning that Apache could not reliably determine the server's fully qualified domain name is harmless; a global ServerName line, outside any virtual host, silences it. Reload rather than restart: running requests finish, and no visitor sees an error.

If you disable 000-default.conf, your first site in alphabetical order becomes the default for unknown names.

On AlmaLinux and Rocky Linux

There is no sites-available folder and no a2ensite. Put the same block in /etc/httpd/conf.d/example.com.conf and write the log paths in full:

ErrorLog /var/log/httpd/example.com-error_log
    CustomLog /var/log/httpd/example.com-access_log combined

Every file ending in .conf in that folder is loaded, so to disable a site, rename its file, for example to example.com.conf.off. Test and reload:

sudo apachectl configtest
sudo systemctl reload httpd

SELinux labels a folder created under /var/www correctly. Files moved in with mv from a home directory keep their old label, and Apache returns 403; fix that with sudo restorecon -Rv /var/www/example.com.

Test Before DNS Points to the Server

curl -H "Host: example.com" http://203.0.113.10/

Or add a line with the server's IP address and the domain to the hosts file on your own computer (/etc/hosts on Linux and macOS), open the site in a browser, and remove the line afterward.

List What Apache Loaded

sudo apache2ctl -S    # Debian and Ubuntu
sudo httpd -S         # AlmaLinux and Rocky Linux

On AlmaLinux and Rocky Linux, apachectl accepts only a few commands, such as configtest, so call httpd directly here. The output lists each address and port, the default server for it, and every name with the file and line where it is defined. It is the quickest way to find out why the wrong site answers.

Common Problems

  • The default page appears instead of your site: the site is not enabled, the name does not match ServerName or ServerAlias, or Apache was not reloaded.
  • 403 Forbidden: the Directory block lacks Require all granted, a parent folder is not readable by Apache, or an SELinux label is wrong.
  • HTTPS shows another site: a port 80 virtual host does not cover port 443. Certbot creates the matching port 443 block; see Certbot Apache.

Each site's own logs are explained in how to read Apache access and error logs.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations