Score:0

Is there a way to define an Apache 2 virtual host with a partial ServerName

cn flag

I have a tool that generates Apache 2 config blocks. I would like what it generates to be independent of whatever the actual ServerName is, but without needing to actually run on/parse the target machine's Apache config.

In other words, what I need is something like this:

<VirtualHost *>
   ServerName foobar    // <-- no top level domain
   ...
</VirtualHost>

It seems from reading the docs like the ServerName field must always be the fully qualified domain name, and thus inherently a vhost block cannot be written without knowing the FQDN of the target server. Is that true or is there some workaround?

Score:1
in flag

Yes ServerName must be real server identification, but can be ip-address as well. ServerAlias on the other hand can contain wildcards. Documentation mentions this.

But it also says that it is not required

If no ServerName is specified, the server attempts to deduce the client visible hostname by first asking the operating system for the system hostname, and if that fails, performing a reverse lookup on an IP address present on the system.

Score:0
ar flag
bob

Neither a ServerName nor a ServerAlias are required directives in a VirtualHost block.

You can simply omit them.

The manual explains when you do that:

If no ServerName is specified, the server attempts to deduce the client visible hostname by first asking the operating system for the system hostname, and if that fails, performing a reverse lookup on an IP address present on the system.

which may exactly be what you need.

Additionally, in practice those directives will also accept almost any string...
A single word, like localhost or test and example will work, as will things that look more like FQDN's but which are not actual working DNS names, like there.is.no.tld.named.serverfault. Using something like that may result in a startup warning in the error log (which you can avoid by adding that string to /etc/hosts) and may result in a situation where Apache httd can not distinguish your VirtualHost block from any others present and your settings won't take effect. https://httpd.apache.org/docs/2.4/vhosts/details.html

cn flag
Thanks, but that's not quite what I'm after. It's more like `ServerName foo.${HOSTNAME}` which would work, if the HOSTNAME env var were actually set for the Apache server. Unfortunately it seems modern systemd systems give very clean environments without this value, and there's no way to get Apache's idea of the host name into a variable. Just omitting the directives wouldn't work because the goal here is to have multiple snippets of config that are _independent_ of the machine's own hostname. And ideally that work with a standard Apache config.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.