Tip: Overriding the default Apache virtualhost

brinca

Registered
Sep 22, 2011
3
0
51
cPanel Access Level
Root Administrator
Since I've had this problem myself but cannot reply to old threads, I thought I'd post the answer here as it could save some time for someone looking for a similar solution.

Problem: How to edit the auto-generated default virtual host entry on httpd.conf?
Answer: You can't edit the default virtual host, but you can override it.. :)

EDIT: Actually you can (and probably should).. see Tristan's post below.
Note that you may still use this "hack" for temporary testing and such, if you wish to avoid getting your hands dirty on the shell.

The way to do this is using the pre virtual host includes to match your domain/ip in a more specific way than the catchall default entry:
  1. On WHM, go to Main >> Service Configuration >> Apache Configuration >> Include Editor
  2. On the "Pre VirtualHost Include" section, select the global configuration (i.e. "all versions")
  3. Enter your new vhost entry, making sure the ServerAlias directive includes all domains/ips you want to override
An example entry would be:


Code:
<VirtualHost 1.2.3.4:80>
    ServerName domain
    ServerAlias 1.2.3.4
    DocumentRoot /home/username/public_html
    ServerAdmin [email protected]
</VirtualHost>
If you want to "mimic" a specific user account, just copy that account's virtual host entry, and be sure to update it's respective ServerName/ServerAlias to whatever domains/ips you wish to bind it to.
Of course that, if you change any setting on that account you will have to manually update here as well.


Hope this helps ;)

Daniel Brinca
 
Last edited:

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
43
348
somewhere over the rainbow
cPanel Access Level
Root Administrator
Pre Virtual Host Includes are supposed to be before the VirtualHost entries. The better way to revise an existing entry would be to revise the /var/cpanel/userdata/username/domain.com file for that domain, then run:

Code:
/scripts/rebuildhttpdconf
/etc/init.d/httpd restart
The better way to add VirtualHost entries otherwise for non-existing users would be in /usr/local/apache/conf/userdata location which we have documentation on using at this location:

Changes Contained within a VirtualHost Directive
 

brinca

Registered
Sep 22, 2011
3
0
51
cPanel Access Level
Root Administrator
I think you misunderstood the problem.. I've seen numerous threads on this forum of people wanting to actually change the default catchall virtual host, in order to, for example, have the server's ip point to an existing user account.

My problem was that the server I was working on didn't have any domain configured yet, so in order to be able to preview the site before actually transferring the domain in, I had to use the server's ip.
Now, if I understood correctly, the root account is not allowed to have an ftp, and the default server ip points to /usr/local/apache/htdocs, which is outside the ftp root.

The best solution I found was to create an account, place all the site's files in there using it's ftp, and then override the default virtual host to have the ip point to that account's web root (as discussed on the tip).

I've looked into /var/cpanel/userdata/nobody (/usr/local/apache/conf/userdata doesn't exist on my installation) and the documentation link you posted, but that doesn't seem to apply to what I wanted and there are no examples to understand the format.
If you know of a better way to have an ip pointing to an existing account, please share an example or explanation on how to do it (I would rather use an elegant solution over my ugly hack).
If there is a blatant easier way to do this and what I'm proposing is just utterly stupid rubbish, please bear in mind that I'm a cPanel newbie.. ;)
 

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
43
348
somewhere over the rainbow
cPanel Access Level
Root Administrator
When you'd posted the initial thread, auto-generated default VirtualHost entry does not correspond to the main server IP catchall VirtualHost. All of the VirtualHost entries are auto-generated and use the default template. You may want to be far more clear on exactly what your guide is attempting to do.

As for another way to accomplish it for the main shared IP htdocs VirtualHost entry, you could revise these lines:

Code:
[%- FOREACH vh IN sharedips -%]
# DO NOT EDIT. AUTOMATICALLY GENERATED.  IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES.

<VirtualHost [% vh %]>
    ServerName [% servername %]
    DocumentRoot [% serverroot %]/htdocs
    ServerAdmin [% serveradmin %]
    [%- IF supported.mod_suphp %]
    <IfModule mod_suphp.c>
        suPHP_UserGroup nobody nobody
    </IfModule>
    [%- END %]
    [%- IF supported.mod_userdir && userdirprotect_enabled && defaultvhost.userdirprotect != '-1' %]
    UserDir disable
    [%- IF defaultvhost.userdirprotect != '' %]
    UserDir enabled [% defaultvhost.userdirprotect %]
    [%- END -%]
    [%- END %]
</VirtualHost>

[% END -%]

# Default vhost for unbound IPs

<VirtualHost *>
    ServerName [% servername %]
    DocumentRoot [% serverroot %]/htdocs
    ServerAdmin [% serveradmin %]
    [%- IF supported.mod_suphp %]
    <IfModule mod_suphp.c>
        suPHP_UserGroup nobody nobody
    </IfModule>
    [%- END %]
    [%- IF supported.mod_userdir && userdirprotect_enabled && defaultvhost.userdirprotect != '-1' %]
    UserDir disable
    [%- IF defaultvhost.userdirprotect != '' %]
    UserDir enabled [% defaultvhost.userdirprotect %]
    [%- END -%]
    [%- END %]
</VirtualHost>
They are found in /var/cpanel/templates/apache2/main.default and can be revised by copying to main.local and then being edited:

Code:
cp /var/cpanel/templates/apache2/main.default /var/cpanel/templates/apache2/main.local
vi /var/cpanel/templates/apache2/main.local
After editing, you'd rebuild Apache and restart it. I'd suggest making a backup copy first:

Code:
cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.bak110922
/scripts/rebuildhttpdconf
/etc/init.d/httpd restart
 

brinca

Registered
Sep 22, 2011
3
0
51
cPanel Access Level
Root Administrator
Thanks, I've tested it and it works fine, but while it is undoubtedly more elegant, it is also a lot more complicated (and dangerous if you mess up).

I would use your suggestion for permanent changes, and my hack for temporary stuff, such as to be able to preview a site through the ip while waiting for the domain to be transferred (once it is, the hack can be removed).

Thanks again Tristan :cool: