Results 1 to 3 of 3

Thread: HOWTO: localhost subdomains

  1. #1
    Join Date
    Apr 2006
    Beans
    Hidden!

    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: http://ubuntuforums.org/showthread.php?t=592456
    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:
    127.0.0.1 localhost
    127.0.0.2 extra.localhost
    127.0.0.3 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/)}
    </VirtualHost>


    Your file should end up looking something like this:
    Code:
    <VirtualHost 127.0.0.1>
    ServerName localhost
    DocumentRoot /var/www/
    </VirtualHost>
    
    <VirtualHost 127.0.0.2>
    ServerName extra.localhost
    DocumentRoot /var/www/extra/
    </VirtualHost>
    
    <VirtualHost 127.0.0.3>
    ServerName another.localhost
    DocumentRoot /var/www/anothersite/
    </VirtualHost>
    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:
    Code:
    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 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:
    - 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
    Location
    Barcelona, Spain
    Beans
    191
    Distro
    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
    Beans
    4

    Re: HOWTO: localhost subdomains

    Excellent guide. Just got it working in Karmic.

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

    Code:
    sudo a2ensite subdomians

    It should read
    Code:
    sudo a2ensite subdomains
    Thanks again!

Tags for this Thread

Bookmarks

Posting Permissions

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