Virtualhost entries gets deleted when apache httpd.conf is rebuilt

amit_roy

Member
Oct 19, 2012
7
0
1
cPanel Access Level
Root Administrator
Background:
We have been trying to get a wildcard SSL working on multiple sub domains on a single dedicated address. We have two sub domains next.my-personal-website.com and blog.my-personal-website.com

Part of our strategy has been to edit the httpd.conf and add the NameVirtualHost xx.xx.144.72:443 directive and the virtualhost entries for port 443 for the subdomains there. This works good if we just edit the httpd.conf, add the entries, save it and restart the apache.


The problem:
But if we add a new sub domain from cpanel or we run the

Code:
# /usr/local/cpanel/bin/apache_conf_distiller --update
# /scripts/rebuildhttpdconf
the virtualhost entries that we added manually are no more there in the newly generated httpd.conf file. Only the virtualhost entry for the main domain for port 443 that was there before we made edits to the httpd.conf is there(assuming we are not discussing virtualhost entries for port 80).

I understand we need to put the new virtualhost entries in some include files as mentioned here in the cpanel documentation. But am not sure where to.

So the question would be where do I put the NameVirtualHost xx.xx.144.72:443 directive and the two virtualhost directive for port 443, so that they are not overwritten when httpd.conf is rebuilt/regenerated later.

I really appreciate if somebody can help me out on this. Thanks.
 

amit_roy

Member
Oct 19, 2012
7
0
1
cPanel Access Level
Root Administrator
Thanks everybody. @Dalem "includename" can be any arbitrary name I assume?Or does it have a specific naming convention?

Also I wanted to point out I was browsing the posts in this forum and somewhere I found a post where it says the virtualhost entries are stored in YAML format in the /var/cpanel/userdata/USERNAME . I actually went there in our VPS and could see the domain and the subdomain files in YAML format there. But there there was another file for the main domain ending with _SSL my-personal-website.com_SSL . There were no such files(ending with _SSL) for sub domains. Any idea, if I can add it there too.

I am curious to know are these files generated in the /var/cpanel/userdata/? Are they generated when the httpd.conf is built? or these files are used to generate the httpd.conf?
Can we manually add an entry for the sub-domain there and then run the

# /usr/local/cpanel/bin/apache_conf_distiller --update
# /scripts/rebuildhttpdconf
 

Bestrafung

Active Member
Apr 2, 2012
42
0
56
cPanel Access Level
Root Administrator
Thanks everybody. @Dalem "includename" can be any arbitrary name I assume?Or does it have a specific naming convention?

Also I wanted to point out I was browsing the posts in this forum and somewhere I found a post where it says the virtualhost entries are stored in YAML format in the /var/cpanel/userdata/USERNAME . I actually went there in our VPS and could see the domain and the subdomain files in YAML format there. But there there was another file for the main domain ending with _SSL my-personal-website.com_SSL . There were no such files(ending with _SSL) for sub domains. Any idea, if I can add it there too.

I am curious to know are these files generated in the /var/cpanel/userdata/? Are they generated when the httpd.conf is built? or these files are used to generate the httpd.conf?
Can we manually add an entry for the sub-domain there and then run the
If I understand it correctly the include script searches with a wildcard and the "includename" could technically be anything. That said, the default behavior for Apache and other scripts is to look for a "vhost.conf".

In the post I linked it shows the usage for the scripts to include the vhosts, it generates the full httpd.conf including vhost includes. I only used it for my purposes setting up Django so it may be different for you but the process should be the same when dealing with the includes.

Example from post:
nano /usr/local/apache/conf/userdata/std/2/[username]/domain.com/vhost.conf

/scripts/verify_vhost_includes
/scripts/ensure_vhost_includes --user=[username]
 
Last edited:

Bestrafung

Active Member
Apr 2, 2012
42
0
56
cPanel Access Level
Root Administrator
Tried it. It says

<VirtualHost> cannot occur within <VirtualHost> section
Without seeing your files the obvious guess by that error message is that it looks something like this:
Code:
<VirtualHost>
<VirtualHost>
</VirtualHost>
</VirtualHost>
If using the Django guide as a reference, your include file would look like this:
Code:
<IfModule mod_alias.c>
Alias /robots.txt /home/username/sites/domain.com/domain/domain/media/robots.txt
Alias /site_media /home/username/sites/domain.com/domain/domain/media
Alias /admin_media /home/username/sites/domain.com/django/contrib/admin/media
</IfModule>

<IfModule mod_wsgi.c>
# See the link below for an introduction about this mod_wsgi config.
# http://groups.google.com/group/modwsgi/browse_thread/thread/60cb0ec3041ac1bc/2c547b701c4d74aa

WSGIScriptAlias / /home/username/public_html/domain.wsgi
WSGIDaemonProcess domain processes=7 threads=1 display-name=%{GROUP}
WSGIProcessGroup domain
WSGIApplicationGroup %{GLOBAL}
</IfModule>

