Question

I have a website where I want users that sign up to get their own subdomain. This subdomain is virtual, and every subdomain uses the same web server files.

I use PHP and Apache, and I know about Virtual Hosts, but I'm wondering where I need to put the vhosts code. First, I don't have access to httpd.conf. Second, I want this to be done automatically upon registration.

I've read about virtual hosts, but didn't find anything that answers my questions. Is there anyone who can explain me how all this works together, or know where I can find my answers?

Was it helpful?

Solution

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 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
        </Directory>
</VirtualHost>

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

$_SERVER['HTTP_HOST']

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

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

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

OTHER TIPS

You will need 3 thing for this:

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 that you apache server has UseCanonicalName set to on (this is the default)

3. Grep the subdomain name with PHP:

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

(inspired by Janek's comment)

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

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

For instance

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

This assume that 192.168.1.4 is the IP of your server. Everything after that are alias's that the server can be called.

Then, as Janek suggested create a page that will echo $_SERVER['HTTP_HOST'] to see if it capturing 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 kind of implies that you don't have access to a lot of things) this will be difficult to pull off. The programming won't be - but the implementation will be.

Here's why:

  1. Apache by default will serve up virtual hosts. But you do need access to the server's conf directory (often located in /etc/httpd/conf.d/) so you can create the virtual host "include" file (IF the Apache configuration is setup to include it - most recent installs should be).

  2. You will need to be able to cycle Apache (restart). Without this the Virtual Host changes won't take affect.

  3. You will need to be able to change the DNS. You can always change your local /etc/hosts file - but in order for visitors to get to your site, you'll need to be able to push through a DNS change. (Which may instantaneous - or it may take 24 hours to propagate).

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

You will first of all need to setup the DNS server to resolve any subdomains to your main IP address, i.e. *.domain.com will have to resolve to your server's IP address. Then the web server needs to handle all incoming requests regardless of subdomain as well, invoking some PHP script. The PHP script can then simply figure out what domain was requested from the $_SERVER['HTTP_HOST'] header and act accordingly.

You're not actually "creating subdomains upon user registration".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top