views:

314

answers:

3

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?

+5  A: 

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.

Janek
I'm wondering if I have to get a (virtual) dedicated server to do this. Am I right if I normally can't add virtual hosts to traditional web hosts, because of restricted Apache configuration access?
rebellion
No - Apache by default will serve up virtual hosts. But you do need access to the server: 1. You will need to be able to create a vhost include file (located in /etc/httpd/conf.d/vhostname.conf). 2. You will need to be able to cycle Apache (restart). 3. You will need to be able to change the DNS.
ChronoFish
Yeah, if you don't have access to your Apache configuration this is not really doable.Have you checked $_SERVER['HTTP_HOST'] when requesting your server with a subdomain? maybe you are already configured by chance..
Janek
@Janek: That's a great idea. I forgot about * aliasing.@Rebellion: Edit your local /etc/hosts file (or windows equivalent) so that the desired virtual name is pointing to your server. Then, as Janek suggested 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 and remain unchanged.
ChronoFish
Just had a chat with my host. They support wildcards, and they are hosted to the /www folder (public web server). That means I can use `$_SERVER['HTTP_HOST']` to get the sub domain.
rebellion
+2  A: 

(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)

ChronoFish
+1: Not sure what you mean with the /etc/hosts stuff, but if you can add a dns record like *.mywebsite.com CNAME mywebsite.com, you can test for the sub-domain using $_SERVER['HTTP_HOST'] and take it from there.
jeroen
Thank you for a thoroughly explanation! I've had a chat with my host, and they support DNS wildcards. As I understood it, I can get the subdomain with `$_SERVER['HTTP_HOST']`?
rebellion
A: 

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];
Jacco
He has indicated that he does not have access to the Apache configuration file - otherwise this is what would be done.
ChronoFish
I can use the PHP code for fething the sub domain, since my host support DNS wildcards. Wildcards are sent to the main www folder.
rebellion
Why is this answer downvoted ?
Jacco