# This fixes the broken ErrorDocument directive we inherit that breaks auth
# if we use a WSGI app.
ErrorDocument 401 "Authentication Error"
ErrorDocument 403 "Forbidden"
The generated httpd.conf would then contain this section:
Code:
# DO NOT EDIT. AUTOMATICALLY GENERATED.  IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES.

<VirtualHost xx.xx.xx.xx:80>
    ServerName domain.parent-domain.com
    ServerAlias www.domain.com www.domain.parent-domain.com domain.com
    DocumentRoot /home/username/sites/domain.com
    ServerAdmin [email protected]
    UseCanonicalName Off
    CustomLog /usr/local/apache/domlogs/domain.parent-domain.com combined
    CustomLog /usr/local/apache/domlogs/domain.parent-domain.com-bytes_log "%{%s}t %I .\n%{%s}t %O ."
    ## User username # Needed for Cpanel::ApacheConf
    <IfModule mod_suphp.c>
        suPHP_UserGroup username username
    </IfModule>
    <IfModule !mod_disable_suexec.c>
        <IfModule !mod_ruid2.c>
            SuexecUserGroup username username
        </IfModule>
    </IfModule>
    <IfModule mod_ruid2.c>
        RUidGid username username
    </IfModule>
    ScriptAlias /cgi-bin/ /home/username/sites/domain.com/cgi-bin/

    Include "/usr/local/apache/conf/userdata/std/2/username/domain.com/*.conf"

</VirtualHost>
The Include occurs at the end of the virtualhost section but still within it. Try removing any VirtualHost tags you have and write your code as if it's already inside the virtualhost tags. Hope this helps, I'm sure you've already got it by now. ;)
 

cPanelKenneth

cPanel Development
Staff member
Apr 7, 2006
4,607
80
458
cPanel Access Level
Root Administrator
Documentation:
* Changes Contained within a VirtualHost Directive
* Include Editor

To add a custom VirtualHost you should use the IncludeEditor within WHM. As noted in the first documentation link, and various posts in this thread, the includes you manually create in /usr/local/apache/conf/userdata are pulled directly into VirtualHost blocks. They are meant to modify VirtualHosts, not create new ones.


Editing the data in /var/cpanel/userdata is strongly discouraged. The contents are not documented, nor generally meant for human modification.
 

amit_roy

Member
Oct 19, 2012
7
0
1
cPanel Access Level
Root Administrator
We tried to follow the steps listed here ,:
- Created the respective directories
- Created a .conf file in the required directory
- Added the virtualhost entries there
- Ran
Code:
 /usr/local/cpanel/bin/apache_conf_distiller --update
and
Code:
/scripts/rebuildhttpdconf
and it gave us the virtualhost contained within another virtualhost error.

We then had a look at our httpd.conf file which usually has a lot of info. The following section at the top proved helpful for us
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Direct modifications to the Apache configuration file may be lost upon subsequent regeneration of the #
# configuration file. To have modifications retained, all modifications must be checked into the #
# configuration system by running: #
# /usr/local/cpanel/bin/apache_conf_distiller --update #
# To see if your changes will be conserved, regenerate the Apache configuration file by running: #
# /usr/local/cpanel/bin/build_apache_conf #
# and check the configuration file for your alterations. If your changes have been ignored, then they will #
# need to be added directly to their respective template files. #
# #
# It is also possible to add custom directives to the various "Include" files loaded by this httpd.conf #
# For detailed instructions on using Include files and the apache_conf_distiller with the new configuration #
# system refer to the documentation at: The Apache Web Server and cPanel & WHM #
# #
# This configuration file was built from the following templates: #
# /var/cpanel/templates/apache2/main.default #
# /var/cpanel/templates/apache2/main.local #
# /var/cpanel/templates/apache2/vhost.default #
# /var/cpanel/templates/apache2/vhost.local #
# /var/cpanel/templates/apache2/ssl_vhost.default #
# /var/cpanel/templates/apache2/ssl_vhost.local #
# #
# Templates with the '.local' extension will be preferred over templates with the '.default' extension. #
# The only template updated by the apache_conf_distiller is main.default. #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
We gave it a second shot which was as follows:

- We listened to our httpd.conf and created a ssl_vhost.local file at /var/cpanel/templates/apache2/

- Added the nameVirtualHost directive there for port 443

- Added the virtualhost entries for port 443 there

- checked if apache passed basic config test /usr/local/apache/bin/apachectl configtest

- We didnt use # /usr/local/apache/bin/apachectl restart , but rather restarted apache from WHM because the apache didnt restart when we used this command

- Test if apache restarted successfully by checking its status # /usr/local/apache/bin/apachectl status

- To have modifications retained, all modifications must be checked into configuration system by running
/usr/local/cpanel/bin/apache_conf_distiller –update

- To see if your changes will be conserved, regenerate the Apache configuration file by running
/usr/local/cpanel/bin/build_apache_conf instead of /scripts/rebuildhttpdconf

and finally we were able to get the virtualhost entries saved permanently. I hope this helps peoples. We are still making notes and looking into it to make sure we got everything correct.