Results 1 to 3 of 3

Thread: HOWTO: localhost subdomains

  1. #1
    Join Date
    Apr 2006

    HOWTO: localhost subdomains

    It's a simple and very useful hack. This was done on Apache2 under Ubuntu 8.10 (Ibex).

    For versions of ubuntu earlier than 8.10 see the outdated tutorial:
    Both stages have had to be edited for 8.10 as the network manager has disappeared and Apache isn't recognizing the httpd.conf file like it used to.

    1. Add your subdomain(s) to your hosts file:
    Open up a terminal or the Run Application diaogue (Alt+f2), then run the command: gksudo gedit /etc/hosts
    (KDE users run kdesu kate /etc/hosts from terminal)
    • after the first line (ending in "localhost") create a new line
    • For each subdomain you want write in a new line in the format:
      127.0.0.# name.localhost
      The # must be a unique number for each domain
    • Save and close. You should now have a file beginning something like:

    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/sites-available/subdomains
    (KDE users run kdesu kate /etc/apache2/sites-available/subdomains from terminal)

    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 127.0.0.#>
    ServerName {Domain (e.g. localhost)}
    DocumentRoot {Absolute path to folder for this domain (e.g. /var/www/)}

    Your file should end up looking something like this:
    ServerName localhost
    DocumentRoot /var/www/
    ServerName extra.localhost
    DocumentRoot /var/www/extra/
    ServerName another.localhost
    DocumentRoot /var/www/anothersite/
    Make sure the IPs match up with the ones you defined in the hosts file.
    Save and exit.

    3. Restart apache with subdomains:
    Open up the terminal and run:
    sudo a2ensite subdomians
    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 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:
    - Thanks to firmit's post for help with the new way of doing this
    - I couldn't find a way to apply wildcards to the hosts file (e.g. *.localhost), does anyone know?

  2. #2
    Join Date
    Oct 2004
    Barcelona, Spain
    Ubuntu Development Release

    Re: HOWTO: localhost subdomains

    Nice one, dude!
    Ubuntu in Spanish is Ubuñtu!
    Running Aspire 9814 AMD64 & Extensa 5620

  3. #3
    Join Date
    Oct 2009

    Re: HOWTO: localhost subdomains

    Excellent guide. Just got it working in Karmic.

    You just have one typo under step 3. The guide reads:

    sudo a2ensite subdomians

    It should read
    sudo a2ensite subdomains
    Thanks again!

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts