Dynamically handling subdomains in Cyberpanel

I have installed cyberpanel (open litespeed) on my Ubuntu server and listed a site there. I want to handle subdomains dynamically.

here is my vhost.conf

docRoot $VH_ROOT/public_html vhDomain
$VH_NAME vhAliases *.$VH_NAME, www.$VH_NAME adminEmails user@gmail.com enableGzip
1 enableIpGeo 1

errorlog $VH_ROOT/logs/$VH_NAME.error_log { useServer
0 logLevel WARN rollingSize 10M }

accesslog $VH_ROOT/logs/$VH_NAME.access_log { useServer
0 logFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” logHeaders 5 rollingSize
10M keepDays 10 compressArchive 1 }

index { useServer 0 indexFiles
index.php, index.html }

scripthandler { add lsapi:aihow5388 php }

phpIniOverride { php_admin_value open_basedir “/tmp:$VH_ROOT” }

extprocessor aihow5388 { type lsapi address
UDS://tmp/lshttpd/aihow5388.sock maxConns 10 env
LSAPI_CHILDREN=10 initTimeout 600 retryTimeout
0 persistConn 1 pcKeepAliveTimeout 1 respBuffer 0 autoStart 1 path
/usr/local/lsws/lsphp82/bin/lsphp extUser aihow5388 extGroup aihow5388 memSoftLimit 2047M
memHardLimit 2047M procSoftLimit 400
procHardLimit 500 }

context /.well-known/acme-challenge { location
/usr/local/lsws/Example/html/.well-known/acme-challenge allowBrowse 1

rewrite { enable 0 } addDefaultCharset off }

rewrite { enable 1 autoLoadHtaccess 1 }

module cache { storagePath /usr/local/lsws/cachedata/$VH_NAME }

Moreover, I have created an A record pointing *.site.com to the IP address to handle subdomains.

But I am getting: 404 Not Found The resource requested could not be found on this server!

when I access subdomain like: sub.site.com

Note: my existing site works if I upgrade from Open LiteSpeed to LiteSpeed Enterprise.

How to fix this error?

To handle subdomains dynamically in OpenLiteSpeed and resolve the “404 Not Found” error for subdomains, there are a few key areas to check in your configuration. Here’s a step-by-step guide to troubleshoot and ensure everything is set up correctly:

1. A Record Configuration

Since you’ve already created an A record pointing *.site.com to your server’s IP, make sure it’s correctly propagated. You can use tools like nslookup or dig to verify this.

nslookup sub.site.com

2. Virtual Host Configuration

Your vhost.conf appears to be configured for dynamic subdomains, but ensure the following:

  • Document Root: The docRoot should point to the correct directory for your subdomains. If you want to handle subdomains dynamically, ensure that all subdomains can access the same root.
  • vhDomain and vhAliases: Ensure these are set correctly:
vhDomain site.com
vhAliases *.$VH_NAME, www.$VH_NAME
  • Rewrite Rules: Ensure rewrite rules are properly defined to handle dynamic subdomains if needed.

3. Check Directory Structure

Make sure that you have a public directory for your subdomains. If your main site is in public_html, you can set up a similar structure for subdomains or use the same directory.

4. File and Directory Permissions

Ensure that the web server user (often nobody or www-data) has the appropriate permissions to access the directories and files for your subdomains.

chown -R www-data:www-data /path/to/your/public_html
chmod -R 755 /path/to/your/public_html

5. Check Logs

Inspect the OpenLiteSpeed error logs to see if there are any clues about the “404 Not Found” error. You can typically find logs in:

/usr/local/lsws/logs/error.log

6. Restart OpenLiteSpeed

After making changes, ensure you restart OpenLiteSpeed to apply the new configurations:

/usr/local/lsws/bin/lswsctrl restart

7. Test Directly

If you still encounter issues, try accessing the subdomain directly via the server IP or a test file placed in the document root to rule out DNS issues:

http://your-server-ip/testfile.php

Example Subdomain Configuration

Here’s a sample configuration for dynamic subdomain handling:

docRoot $VH_ROOT/public_html
vhDomain site.com
vhAliases *.$VH_NAME, www.$VH_NAME

8. Consider Using Wildcard SSL

If you’re planning to use SSL on your subdomains, ensure you have a wildcard SSL certificate that covers all subdomains. This may require additional configuration.

9. Cache Settings

If caching is enabled, try clearing the cache or temporarily disabling it to see if it resolves the issue.

Conclusion

By following these steps, you should be able to troubleshoot the “404 Not Found” error for your subdomains. If you continue to have issues, please share any specific error messages from the logs or additional configurations for further assistance!