Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old October 26th, 2007   #1
Stex
Just Give Me the Beans!
 
Stex's Avatar
 
Join Date: Apr 2006
My beans are hidden!
HOWTO: localhost subdomains

It's a very simple and very useful hack. This was done on Apache2 under Gutsy.

1. Add your subdomain(s) to your hosts file:
  • Open up your network settings (System > Administration > Network)
  • Click on the Hosts tab, select the localhost alias and click properties
  • Add the subdomain(s) you want alongside localhost, separate with new lines
e.g:
localhost
extra.localhost
another.localhost


2. Apply subdomains to folders:
Open up a terminal or the Run Application diaogue (Alt+f2), then run the command: gksudo gedit /etc/apache2/httpd.conf

For each subdomain you need to add a VirtualHost clause, you also need to add one for the base localhost. The clause for each uses the template:
<VirtualHost *>
ServerName {Domain (e.g. localhost)}
DocumentRoot {Absolute path to folder for this domain (e.g. /var/www/)}
</VirtualHost>


Your file should end up looking something like this:
Code:
<VirtualHost *>
ServerName localhost
DocumentRoot /var/www/
</VirtualHost>

<VirtualHost *>
ServerName extra.localhost
DocumentRoot /var/www/extra/
</VirtualHost>

<VirtualHost *>
ServerName another.localhost
DocumentRoot /var/www/anothersite/
</VirtualHost>
Save and exit.

3. Restart apache:
Open up the terminal and run: sudo /etc/init.d/apache2 restart
You should now be able to view your subdomains through whatever browser you use (but obviously only from your computer). All done!

Making use of it:
I write and test websites on my localhost before uploading them to my public sites and I don't want to go through my code renaming localhost to example.com every time I change something. Nor do I want to use relative URLs as they can get messy once you get folders involved. By putting each site I make on it's own local subdomain I can start all my URLs with a slash which means the URL starts at the base of the subdomain.

That's why I use it anyway.


-- Notes:
- I couldn't find a way to apply wildcards to the hosts file (e.g. *.localhost), does anyone know?
- There are ways to direct all subdomains to folders automatically (mod_rewrite is good for that) but that's less needed for this purpose and would require wildcards in the hosts file
Stex is offline   Reply With Quote
Old October 30th, 2007   #2
meowsus
5 Cups of Ubuntu
 
meowsus's Avatar
 
Join Date: Oct 2007
Location: Philadelphia
Beans: 16
Ubuntu 7.10 Gutsy Gibbon
Send a message via AIM to meowsus
Re: HOWTO: localhost subdomains

This will come in handy at work. Very useful tip, Stex.

I'm pretty sure I've read that hosts files are unable to accept wild card entries. I know that wasn't the answer you were looking for, but at least now it won't keep you up at night.

Maybe theres another way. Hm. If anything pops into my brain I'll give you a holler.
__________________
- Meowsus (The Good Reverend Doktor Curdo)
- http://www.fauxfetus.net
- http://www.ughgod.org
- http://myspace.com/ughgod
meowsus is offline   Reply With Quote
Old November 25th, 2007   #3
Sarteck
Gee! These Aren't Roasted!
 
Sarteck's Avatar
 
Join Date: Jul 2006
Location: US
Beans: 200
Kubuntu 9.04 Jaunty Jackalope
Send a message via ICQ to Sarteck Send a message via AIM to Sarteck Send a message via MSN to Sarteck Send a message via Yahoo to Sarteck
Re: HOWTO: localhost subdomains

For Kubuntu newbies like myself, there are slight alterations to steps 1 and 2.
  1. Add your subdomain(s) to your hosts file:
    • Open up your network settings (K Menu > System Settings > Network Settings)
    • Click the "Administrator Mode" button
    • Click on the Domain Name System tab, select the localhost alias in the bottom "static hosts" pane, and click "Edit"
    • For each subdomain you want (other than "localhost"), click "add" and enter the name.
    • "OK" everything and apply the changes.
  2. The command for us Kubuntu users is
    Code:
    kdesu kate /etc/apache2/httpd.conf

Everything else remains the same, really. Happy website designing.



To the OP, thanks for this--I was unsure about the right format for step 2, and forgot the command to restart the apache server in step three. XD If you want to include the above in your HOWTO for us Kubuntu users, go right ahead.

Last edited by Sarteck; November 25th, 2007 at 12:44 PM..
Sarteck is offline   Reply With Quote
Old March 19th, 2008   #4
firmit
5 Cups of Ubuntu
 
Join Date: Jul 2007
Beans: 34
Xubuntu 8.04 Hardy Heron
Re: HOWTO: localhost subdomains

Of course - I found this post AFTER I made my own workaround. The subdomain trick is nice. However, I made my "multiple-virtual-local-servers"-thingy.

First, I added a couple of new hosts in /etc/hosts
Code:
127.0.0.1 localhost
127.0.0.2 alpha
127.0.0.3 beta
127.0.0.4 gamma
In /var/www, I added the folders alpha, beta, gamma containing index.php
Code:
<?php echo getcwd(); ?>
Then I added a new site in /etc/apache2/sites-availabe/
Code:
sudo touch greek
sudo nano greek
Code:
# alpha
<VirtualHost 127.0.0.2>
	DocumentRoot /var/www/alpha/
</VirtualHost>
# beta
<VirtualHost 127.0.0.3>
	DocumentRoot /var/www/beta/
</VirtualHost>
# gamma
<VirtualHost 127.0.0.4>
	DocumentRoot /var/www/gamma/
</VirtualHost>
And finished of with:
Code:
sudo a2ensite greek
sudo /etc/init.d/apache2 reload
Code:
firefox alpha
now showed /var/www/alpha

Last edited by firmit; March 19th, 2008 at 10:51 AM..
firmit is offline   Reply With Quote
Old June 13th, 2008   #5
josemota
First Cup of Ubuntu
 
Join Date: Jun 2008
Location: Portugal
Beans: 4
Ubuntu 8.04 Hardy Heron
Re: HOWTO: localhost subdomains

firmit's solution has green light in 8.04

Fantastic! Thanks firmit!
josemota is offline   Reply With Quote
Old June 13th, 2008   #6
wootah
Way Too Much Ubuntu
 
Join Date: Feb 2007
Location: Kamloops, BC
Beans: 310
Ubuntu 9.04 Jaunty Jackalope
Re: HOWTO: localhost subdomains

Great tutorial! This one's going in as a keeper
wootah is offline   Reply With Quote
Old July 9th, 2008   #7
limcopy
First Cup of Ubuntu
 
Join Date: Jul 2008
Beans: 1
Re: HOWTO: localhost subdomains

Quote:
Originally Posted by Stex View Post
It's a very simple and very useful hack. This was done on Apache2 under Gutsy.

1. Add your subdomain(s) to your hosts file:
  • Open up your network settings (System > Administration > Network)
  • Click on the Hosts tab, select the localhost alias and click properties
  • Add the subdomain(s) you want alongside localhost, separate with new lines
e.g:
localhost
extra.localhost
another.localhost


2. Apply subdomains to folders:
Open up a terminal or the Run Application diaogue (Alt+f2), then run the command: gksudo gedit /etc/apache2/httpd.conf

For each subdomain you need to add a VirtualHost clause, you also need to add one for the base localhost. The clause for each uses the template:
<VirtualHost *>
ServerName {Domain (e.g. localhost)}
DocumentRoot {Absolute path to folder for this domain (e.g. /var/www/)}
</VirtualHost>


Your file should end up looking something like this:
Code:
<VirtualHost *>
ServerName localhost
DocumentRoot /var/www/
</VirtualHost>

<VirtualHost *>
ServerName extra.localhost
DocumentRoot /var/www/extra/
</VirtualHost>

<VirtualHost *>
ServerName another.localhost
DocumentRoot /var/www/anothersite/
</VirtualHost>
Save and exit.

3. Restart apache:
Open up the terminal and run: sudo /etc/init.d/apache2 restart
You should now be able to view your subdomains through whatever browser you use (but obviously only from your computer). All done!

Making use of it:
I write and test websites on my localhost before uploading them to my public sites and I don't want to go through my code renaming localhost to example.com every time I change something. Nor do I want to use relative URLs as they can get messy once you get folders involved. By putting each site I make on it's own local subdomain I can start all my URLs with a slash which means the URL starts at the base of the subdomain.

That's why I use it anyway.


-- Notes:
- I couldn't find a way to apply wildcards to the hosts file (e.g. *.localhost), does anyone know?
- There are ways to direct all subdomains to folders automatically (mod_rewrite is good for that) but that's less needed for this purpose and would require wildcards in the hosts file
Dear Sir,

Thank you for the tutorial. I'm sorry that I new to this features

I had tried to set it and restart my apache server it prompt a message box request username and password ? May I know what is my username and password to login into the sub domain ?
limcopy is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:16 PM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. bilberry