Creating a subdomain during user registration

I have a site on which I want users to subscribe in order to get their own subdomain. This subdomain is virtual, and each subdomain uses the same web server files.

I use PHP and Apache, and I know about virtual hosts, but I wonder where I need to put the vhosts code. Firstly, I do not have access to httpd.conf. Secondly, I want this to be done automatically during registration.

I read about virtual hosts, but did not find anything that answered my questions. Is there anyone who can explain to me how this all works together, or know where I can find the answers?

+4
source share
4 answers

Can you tell apache to read an extra .conf file? (traditionally you store your vhosts in httpd-vhosts.conf)

if so, add something like the following and restart the web server

NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot /abs/path/to/webroot ServerName domainname.com ServerAlias *.domainname.com <Directory /abs/path/to/webroot> AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> 

then in php you can see which subdomain the user is requesting by checking:

 $_SERVER['HTTP_HOST'] 

i.e. if the user requests http://user1.domainname.com/index.php

$ _ SERVER ['HTTP_HOST'] will have user1.domainname.com

you can explode ('.', $ _SERVER ['HTTP_HOST']) to check each segment, etc.

+7
source

(inspired by Janek's comment)

If your Apache instance is configured for * smoothing, then there is no need to create a virtual named host. You can fake it using PHP by evaluating $ _SERVER ['HTTP_HOST'].

To determine if your Apache instance will handle it, edit the local / etc / hosts file (or the Windows equivalent -% SystemRoot% \ system32 \ drivers \ etc \ hosts) so that the desired virtual name points to your server.

for instance

 # An example HOSTS file. 192.168.1.4 testserver testserver.com subdomain.testserver.com secondname.com 

Suppose 192.168.1.4 is the IP address of your server. Everything after that is an alias that can be called by the server.

Then, when Janek suggested creating a page that would be echo $_SERVER['HTTP_HOST'] to see if it captures the name correctly. If so, then all that is required is a DNS change, and Apache can remain unchanged.

Otherwise, without access to Apache.conf (this type implies that you do not have access to many things), it will be difficult to implement. Programming will not - but the implementation will be.

That's why:

  • Apache will host virtual hosts by default. But you need access to the conf conf directory of the server (often located in /etc/httpd/conf.d/) so that you can create a “include” file for the virtual host (if Apache is configured to enable it), there should be recent installations).

  • You will need to be able to start Apache (reboot). Without this, the virtual host changes will not be affected.

  • You will need to change the DNS. You can always change your local / etc / hosts file, but in order for visitors to get to your site, you will need to skip the DNS change. (Which may instantly - or may take 24 hours).

Scripts can certainly be executed (see Cpanel and WHM)

+2
source

You will need 3 pieces:

1. Set your DNS for * .yourDomain.com

2. Add the ServerAlias ​​directive to the apache configuration for this domain:

 ServerName www.yourDomain.com ServerAlias *.yourDomain.com yourDomain.com 

Also make sure the apache server UseCanonicalName set to on (this is the default value)

3. Grep subdomain name with PHP:

  $nameChunks = explode('.', $_SERVER['HTTP_HOST']); $subDomainName = $nameChunks[count($nameChunks) - 3]; 
+2
source

First of all, you need to configure the DNS server to resolve any subdomains to your primary IP address, i.e. *.domain.com will have to be resolved to your IP address of your server. Then the web server needs to process all incoming requests regardless of the subdomain, invoking some PHP script. The PHP script can then simply find out which domain was requested from the $_SERVER['HTTP_HOST'] header and act accordingly.

In fact, you do not create subdomains when registering a user.

+1
source

All Articles