To configure Apache virtual hosts on Ubuntu, create one file per site in /etc/apache2/sites-available/ with a VirtualHost block that sets ServerName and DocumentRoot, enable it with sudo a2ensite, check the syntax with sudo apache2ctl configtest, and reload Apache. Virtual hosts let one Apache server and one IP address serve many domains, each with its own folder, logs and settings.
This guide builds two sites, example.com and blog.example.com, shows how Apache picks a site for each request, and covers the mistakes that cause the wrong site to appear.
When a request arrives, Apache looks at the port and at the Host header the browser sent. It compares the host name with the ServerName and ServerAlias values of the virtual hosts defined for that port. The first virtual host defined for a port is the default, and it answers any request that matches no name.
This is why several domains can point to the same server IP and still show different sites.
Keeping the web root in a public subfolder leaves room for private files, such as configuration, outside the served folder.
Create /etc/apache2/sites-available/example.com.conf:
Create a second file, blog.example.com.conf, with the same layout but the blog name and folder. The lines mean:
| Directive | Purpose |
|---|---|
ServerName | The main host name for this site |
ServerAlias | Extra names that reach the same site |
DocumentRoot | The folder Apache serves |
Options -Indexes | Stops folder listings when no index file exists |
AllowOverride None | Ignores .htaccess files; use All only if the software needs them |
Require all granted | Allows access to the folder |
ErrorLog and CustomLog | Separate logs per site |
Without the Directory block with Require all granted, Apache returns 403 Forbidden for folders outside its default /var/www.
a2ensite creates a symlink in sites-enabled/, and a2dissite removes it. Disabling 000-default.conf stops the default Apache page from answering unknown names. If you want a catch-all, keep it but make sure it is listed first alphabetically, because Apache loads files in order and the first virtual host becomes the default.
configtest should print Syntax OK. Use reload rather than restart so active connections are not dropped.
Point DNS records for both names at the server's IP in production. To test earlier, send the Host header yourself:
Or use curl --resolve example.com:80:203.0.113.5 http://example.com/. You can also add a line to /etc/hosts on your own computer for local testing.
After HTTP works, add a certificate. With Certbot and the Apache plugin:
Certbot creates a second virtual host on port 443 and can add a redirect from HTTP. Make sure mod_ssl is enabled with sudo a2enmod ssl if the certificate step reports it missing.
ServerName does not match, or the site is not enabled. Check apache2ctl -S, which prints the virtual host list and which one is the default.Directory block is missing or the folder permissions block www-data from reading the path. Every parent folder needs execute permission.sites-available/ but did not enable it, or did not reload.sudo ss -tlnp | grep :80.ServerName is the primary host name of the site. ServerAlias adds other names, such as the www version, that serve the same content.
Yes. Name-based virtual hosts use the Host header, so any number of domains can share one address and port.
It is not required, but it is the standard practice. One file per site makes it easy to enable, disable and review each one.
The first virtual host for a port acts as the default. Order your files so the default one is the site you want to answer unmatched requests.
Create a folder and a VirtualHost file for each site, set ServerName, DocumentRoot and a Directory block, enable it with a2ensite, run apache2ctl configtest and reload. Use apache2ctl -S to see how Apache matches names, and add HTTPS with Certbot once HTTP works.