Question

How do I set up wildcard *DOMAIN* on my droplet?

Posted February 16, 2020 6k views
ApacheDigitalOceanDNS

I’m working on a webapp where users can connect their profile to their domains, but how do I enable wildcard dns in digitalocean? Using vhosts or from the domain panel?
Eg: If the user profile is https://webapp.com/user
It will be shown on https://user.com/ once the user connects the domain.

I’m using
1) PHP
2) Apache
3) Ubuntu
4) MySQL

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.

×
2 answers

@Cybil Here is the approach I would take:

Update DNS to direct all subdomain requests to your Droplet

  1. Add your domain webapp.com without any subdomains to the DO Control Panel. add root domain
  2. Add an A Record entry to DNS where an asterisk * is the host name and your droplet is selected as the target so that all subdomains route Add * wildcard A-Record

Now the DNS record is directing all subdomain URL requests (user1.webapp.com, user2.webapp.com) to your Droplet. To ensure the root domain is also pointing to webapp.com, add an @ A-Record too.

Note: From our documentation:

(Use) A wildcard (*). To direct requests for a non-existent subdomain to a server or load balancer, enter *. However, if any kind of DNS record exists for a hostname, the wildcard will not apply; you will need to explicitly create an A record for it.
https://www.digitalocean.com/docs/networking/dns/how-to/manage-records/#a-records

Update Apache config on Droplet to map all subdomains to the right webapp route

  1. Enable the mod_vhost_alias extension on Apache with sudo a2enmod vhost_alias and then restart Apache with systemctl restart apache2
  2. Update your Apache config to add a VirtualHost block that maps subdomains to the proper PHP route
# ROOT DOMAIN CONFIG
<VirtualHost *:80>
    ServerName webapp.com
    DocumentRoot /path/to/webapp
</VirtualHost>

#DYNAMIC SUBDOMAIN CONFIG
<VirtualHost *:80>
    ServerAlias *.webapp.com
    VirtualDocumentRoot /path/to/webapp/users/%1.php
</VirtualHost>

Lastly restart apache again with systemctl restart apache2

  • I think there is a bit of confusion, I want to add domain

    Let me get this straight
    1) I’ve a web app on droplet with IP 12.123.34.3
    2) Users can create profile and connect with their custom DOMAIN
    eg. https://webapp.com/profile to https://userdomain.com/.
    3) I’ve figured out the dns redirection but I can figure out how to dynamically add a domain to my droplet.

    For better understanding you can checkout: about me
    Check out its premium domain adding feature

    • Ah I see!

      In that case, you just want to set up your Apache config to map these other domains to corresponding routes.

      For example, this kind of config will take the entire domain and add it as a URL variable to an internal URL.

      #DYNAMIC DOMAIN CONFIG
      <VirtualHost *:80>
          ServerAlias *
          VirtualDocumentRoot /path/to/webapp/profile?domain=%1
      </VirtualHost>
      

      You still need a way of knowing userdomain.com corresponds to user123 etc.. but I am assuming you’ve already built that into your app.

      • So I just need to change the main domain default config file, and change the server alias to ’*’, right?
        Is there anything I need to know other than that?

        Appreciate your help man!!

      • Not working :(
        So Im using a domain from freenom to connect it to my server

        What I did:
        1) Went to /etc/apache2/sites-available/ edited the default config file (000-default-le-ssl.conf)and (000-default-le.conf).
        2) Added ServerAlias * and restarted the apache server.

        But Im still getting at freenom: Error occured: Wild cards are not allowed!

        Please help

        • Error occured: Wild cards are not allowed!

          This error is rather odd. Could you provide a bit more information on exactly where this is happening and what you’re doing when it happens?

          • Thankyou for looking into it, the problem has been resolved.
            Appreciate your help:)

          • Also one more request can you tell me how do I display the webapp.com/userprofile to userdomain.com without iframe.
            I tried iframe but it is blocked my major adblockers and doesn’t work well.
            Do you know how about.me team does that??

            Thank you

          • @Cybil If you’re using the above configuration or something similar, userdomain.com should just be rendering a page within your webapp, so you should just be able to change what is being rendered there to show the profile, in the same way you render it at webapp.com/userprofile

          • I did not add the virtuadocumentroot, when I add the A record, I do get the homepage rendered to custom domain. But I want it to display user’s profile.
            So I’ve used dnsgetrecords() and the display according to that.

Hi,

Maybe you want to say https://users.webapp.com? Your question is to add subdomains for every user and enable https access?

Maybe you can turn to acme.sh and it will redirect to a GitHub repo with more information on setting up https. At there you can find what you want.

As for wildcard subdomains, just add *.webapp.com in your DNS console. That means, * is the record, and YOUR SERVER IP is the value.

Regards,
Shiroka

edited by MattIPv4
  • No I want it in such a way that users can connect their custom domains. Is there any other thing for that and not wild card domain?

    • It seems that you want to forward URL,if you just want to let them type in the domain and click enter,then redirect to the user page on your site.Use Apache forward method,it is easy to find on Google.About how to introduce to your customers to use,the best way is

      1) you set a subdomain points to one server only to forward,if your users are many
      2) let them point their domains a cname record to the domain in step 1
      3) (can be ignore) use let’s encrypt API to issue SSL cert for each domain.

      • But when I try to add my IP in A record for my domain, I get error. Wildcard Domain Not Allowed

        • maybe you thought wrongly.I mean that you can just add one subdmain,like alias.webapp.com points to one server that just used to forward your users’ domains.
          Then let your users point their domains a cname record to the alias.webapp.com

        • Sir,I saw you mentioned that your domain uses freenom DNS.I don’t know if they support wildcard resolve,but you talked about you can’t add that.So it is better to move your domain to a DNS provider that support you add wildcard record,like ns1,CloudFlare and DigitalOcean DNS.

          Regards

Submit an Answer