Ais..2025 11 19..2147..sub2

· eog's blog


πŸ‘€ User #

@ my php hoster and its cpanel i can generate subdomains i have ssh access but user rights can i script the generating of subdomains?


πŸ‘† ChatGPT #

Yes β€” you can automate creating subdomains in cPanel, but only if your hosting provider allows it via one of the following:

βœ… 1. Use cPanel’s API (UAPI)

cPanel provides an API you can call over HTTPS to create subdomains.

API endpoint bash Copy code POST /execute/SubDomain/addsubdomain

Example cURL command bash Copy code curl -u "cpanel_user:cpanel_password"
-H "Authorization: Basic $(printf '%s' "cpanel_user:cpanel_password" | base64)"
"https://yourdomain.com:2083/execute/SubDomain/addsubdomain?domain=sub&rootdomain=yourdomain.com&dir=public_html/sub"

Requirements

βœ” cPanel account βœ” Ability to use HTTPS from your script βœ” API access not blocked by your host

βœ… 2. Use SSH + cPanel API tokens (better than password)

In cPanel, go to:

cPanel β†’ Security β†’ API Tokens β†’ Create Token

Then use:

bash Copy code curl -H "Authorization: cpanel youruser:YOUR_API_TOKEN"
"https://yourdomain.com:2083/execute/SubDomain/addsubdomain?domain=test&rootdomain=yourdomain.com&dir=public_html/test"

This is the safest way.

🚫 3. What you CANNOT do

With only user-level SSH access, you cannot:

Modify Apache/Nginx configs directly

Create DNS zones manually

Edit system-wide domain settings

Those are root-only tasks. So your only option is cPanel’s API, because it works at the user level.

βœ… 4. Example Bash script to create subdomains automatically bash Copy code #!/bin/bash

API_TOKEN="YOUR_TOKEN" USER="cpaneluser" HOST="yourdomain.com"

SUB=$1 DOMAIN="yourdomain.com" DIR="public_html/$SUB"

curl -s -H "Authorization: cpanel $USER:$API_TOKEN"
"https://$HOST:2083/execute/SubDomain/addsubdomain?domain=$SUB&rootdomain=$DOMAIN&dir=$DIR"

Usage:

bash Copy code ./addsub.sh blog

❓ Want me to write:

βœ” a full script? βœ” a PHP version? βœ” a bulk-generator to create many subdomains at once?

Just tell me what you want automated.


πŸ‘€ User #

are 'execute' 'SubDomain' and 'addsubdomain' official commands? from cpanel?


πŸ‘† ChatGPT #

Good question. Yes β€” execute/SubDomain/addsubdomain is an official cPanel UAPI call. Here’s some more detail, and what the parts mean:

πŸ” What It Means

execute β€” This is part of the cPanel UAPI URL pattern. When you do /execute/..., you're calling a UAPI function over HTTP. cPanel+1

SubDomain β€” This is the module in cPanel’s UAPI that handles subdomains. cPanel & WHM Developer Portal+1

addsubdomain β€” This is the function within the SubDomain module that actually creates a new subdomain. cPanel & WHM Developer Portal

βœ… Official Documentation

The cPanel UAPI docs explicitly list SubDomain::addsubdomain as a function. cPanel & WHM Developer Portal

There is also an older (deprecated) API2 version: SubDomain::addsubdomain under cPanel API 2. cPanel & WHM Developer Portal+1

The parameters for UAPI include domain, rootdomain, dir, canoff, disallowdot, etc. cPanel & WHM Developer Portal

⚠️ Notes / Caveats

Even though you can create a subdomain via UAPI, SSL for that subdomain might not be auto-generated. Some users report needing additional steps for certificates. cPanel Support

Whether DNS resolves or not depends on where your DNS is managed: creating a subdomain via cPanel does not guarantee DNS propagation if your DNS is external. (Users often run into NXDOMAIN issues.) cPanel Support

The canoff parameter controls whether a canonical name (CNAME) is used in Apache config. cPanel & WHM Developer Portal

