Question

How to dynamically create subdomains from the application

Currently am working on an application(LAMP) that requires creating dynamic subdomains. I have gotten the Apache part correctly. But is there a digital ocean API that can help manipulate subdomains or I can have a general way like the * in the Apache configurations?

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
May 11, 2023

Hi there,

If you already have a wild card virtual host added in your Apache configuration, what you could also do is to add a wild card A record for *.yourdomain.com to point to your Droplet’s IP address.

That way you will not have to add DNS records individually for all domains, but they will automatically be available and pointing to your Droplet’s IP address.

Then on your application level, you could decide how to handle the logic.

Alternatively, you could indeed use the DigitalOcean API and create A records for each sub domain name separately:

https://docs.digitalocean.com/reference/api/api-reference/#tag/Domain-Records

Let me know if you have any questions!

Best,

Bobby

Yes, DigitalOcean provides an API that allows you to manage your DNS records, including creating and deleting subdomains dynamically. To use the API, you will need to generate a Personal Access Token (PAT) from your DigitalOcean account.

Once you have your PAT, you can use the DigitalOcean API client (doctl) to interact with the API from your application. doctl is a command-line tool that allows you to manage your DigitalOcean resources, including domains and DNS records.

To create a subdomain dynamically, you can use the doctl compute domain records create command. Here’s an example of how to create a subdomain named “test.example.com”:

doctl compute domain records create example.com --record-type A --record-name test --record-data <IP Address>

In this command, you specify the domain name (“example.com”), the record type (A record), the record name (“test”), and the record data (the IP address of your server).

To delete a subdomain dynamically, you can use the doctl compute domain records delete command. Here’s an example of how to delete the “test.example.com” subdomain:

doctl compute domain records delete example.com test A

In this command, you specify the domain name (“example.com”), the record name (“test”), and the record type (A record).

Using the DigitalOcean API and doctl, you can create and delete subdomains dynamically from your application. This allows you to create a scalable and flexible architecture that can adapt to the needs of your users.

alexdo
Site Moderator
Site Moderator badge
May 12, 2023

Heya,

You can use a wildcard A record as mentioned by Bobby. Wildcard records are DNS records that direct requests for non-existent subdomains to a specified resource or IP address.

Link to our docs:

https://docs.digitalocean.com/glossary/wildcard-record/

Hope that this helps!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel