Setting up a sub-domain on a Linux box

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Setting up a sub-domain on a Linux box

Post by impulse() »

Does this require the use of a DNS server on the box you're hosting a sub-domain? I understand the part of creating a new A record on the DNS record but what actions do I need to take on the server that's hosting the sub-domain?

Regards,
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Weird...I was just about to ask this same question...

I want to setup sub-domains on localhost...
Not sure how that would work :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Simply edit your /etc/hosts file..
127.0.0.1 localhost.localdomain localhost CWBE-VM005 mysite.localhost
And then add virtualhosts to your apche configuration...

(I'm pretty sure i've posted a couple of examples in the apache-webserver forum at phpdn).



If you want to make sure other people can access the new domains too, yes, you'll have to add an A or CNAME record to your dns configuration so that blah.example.com points to the ip (or hostname) where the webserver is running....
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

(Not referring to local domains via /etc/hosts here)

You just need to create new A records at the DNS end of things. The have those subdomains bring up different web content over HTTP you need to create NameBasedVirtualHost's in Apache or the equivalent in your web server. Basically the sub-domain will point to the same IP so is effectively the same as far as TCP/IP goes, but HTTP send the name of the domain along with the request, so Apache can see the domain name that was requested and deliver content accordingly :)

EDIT | Sorry tim, I didn't read your post fully :oops:
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

timvw wrote:Simply edit your /etc/hosts file..
127.0.0.1 localhost.localdomain localhost CWBE-VM005 mysite.localhost
I don't need anyone from the outside accessing it (yet anyways) but here is my current hosts file:
127.0.0.1 localhost
127.0.1.1 pcspectra-dev

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
what would I add to this file if I wanted to add a subdomain forums.localhost or forums.pcspectra-dev???

Then I have to add an entry for apache which I assume is what maps the directory to the subdomain name?

You wouldn't care to show me explicity what I would need to edit inorder to add a sub-domain (forums) would you? :)

Cheers :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Either:

Code: Select all

127.0.0.1 localhost  forums.localhost
127.0.1.1 pcspectra-dev

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
Or

Code: Select all

127.0.0.1 localhost
127.0.1.1 pcspectra-dev
127.0.0.1 forums.localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
Either will point the sub-domain to 127.0.0.1 if resolved locally. You then just need to modify httpd.conf if you plan on using these sub-domains for different websites.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Bam...perfect...I got everything working how I expected...

Couple more questions...

I get the Apache config directives and how they work...sorta...but the HOSTS file...is that part of Apache or part of the default linux install...is it's purpose basically to act as a local DNS???

So I could in theory I could bind my local loopback address: 127.0.0.1 to a domain like pooptest.com and whenever I entered http://pooptest.com it would act as localhost???

If you could just yes or no me on that one...it would certainly clear everything up a little...or better yet explain it...as the more I read about how it all works from the more sources I find the better off I am ;)

Secondly...if I needed to programatically locate that HOSTS file...is there a way?

Also, one more thing...

Under localhost I have a directory setup like:

docroot/myprojects
docroot/tempprojects
docroot/payprojects

I typically work under the /myprojects directory which is further broken up:

cms/
crm/
kbase/

and so on...

Right now, the project I'm working with is accessed via HTTP as: http://localhost/myprojects/someapp and I have mapped a forums subdomain to localhost so despite being for the above someapp it's actually accessed as: http://forums.localhost

It works, but looks awkward...

So I would obviously have to change my docroot to instead of /htdocs it would be /htdocs/myprojects/someapp the problem is...whenever I need to work on someapp_version3.4 I would need to again change the docroot...

So I wonder...is it possible to specify a docroot using .htaccess??? That way depending on which directory I start, Apache assume the local docroot instead of the default....and I think I just answered my own question there...as I can't see that being technically possible... :P

bummer :cry:
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Hockey wrote:Bam...perfect...I got everything working how I expected...

Couple more questions...

I get the Apache config directives and how they work...sorta...but the HOSTS file...is that part of Apache or part of the default linux install...is it's purpose basically to act as a local DNS???
Pretty Much, yup!
Hockey wrote: So I could in theory I could bind my local loopback address: 127.0.0.1 to a domain like pooptest.com and whenever I entered http://pooptest.com it would act as localhost???
Yep... (not really 'acting' like localhost, it will just resolve to the loopback, just as 'localhost' does)
Hockey wrote: If you could just yes or no me on that one...it would certainly clear everything up a little...or better yet explain it...as the more I read about how it all works from the more sources I find the better off I am ;)

Secondly...if I needed to programatically locate that HOSTS file...is there a way?
Should be as simple (and as universal) as /etc/hosts...
Hockey wrote: Also, one more thing...

Under localhost I have a directory setup like:

docroot/myprojects
docroot/tempprojects
docroot/payprojects

I typically work under the /myprojects directory which is further broken up:

cms/
crm/
kbase/

and so on...

Right now, the project I'm working with is accessed via HTTP as: http://localhost/myprojects/someapp and I have mapped a forums subdomain to localhost so despite being for the above someapp it's actually accessed as: http://forums.localhost

It works, but looks awkward...

So I would obviously have to change my docroot to instead of /htdocs it would be /htdocs/myprojects/someapp the problem is...whenever I need to work on someapp_version3.4 I would need to again change the docroot...

So I wonder...is it possible to specify a docroot using .htaccess??? That way depending on which directory I start, Apache assume the local docroot instead of the default....and I think I just answered my own question there...as I can't see that being technically possible... :P

bummer :cry:
Um?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Hockey wrote:Bam...perfect...I got everything working how I expected...

Couple more questions...

I get the Apache config directives and how they work...sorta...but the HOSTS file...is that part of Apache or part of the default linux install...is it's purpose basically to act as a local DNS???
Windows has an etc/hosts file to (it's in the system folder) :? It's a standard thing really. Viruses often modify the contents so that domains for the big virus cleaning apps point to localhost and render updates impossible.
Hockey wrote: So I could in theory I could bind my local loopback address: 127.0.0.1 to a domain like pooptest.com and whenever I entered http://pooptest.com it would act as localhost???
Yes
Hockey wrote: If you could just yes or no me on that one...it would certainly clear everything up a little...or better yet explain it...as the more I read about how it all works from the more sources I find the better off I am ;)

Secondly...if I needed to programatically locate that HOSTS file...is there a way?
/etc/hosts is there on all linux/unix systems. I'd be amazed if anybody ventured away from that standard. Mac OS X is based in Darwin (a UNIX system) and they have oddly decided to go with /private/etc/hosts but then Mac is sort of an exception anyway.
Hockey wrote: Also, one more thing...

Under localhost I have a directory setup like:

docroot/myprojects
docroot/tempprojects
docroot/payprojects

I typically work under the /myprojects directory which is further broken up:

cms/
crm/
kbase/

and so on...

Right now, the project I'm working with is accessed via HTTP as: http://localhost/myprojects/someapp and I have mapped a forums subdomain to localhost so despite being for the above someapp it's actually accessed as: http://forums.localhost

It works, but looks awkward...

So I would obviously have to change my docroot to instead of /htdocs it would be /htdocs/myprojects/someapp the problem is...whenever I need to work on someapp_version3.4 I would need to again change the docroot...

So I wonder...is it possible to specify a docroot using .htaccess??? That way depending on which directory I start, Apache assume the local docroot instead of the default....and I think I just answered my own question there...as I can't see that being technically possible... :P

bummer :cry:
I'm sure I've mentioned it a few times in this thread, VirtualHosts is what you need. Apache can determine different doc roots based upon the domain name used to access it.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Could you take a look at my Apache config for virtual hosts please?
At the moment I have setup a virtual host on my machine but it never redirects to the virtual hosts root directory.
<VirtualHost *>
ServerName stesbox.co.uk
DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost *>
ServerName test.stesbox.co.uk
DocumentRoot /var/www/html/test
</VirtualHost>
Whether I goto stesbox.co.uk or test.stesbox.co.uk, it always redirects me to the contents of /var/www/html.

I have restarted apache since changes were made and I have received no error messages.

Any ideas appreciated.

Regards,
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

impulse() wrote:Could you take a look at my Apache config for virtual hosts please?
At the moment I have setup a virtual host on my machine but it never redirects to the virtual hosts root directory.
<VirtualHost *>
ServerName stesbox.co.uk
DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost *>
ServerName test.stesbox.co.uk
DocumentRoot /var/www/html/test
</VirtualHost>
Whether I goto stesbox.co.uk or test.stesbox.co.uk, it always redirects me to the contents of /var/www/html.

I have restarted apache since changes were made and I have received no error messages.

Any ideas appreciated.

Regards,
Do you have this line anywhere in your httpd.conf?

Code: Select all

NameVirtualHost *
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Gracias amigo.
Post Reply