Creating My VirtualHosts to Apply Directives
Hi,
I have some infrastructure hostnames (server hostname SH, nameservers NS, and SMTP) that don't serve content and exist solely in DNS for the mentioned purposes. I wish to apply some directives to them only (mTLS, Redirect, HTTP Headers). I don't want to discuss directives here, but how to create VHs properly to avoid breaking any functionality.
I created these minimal VHs for each SMTP & NS:
<VirtualHost [serverIP]:80>
ServerName [hostname]
</VirtualHost>
<VirtualHost [serverIP]:443>
ServerName [hostname]
<IfModule ssl_module>
SSLEngine on
SSLCertificateFile /[path]/combined
</IfModule>
</VirtualHost>
I created this minimal VHs for SH:
<VirtualHost 127.0.0.1:80 [serverIP]:80 [dedicatedIP1]:80 [dedicatedIP2]:80 *:80>
ServerName [hostname]
</VirtualHost>
<VirtualHost 127.0.0.1:443 [serverIP]:443 [dedicatedIP1]:443 [dedicatedIP2]:443 *:443>
ServerName [hostname]
<IfModule ssl_module>
SSLEngine on
SSLCertificateFile /[path]/mycpanel.pem
</IfModule>
</VirtualHost>
I put these in pre_virtualhost_global.conf to take precedence over the ones defined in httpd.conf. Since they take precedence over default ones in httpd.conf, all directives from the previous VHs in httpd.conf are discarded. Should this worry me? Again, these VHs I've created won't serve any content; I use them to apply my own simple directives.
I also created VHs for each of my IPs for the same reason:
<VirtualHost [serverIP]:80>
ServerName [serverIP]
</VirtualHost>
<VirtualHost [serverIP]:443>
ServerName [serverIP]
<IfModule ssl_module>
SSLEngine on
SSLCertificateFile /[path]/mycpanel.pem
</IfModule>
</VirtualHost>
<VirtualHost [dedicatedIP]:80>
ServerName [dedicatedIP]
</VirtualHost>
<VirtualHost [dedicatedIP]:443>
ServerName [dedicatedIP]
<IfModule ssl_module>
SSLEngine on
SSLCertificateFile /[path]/combined
</IfModule>
</VirtualHost>
-
Hey there! If you just need a virtualhost for them to exist for **reasons** could you just create a subdomain through cPanel? That would create the hostname and then you could customize the individual vhosts using the details here in a way that would only affect the individual domains:
https://docs.cpanel.net/ea4/apache/modify-apache-virtual-hosts-with-include-files/
0 -
I can't; I need to create separate VHs for those subdomains because the settings I apply to them are separate from those of their apex domain. The only way to do that is to create them myself in the pre_virtualhost_global.conf.
0 -
If you create a subdomain it gets an separate vhost entry with a separate customization area. Here's one I created just now to confirm:
# To customize this VirtualHost use an include file at the following location
# Include "/etc/apache2/conf.d/userdata/std/2_4/username/sub.domain.com/*.conf"Would that not work since it's completely separate from the main domain.com?
0 -
OK, my mistake, you're right, but I still need to create the one for the server hostname and NSs. Plus, sometimes I need to modify just the ServerName domain without its ServerAlias domains.
0 -
Plus, I need them to be able to override the default ones.
0 -
There's no reason you can't create a subdomain for the server's hostname - you just may need to remove existing DNS entries first if they are hosted locally.
If you want to do it fully manual like you outlined I don't see why it shouldn't work. The only conflict you'd run into is if you forget in the future and try to make another change related to one of those domain names, but that would give you some type of "already exists" error during the Apache rebuild process.
0 -
Manual works perfectly, I just need an experienced eye to look at all of this and see if something can be improved or prevented.
For example, is it a good tactic to make SH a default for all IPs:
<VirtualHost 127.0.0.1:80 [serverIP]:80 [dedicatedIP1]:80 [dedicatedIP2]:80 *:80>
<VirtualHost 127.0.0.1:443 [serverIP]:443 [dedicatedIP1]:443 [dedicatedIP2]:443 *:443>0 -
Sure, I don't see any issues with that.
0
Please sign in to leave a comment.
Comments
8 comments