How to setup sub-domains like blogspot

2019-03-16 18:20发布

问题:

What should do to setup a sub-domain for the users when they sign-up into my site.

What are the infrastructure required? I am using Linux servers.

回答1:

You can make a CNAME entry/ A Record in your DNS settings, for each subdomain

A CNAME record is a record in your Domain Management Settings that allows you to control a subdomain of your domain.

To automate it along with registration, you can write a script which is executed for each user, when s/he registers.

You can refer to this link, as well, for a step-by-step process for Apache:

How to setup subdomains in apache

(since you mentioned Linux, I assume it must be APache. Please mention if it is otherwise)

Alternate Solution

You can also refer to the wildcard solution, given by Alnitak, in the same thread. I find his is an easier way. :)



回答2:

You can either use a specific DNS (CNAME or A/AAAA) entry for each known subdomain, or a wild-card DNS entry that'll accept *.example.com:

$ORIGIN example.com
foo     IN A 12.34.6.78
bar     IN A 12.34.6.78

or

$ORIGIN example.com
*       IN A 12.34.6.78

The advantage of this latter is that no changes are required to either DNS or Apache configuration once the service is running. The disadvantage is that all such wildcard lookups must (by definition) end up returning the same IP address.

The Apache configuration will depend on your requirements, both for end-user control and security. Note that if the users have permission to run CGI scripts on the server then additional setup will be needed to ensure that that's done securely.

Depending on whether content is static or dynamic this will also affect your configuration:

  1. Use mod_vhost_alias to map individual virtual hosts into their individual directories on the server.

  2. If you really want, create a separate <VirtualHost> section for each known site, but then you'll have to restart Apache each time a new user signs up

  3. Use a single <VirtualHost> and then look at the hostname part of the requested URL (from the $SERVER_NAME environment variable) in the scripts that render the output to figure out which user's content to display.



回答3:

infrastructure includes access the the dns server to add a wildcard entry, and rewrite rules in Apache.

Try these answers:

  • How to let PHP to create subdomain automatically for each user?
  • How to make subdomain user accounts in a webapp

or this link:

  • http://jam.jrox.com/docs/index.php?article=76


回答4:

If your using Linux server's I'm assuming your using Apache as your webserver.

You'll have to setup proper DNS routing for the sub domain as well as a virtual host.

Virtual Hosts are fairly easy to setup but I'm not sure how easy it is to do them on the fly progmatically.

Most of the time it's as easy as editing your apache config file and adding the following:

Port 80
ServerName www.mydomain.com

NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/user-bob
ServerName bob.mydomain.com
...
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/user-sally
ServerName sally.mydomain.com
...
</VirtualHost> 

The VirtualHost Documention will probably of some use to you.



回答5:

Apache allows you to specify any number of 'sites' based on subdomains on a single server. Creating a new 'site definition' file with the appropriate subdomain information in it, along with proper DNS wildcards, will do what you want.

In other words, the process is like this:

  1. Setup wildcards so that *.mysite.com directs to the proper server.
  2. When a new user signs up, create the proper Apache site definition file - you'll probably have a base template that you put the right subdomain information into and save.
  3. Make Apache re-read its configuration.
  4. Profit.

IMPORTANT This is based on a Debian-style Apache configuration, where the config files are included in a directory, and the main configuration reads all the config files in that directory. This will simplify things a great deal, because adding/removing subdomains will mean adding/removing files, rather than editing a single file, and so the process will be much easier to automate.