Apache VirtualHosts

I know there aren’t many apache folks here (if any?) but figured I might try asking anyway…

When creating multiple sites on one host, instructions say to create a .conf file for each URL. e.g.
example.com.conf containing:

<VirtualHost *:80>
    DocumentRoot "/www/example.com"
    ServerName example.com
    SeverAlias www.example.com
</VirtualHost>

and
example.org.conf containing:

<VirtualHost *:80>
    DocumentRoot "/www/example.org"
    ServerName example.org
    ServerAlias www.example.org
</VirtualHost>

even though you can have the entries in a single file. (I think) like this:
example.conf containing:

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

<VirtualHost *:80>
    DocumentRoot "/www/example.org"
    ServerName example.org
    ServerAlias www.example.org
</VirtualHost>

What’s the advantage of having 2 files over putting both settings within a single
example.conf file?

1 Like

I find it useful to keep them in different files for a couple of reasons:

  • Temporary web sites can be easily added or torn down without touching my production sites. For example, I was playing with the Omeka content management system. By keeping it in a separate file, I don’t have to determine what belongs to Omeka (e.g., Directory directives) and what belongs to my production site.

  • You can use the “a2en…” and “a2dis…” tools to change the configuration. Using the example above, I could a2dissite omeka on my Ubuntu based system and it will remove the link in /etc/apache2/sites-enabled. I could put it back with a2ensite omeka later. (You also have a2enmod and a2dismod for modules, etc.)

The logical separation enforced by using different files, though, mostly protects me from my future self that has forgotten half of what I did. And I need all the help that I can get for that.

1 Like

Thank you @swampcat , that makes perfect sense!

1 Like

Do you know if it’s permissible to have a DNS entry that has a wildcard extension?
i.e. if I own example.com, example.net, example.org, example.ca, etc. can I have an A record that’s:
example.* instead of using CNAMEs for each possible suffix?

If not, then can that sort of wildcard be used in the Virtualhosts entry? .e.g

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

I don’t know of any way to have a wildcard. I’ll admit that I don’t have an in-depth knowledge of all the options.

My first thought would be to define a virtual host for each variant. One site will have all of the definitions for your site and all the others will have a Redirect directive to point to the primary. An alternative would be to use rewrite instead so the domain name doesn’t change in the visitor’s address bar.

This is one case where I would put all the virtual host definitions in one file since they are essentially a single unit that you would want to control as one and group together for ease of management.

1 Like

I feel kind of awkward as you’re interested in Apache, but I thought you might want to know that nginx will actually do what you want:

The parameter to server_name can be a full (exact) name, a wildcard, or a regular expression.

I really don’t mean to be rude and derail, I just thought it was worth your time!

No worries. I’m looking at Nginx as well.

From what I’m reading, it seems there cannot be wildcards in an A record, but there can on a CNAME, but only as the first part. .e.g.
*.example.com is ok, but not example.*

Similarly, in the VirtualHosts section, you can’t have any wildcards in the ServerName parameter, but you can have them anywhere in the ServerAlias, it seems.

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

Seems to work with example.com, example.net, test.example.com, etc, as long as there’s an A or CNAME record for each…