How to include subdomain specific configuration files

Mani Rai

Registered
Feb 22, 2017
2
1
3
Kathmandu, Nepal
cPanel Access Level
Root Administrator
I have been researching for days to include subdomain specific configuration files in subdomain virtual host configuration that is in main server configuration file. My overall goal is to add proxy pass and proxy pass reverse configuration that are different in each subdomain. For example: I want sub1.domain.com to act as proxy server of tomcat server that is running in localhost port 8080 and sub2.domain.com of another tomcat running in 8081. But cpanel generated httpd.conf file doesn't allow to add subdomain specific configuration files. For example: here's my httpd.conf file.

Code:
<VirtualHost *:80>
  ServerName sub1.domain.com
  ServerAlias www.sub1.domain.com
  ...
 Include "/usr/local/apache/conf/userdata/std/2/domain/*.conf"
</VirtualHost>
<VirtualHost *:80>
  ServerName sub2.domain.com
  ServerAlias www.sub2.domain.com
  ...
 Include "/usr/local/apache/conf/userdata/std/2/domain/*.conf"
</VirtualHost>
Notice the include directory is same for all subdomain's virtual host configuration. So if I add a subdomain specific proxy configuration file in above directory, that configuration is included in all virtualhost. I also couldn't achieve proxy configuration using .htaccess. So the only option I think I have is to edit vhost.default template which seems little complicate for me. Is there any other solution of this problem beside customizing vhost template?
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,268
463
  • Like
Reactions: Mani Rai

Zeddic

Registered
Feb 26, 2017
1
1
3
NA
cPanel Access Level
Root Administrator
I recently ran into this problem too and discovered that cpanel does support subdomain specific virtual host includes. However, the include line is not added to the httpd.conf file until after you create the subdomain specific include file and run the script to regenerate virtual hosts. The virtual host template file appears to do a directory existence check deciding to add the Include line.

The steps I did:

1: Create Subdomain Specific .conf Files in:

Code:
/etc/apache2/conf.d/userdata/ssl/2/username/sub.example.com/test.conf
/etc/apache2/conf.d/userdata/std/2/username/sub.example.com/test.conf
2: Regenerate Virtual Hosts
Code:
/scripts/ensure_vhost_includes --user=username
3: Find the new Includes in httpd.conf for just your subdomain:
Code:
<VirtualHost *:443>
  ServerName sub.example.com
  ServerAlias www.sub.example.com
  ...
 Include "/usr/local/apache/conf/userdata/std/2/username/*.conf"
 Include "/usr/local/apache/conf/userdata/std/2/username/sub.example.com/*.conf"
</VirtualHost>
 
  • Like
Reactions: Mani Rai

Mani Rai

Registered
Feb 22, 2017
2
1
3
Kathmandu, Nepal
cPanel Access Level
Root Administrator
I recently ran into this problem too and discovered that cpanel does support subdomain specific virtual host includes. However, the include line is not added to the httpd.conf file until after you create the subdomain specific include file and run the script to regenerate virtual hosts. The virtual host template file appears to do a directory existence check deciding to add the Include line.

The steps I did:

1: Create Subdomain Specific .conf Files in:

Code:
/etc/apache2/conf.d/userdata/ssl/2/username/sub.example.com/test.conf
/etc/apache2/conf.d/userdata/std/2/username/sub.example.com/test.conf
2: Regenerate Virtual Hosts
Code:
/scripts/ensure_vhost_includes --user=username
3: Find the new Includes in httpd.conf for just your subdomain:
Code:
<VirtualHost *:443>
  ServerName sub.example.com
  ServerAlias www.sub.example.com
  ...
 Include "/usr/local/apache/conf/userdata/std/2/username/*.conf"
 Include "/usr/local/apache/conf/userdata/std/2/username/sub.example.com/*.conf"
</VirtualHost>
It worked out quite well. Thanks a million Zeddic. I had so much trouble without this information and sorry for delayed response, I had very busy schedule to try it out.
 
  • Like
Reactions: cPanelMichael