apachecpanelvirtualhostwhm

Promote particular user's Apache VirtualHost to default


As documented by Apache under Using Name-based Virtual Hosts:

If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.

As a consequence, the first listed virtual host is the default virtual host.

Currently, the template at /var/cpanel/templates/apache2_4/main.default generates such a default VirtualHost (per IP address) with DocumentRoot [% serverroot %]/htdocs.

We would instead like the VirtualHost that is generated for a particular user to be that default. What is the best/recommended way of accomplishing this?

My current thinking is to place the user's (compiled) VirtualHost directive in /usr/local/apache/conf/includes/pre_virtualhost_2.conf, but this obviously won't be managed by WHM should the user's configuration change.

Is there a better way?


Solution

  • Okay, so this is what I ended up doing:

    1. Copied /var/cpanel/templates/apache2_4/main.default to /var/cpanel/templates/apache2_4/main.local.

    2. Edited the newly created copy to remove the default VirtualHost directives (I inserted [%- IF 0 %] before line 344 and [% END -%] before line 385).

    3. Edited /var/cpanel/conf/apache/primary_virtual_hosts.conf so that the name of the desired default host was specified for the desired IP address(es).

    4. Ran /scripts/rebuildhttpdconf.

    5. Manually verified /usr/local/apache/conf/httpd.conf.

    6. Gracefully restarted Apache.