If you like, I can link you to the most up-to-date UAPI reference (for the cPanel version you're on) β€” do you want me to do that?


πŸ‘€ User #

if i would have root rights, where are subdomains confiured? in a file i guess. apache.ini, or in zones.conf, ... ? what are the paths? i have no clue about that? i know linux FS and FSH, i know bash and edided some .httaccess? but else? u know, i wanna script all staff, automate all. so if all where possible for me, what would a script running on, say debian, look like that generates 3 subdomains and activates SSL / TLS certs w/ LE.

f_gen_subdomain (){ echo "Usage: f_gen_subdomain sub1 [sub2 ... subN]" foo (){ ... ;} bar (){ ... ;}

}


πŸ‘† ChatGPT #

Below is a clear, root-level explanation of where subdomains are stored on a real Linux server (Debian/Ubuntu), how Apache + DNS + Let’s Encrypt handle them, and finally a full bash script that creates subdomains + vhosts + DNS zone entries + SSL certificates automatically.

This does NOT apply to cPanel (which hides all this under its internal system). This is how you’d do it on a real root server, using normal Linux layout.

βœ… Where Subdomains Are Configured (root-level server)

If you have full root access, a subdomain touches 3 components:

  1. DNS Zone File

Location on Debian (using BIND9):

swift
Copy code

`/etc/bind/zones/db.example.com
`

This file contains entries like:

nginx
Copy code

`sub1   IN  A   203.0.113.10
sub2   IN  A   203.0.113.10
sub3   IN  A   203.0.113.10
`

Other files:

zone declarations β†’ /etc/bind/named.conf.local

default options β†’ /etc/bind/named.conf.options

  1. Apache Virtual Hosts

On Debian:

bash
Copy code

`/etc/apache2/sites-available/sub1.example.com.conf
/etc/apache2/sites-available/sub2.example.com.conf
/etc/apache2/sites-available/sub3.example.com.conf
`

After enabling:

bash
Copy code

`/etc/apache2/sites-enabled/sub1.example.com.conf -> ../sites-available/sub1.example.com.conf
`

  1. Web root directories

For example:

bash
Copy code

`/var/www/sub1
/var/www/sub2
/var/www/sub3
`

  1. Let’s Encrypt certificates (via certbot)

Stored here:

swift
Copy code

`/etc/letsencrypt/live/sub1.example.com/
`

Certbot automatically updates Apache SSL configs.

πŸ’‘ What a full automation script requires

Your script must:

Edit DNS zone β†’ add A records

Reload BIND (systemctl reload bind9)

Create web root directories

Generate Apache vhosts

Enable them (a2ensite)

Reload Apache

Request Let’s Encrypt certificates

Apache reload again

🧨 FULL SCRIPT: create subdomains + Apache vhosts + SSL (Let’s Encrypt)

This script works on Debian 11/12, Apache2, BIND9, certbot.

Replace example.com and your server IP.

bash
Copy code

`#!/bin/bash

DOMAIN="example.com"
WWWROOT="/var/www"
APACHE_AVAIL="/etc/apache2/sites-available"
DNS_ZONE="/etc/bind/zones/db.$DOMAIN"
SERVER_IP="203.0.113.10"

f_gen_subdomain() {
    if [ "$#" -lt 1 ]; then
        echo "Usage: f_gen_subdomain sub1 [sub2 ... subN]"
        return 1
    fi

    for SUB in "$@"; do
        FQDN="$SUB.$DOMAIN"

        echo "=== Creating subdomain: $FQDN"

        # 1 β€” DNS: Add an A record
        if ! grep -q "^$SUB" "$DNS_ZONE"; then
            echo "$SUB    IN    A    $SERVER_IP" >> "$DNS_ZONE"
            echo "Added DNS record for $FQDN"
        else
            echo "DNS already exists for $FQDN"
        fi

        # 2 β€” Website directory
        WEBROOT="$WWWROOT/$SUB"
        mkdir -p "$WEBROOT"
        echo "<h1>$FQDN</h1>" > "$WEBROOT/index.html"

        # 3 β€” Apache vhost
        VHOST_CONF="$APACHE_AVAIL/$FQDN.conf"

        cat > "$VHOST_CONF" <<EOF
<VirtualHost *:80>
    ServerName $FQDN
    DocumentRoot $WEBROOT

    <Directory $WEBROOT>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog \${APACHE_LOG_DIR}/$FQDN-error.log
    CustomLog \${APACHE_LOG_DIR}/$FQDN-access.log combined
</VirtualHost>
EOF

        # 4 β€” Enable vhost
        a2ensite "$FQDN.conf" >/dev/null

        echo "Enabled Apache vhost for $FQDN"
    done

    echo "Reloading services..."
    systemctl reload bind9
    systemctl reload apache2

    echo "Requesting SSL certificates..."
    for SUB in "$@"; do
        FQDN="$SUB.$DOMAIN"
        certbot --apache -d "$FQDN" --non-interactive --agree-tos -m admin@$DOMAIN
    done

    systemctl reload apache2
    echo "All done!"
}
`

βœ” Example Usage

Create three subdomains:

bash
Copy code

`f_gen_subdomain blog api shop
`

Output:

python-repl
Copy code

`=== Creating subdomain: blog.example.com
Added DNS record...
Enabled vhost...
=== Creating subdomain: api.example.com
...
=== Creating subdomain: shop.example.com
...
Requesting SSL certificates...
DONE
`

πŸŽ‰ You now have a complete automation solution

If you want, I can also generate:

βœ… nginx version βœ… Cloudflare DNS API version βœ… version without BIND (external DNS) βœ… fully parameterized version βœ… version creating wildcard certificates (*.example.com) βœ… version with templated HTML folders

Just tell me what your setup looks like.

last updated: