Announcement

Collapse
No announcement yet.

Apache VirtualHost or Subdomains in windows XP or Vista how to setup

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Apache VirtualHost or Subdomains in windows XP or Vista how to setup

    ADVANCED EXPLANATION TIP
    I have installed on my XP machine Apache 2.2 with php 5.2 and mysql 5.1 + Phyton + CGI Perl in addition. I like to test some subdomain added to my localhost in my Apache for some tests works, and as reported at apache.org VirtualHost tutorial, i have open my httpd.conf file in the Apache /conf/ folder editing it in this way:
    Code:
    <VirtualHost *:80>
    ServerName sub.localhost
    DocumentRoot "k:/htdocs/sub"
    </VirtualHost>
    So it is necessary to restart the Apache server, but not before to have modify/edit another Windows XP configuration file (this is valid also for Vista as the file exist in the same exact position).
    Without this step VirtualHost won't work as needed in Windows XP or Vista.
    Search and open the follow file:
    Code:
    C:\WINDOWS\system32\drivers\etc\hosts
    open it and add after the line (it should exist)
    Code:
    127.0.0.1       localhost
    the following line:
    Code:
    127.0.0.1       sub.localhost
    Repeat the <VirtualHost *:80> directive in your httpd.conf file for any other subdomain you like to have, changing the name as preferred, ex. sub2.localhost: also do not forget to edit the file hosts of Windows adding the line 127.0.0.1 sub2.localhost and you should be up and running with virtualhost (subdomains) in Windows.
    Restart now Apache (restart maybe also your browser! or clean his cache if before you have try to connect to your subdomain and you was receiving error page as result. Until i have not restart explorer, the browser was returning the error page that was the result before i have correct the file HOSTS).
    So, finally point you browser to your http://sub.localhost and check that really work: the folder named sub need to exist inside the htdocs folder as in this example is explained/requested.

    There are many other things to setup and have an Apache server sufficiently, and correctly customized on Windows, but this is an important thing to know for any eventually test you need to start with a subdomain under the Apache server in Windows. It should also be correct procedure for subdomains that need to run under IIS as server.

  • #2
    How to use it for a port other than 80

    Hi,
    I want to know how do i configure the virtual host for a port other than port 80.

    Comment


    • #3
      example how to configure virtual host for different ports

      example how to edit the httpd.conf file of Apache:

      Code:
      Listen 80
      Listen 8080
      
      NameVirtualHost 172.20.30.40:80
      NameVirtualHost 172.20.30.40:8080
      
      <VirtualHost 172.20.30.40:80>
      
      ServerName www.example1.com
      DocumentRoot /www/domain-80
      
      </VirtualHost>
      
      <VirtualHost 172.20.30.40:8080>
      
      ServerName www.example1.com
      DocumentRoot /www/domain-8080
      
      </VirtualHost>
      
      <VirtualHost 172.20.30.40:80>
      
      ServerName www.example2.org
      DocumentRoot /www/otherdomain-80
      
      </VirtualHost>
      
      <VirtualHost 172.20.30.40:8080>
      
      ServerName www.example2.org
      DocumentRoot /www/otherdomain-8080
      
      </VirtualHost>

      Comment


      • #4
        This is very nice and decent post thanks a lot for posting it....!!!
        Last edited by ale; 22nd December 2015, 16:35.

        Comment


        • #5
          i've just do the same right now, years later! Same procedure now, as on 2007, it work.
          Sub domain Windows 8 this time.
          The HOST file to be edited, after the Apache httpd.conf, is the same folder: C:\Windows\System32\drive rs\etc
          Very nice, up and running with sub domains on localhost in few minutes this time

          Comment


          • #6
            and finally, as i want run 2 subdomains and a domain on same localhost, this is how on window the http.conf about VirtualHost is basically configured:
            Code:
            # All of these directives may appear inside <VirtualHost> containers,
            # in which case these default settings will be overridden for the
            # virtual host being defined.
            #
            NameVirtualHost *:80
            <VirtualHost *:80>
             DocumentRoot "F:/HTDOCS"
             ServerName localhost
             ErrorLog "logs/localhost-error_log"
             TransferLog "logs/localhost-access_log"
            </VirtualHost>
            <VirtualHost *:80>
             DocumentRoot "F:/HTDOCS/subdomain"
             ServerName subdomain.localhost
             ErrorLog "logs/subdomain.localhost-error_log"
             TransferLog "logs/subdomain.localhost-access_log"
            </VirtualHost>
            <VirtualHost *:80>
             DocumentRoot "F:/HTDOCS/subdomain-2"
             ServerName subdomain-2.localhost
             ErrorLog "logs/subdomain-2.localhost-error_log"
             TransferLog "logs/subdomain-2.localhost-access_log"
            </VirtualHost>
            the HOST file instead, look like this:
            Code:
            # localhost name resolution is handled within DNS itself.
            127.0.0.1       localhost
            ::1             localhost
            127.0.0.1       subdomain.localhost
            127.0.0.1       subdomain-2.localhost

            Comment

            Previously entered content was automatically saved. Restore or Discard.
            Auto-Saved
            x
            Insert: Thumbnail Small Medium Large Fullsize Remove  
            x
            x
            Working...
            X