I've got a website where I would like customers that join obtain own subdomain. This subdomain is virtual, and each subdomain uses exactly the same web server files.

I personally use PHP and Apache, and that i learn about Virtual Hosts, but I am wondering where I have to place the vhosts code. First, I do not get access to httpd.conf. Second, I would like this to become done instantly upon registration.

I have find out about virtual hosts, but did not find something that solutions my questions. Can there be anybody who are able to explain me how all of this in concert with, or know where I'm able to find my solutions?

Do you know apache to see an additional .conf file? (typically you store your vhosts in httpd-vhosts.conf)

if that's the case, add something similar to the next and restart your webserver

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

then in php, you can observe which subdomain the consumer is asking for by checking:


ie. when the user demands http://user1.domainname.com/index.php

$_SERVER['HTTP_HOST'] may have user1.domainname.com

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

(inspired by Janek's comment)

In case your Apache instance is set up for * aliasing, then there's you don't need to produce a virtual named host - You are able to fake it with PHP by evaluating $_SERVER['HTTP_HOST'].

To find out in case your Apache instance will handle it, edit the local /etc/hosts file (or home windows equivalent - %SystemRoot%system32driversetchosts) to ensure that the preferred virtual title is pointing for your server.

For example

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

This think that may be the IP of the server. Everything next are alias's the server could be known as.

Then, as Janek recommended produce a page which will echo $_SERVER['HTTP_HOST'] to ascertain if it taking the title properly. If that's the case, then all that's needed is really a DNS change and Apache usually stays unchanged.

Otherwise without use of Apache.conf (this type of suggests you don't connect to numerous things) this is hard to accomplish. The programming will not be - however the implementation is going to be.

Here's why:

  1. Apache automatically assists up virtual hosts. But you will need accessibility server's conf directory (frequently situated in /etc/httpd/conf.d/) so that you can produce the virtual host "include" file (When the Apache configuration is setup to incorporate it - newest installs ought to be).

  2. You will have to have the ability to cycle Apache (restart). Without it the Virtual Host changes will not take affect.

  3. You will have to have the ability to alter the DNS. You could improve your local /etc/hosts file - but to ensure that site visitors to get at your website, you will need to have the ability to proceed a DNS change. (Which might immediate - or it might take 24 hrs to propagate).

The scripting certainly can be achieved (see Cpanel and WHM)

You'll need 3 factor with this:

1. Set your DNS for *.yourDomain.com

2. Add the ServerAlias directive towards the apache configuration with this domain:

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

Also make certain that you simply apache server has UseCanonicalName set to on (this is actually the default)

3. Grep the subdomain title with PHP:

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