Help with WHM hooks to automatically create subdomains in new cPanel accounts
Hello,
I have a server with a separate cPanel account using the domain "gabena.net" and a wildcard record to handle all subdomains automatically. I created a script that runs via a post-hook in WHM to create a subdomain like maindomain.gabena.net or maindomain.com.gabena.net or any random name with gabena.net for each new cPanel account.
Here’s the script:
#!/bin/bash
LOGFILE="/var/log/create_subdomain_hook.log"
# Read JSON payload
payload=$(cat)
# Extract username and domain
username=$(echo "$payload" | /usr/bin/jq -r '.data.user')
domain=$(echo "$payload" | /usr/bin/jq -r '.data.domain')
# Check if username and domain are present
if [ -z "$username" ] || [ -z "$domain" ]; then
echo "$(date '+%F %T') - ERROR: Missing username or domain in hook payload" >> "$LOGFILE"
exit 0
fi
# Subdomain name
subdomain="${domain}.gabena.net"
# Check if subdomain already exists
exists=$(/usr/local/cpanel/bin/uapi --user "$username" SubDomain listsubdomains | /usr/bin/jq -r '.data.subdomains[].subdomain' 2>/dev/null | grep -Fx "$subdomain")
if [ -n "$exists" ]; then
echo "$(date '+%F %T') - Subdomain $subdomain already exists for $username" >> "$LOGFILE"
exit 0
fi
# Create subdomain
create=$(/usr/local/cpanel/bin/uapi --user "$username" SubDomain addsubdomain domain="$domain" rootdomain="gabena.net" 2>&1)
# Log result
if echo "$create" | grep -q '"status":1'; then
echo "$(date '+%F %T') - SUCCESS: Created $subdomain for $username" >> "$LOGFILE"
else
echo "$(date '+%F %T') - ERROR: Failed to create $subdomain for $username - $create" >> "$LOGFILE"
fi
The hook works, but when it tries to create the subdomain, I get this error:
(XID pempbx) The domain “gabena.net” does not belong to “newcPanelusername”.
I’ve enabled the following options:
-
Remote Domains
-
Allow cPanel users to create subdomains across accounts
But the error persists.
What I need:
I want my domain gabena.net which is added to my separate cPanel account on the same server to be available for all other cPanel users. Once new cPanel account is created, my script should add subdomain (maindomain.gabena.net) to it without errors.
How can I configure this properly? Maybe I can somehow disable validation of ownership? Tweak settings didn't help.
Thanks for your help!
-
Hey hey! I did reply to the other thread you had with a few thoughts. Can you try those and report back?
0 -
Hello,
I enabled every possible option in Tweak Settings, but still receive the error that the domain doesn't belong to the cPanel user. It seems API can't see those Tweak Settings options.
Is there any other way to share access to that domain with other cPanel users?
0 -
Thanks for the additional details. Are you able to create these domains manually through the WHM >> Create a New Account interface, or does that also give the same error?
0
Please sign in to leave a comment.
Comments
3 comments