➜apache vhosts directive

! apache vhosts directive
» web » apache vhosts directive » « Prev Next »
  • Logs Apache
  • Htaccess apache rewriting
  • Apache vhosts directive
  • Avant-propos

    Tout le monde sait qu'à l'installation d'Apache, il nous faut spécifier un nom d'hôte.
    Mais savez vous qu'il est possible de créer plusieurs hôtes ?
    C'est ce qu'on appelle les Virtual Host.


    Le fichier vhost.conf


    Nous allons donc stopper Apache durant notre toute notre manoeuvre.
    Une fois Apache stoppé, rendez vous dans le répertoire de configuration d'Apache. Nous allons créer un nouveau fichier de configuration pour y intégrer notre nouveau Virtual Host.
    Nous pourrions bien sûr l'intégrer directement dans le httpd.conf mais celà est beaucoup plus pratique d'avoir un fichier bien distinct vu que les includes fonctionnent très bien sous Apache.
    Personnellement, j'ai un fichier pour chaque chose chez moiun php.conf, un alias.conf, un error.conf, etc... ;)
    Nous nommerons donc ce fichier vhost.conf.
    Voici les lignes à intégrer dans le fichier vhost.conf :

    NameVirtualHost www.mon-domaine2.joe Ici, on nomme notre Virtual Host.
    < VirtualHost www.mon-domaine2.joe > La même chose ici.
    Vous pouvez aussi rajouter le port comme ceci www.mon-domaine2.joe:80
    ServerAdmin mon-email@domaine.com Vous avez compris qu'il faut que vous entriez votre email ici !
    bien sûr cette adresse peut être différente de celle de votre premier domaine (www.mon-domaine.joe). C'est d'ailleurs un des intérêts du Virtual Host.
    DocumentRoot H:/home/www/domaine2/ Entrez ici le chemin complet jusqu'au répertoire de votre site www.mon-domaine2.joe
    Logiquement ce n'est pas le même que votre premier site.
    ServerName www.mon-domaine2.joe Rien de neuf, on entre le nom du Virtual Host comme nom de Serveur.
    ErrorLog logs/www.mon-domaine2.joe-error_log Un petit plus sympathique ;)
    Pour plus de lisibilité, on stocke les erreurs dans un autre fichier que le premier site.
    Vous pouvez nommez ce fichier comme bon vous semble.
    TransferLog logs/www.mon-domaine2.joe-access_log La même chose qu'au dessus, sauf que là c'est pas pour les erreurs :D
    < /VirtualHost > On ferme la configuration de ce Virtual Host comme on fermerait un TAG en HTML.

    Une fois que notre vhost.conf est prêt, nous pouvons l'inclure dans le fichier httpd.conf afin qu'Apache le prenne en compte.
    Nous allons donc ouvrir un httpd.conf et rajouter, en bas de préférence pour y accéder plus facilement, la ligne :
    Include conf/vhost.conf

    Vous pensez que c'est le moment de redémarrer Apache et de voir ce que ça donne tout ça ?
    Et bien détrompez vous, nous en sommes qu'à la moitié :D !


    Le fichier hosts


    La seconde Étape est le fichier hosts de Windows.
    Direction C:WINDOWSsystem32driversetc, le bon fichier est hosts (voir note ci dessous).

    Note : Le fichier hosts peut se trouver dans un répertoire différent sur certaines configs. Vous pouvez chercher dans C:WINDOWS, C:WINDOWSsystem32, C:WINDOWSsystem ou dans tout autre répertoire système de WINDOWS Le fichier peut comporter une extension (par exemple hosts.sam) La casse peut aussi changer. (hosts ou Hosts)

    Je vous conseille fortement de faire un raccourci sur votre bureau, si vous pensez ajouter plusieurs Virtual Host.
    Une fois ce fichier ouvert, vous pourrez voir ceci :

    # Copyright (c) 1993-1999 Microsoft Corp.
    #
    # Ceci est un exemple de fichier HOSTS utilisé par Microsoft TCP/IP
    # pour Windows.
    #
    # Ce fichier contient les correspondances des adresses IP aux noms d'hôtes.
    # Chaque entrée doit être sur une ligne propre. L'adresse IP doit être placée
    # dans la première colonne, suivie par le nom d'hôte correspondant. L'adresse
    # IP et le nom d'hôte doivent être séparés par au moins un espace.
    #
    # De plus, des commentaires (tels que celui-ci) peuvent être insérés sur des
    # lignes propres ou après le nom d'ordinateur. Ils sont indiqué par le
    # symbole '#'.
    #
    # Par exemple :
    #
    # 102.54.94.97 rhino.acme.com # serveur source
    # 38.25.63.10 x.acme.com # hôte client x
    127.0.0.1 localhost

    Deux possibilités s'offrent à vous :

    1. Soit vous êtes le seul PC et vous ne disposez pas de réseau donc pas besoin de prévoir qu'une autre machine puisse accéder à www.mon-domaine2.joe
    2. Soit vous êtes sur un réseau et vous trouvez ça intéressant de faire plusieurs sites Intranet accessibles facilement par www.le-domaine.joe au lieu de http://192.168.0.123/site-que-je-veux-voir/
    Dans le cas 1.
    Il suffit de rajouter la ligne :
    127.0.0.1 www.mon-domaine2.joe

    Dans le cas 2.
    Il faut que vous rajoutiez l'adresse IP que vous avez sur votre réseau. Pour l'exemple, 192.168.0.123.
    Il suffit alors de rajouter la ligne :
    192.168.0.123 www.mon-domaine2.joe

    Vous pouvez, bien entendu, rajouter autant de lignes que vous le souhaitez avec la même IP et un ' domaine différent '

    Au final notre fichier hosts donnera ceci :

    pour le cas 1
    # Copyright (c) 1993-1999 Microsoft Corp.
    #
    # Ceci est un exemple de fichier HOSTS utilisé par Microsoft TCP/IP
    # pour Windows.
    #
    # Ce fichier contient les correspondances des adresses IP aux noms d'hôtes.
    # Chaque entrée doit être sur une ligne propre. L'adresse IP doit être placée
    # dans la première colonne, suivie par le nom d'hôte correspondant. L'adresse
    # IP et le nom d'hôte doivent être séparés par au moins un espace.
    #
    # De plus, des commentaires (tels que celui-ci) peuvent être insérés sur des
    # lignes propres ou après le nom d'ordinateur. Ils sont indiqué par le
    # symbole '#'.
    #
    # Par exemple :
    #
    # 102.54.94.97 rhino.acme.com # serveur source
    # 38.25.63.10 x.acme.com # hôte client x
    127.0.0.1          localhost
    127.0.0.1   www.mon-domaine2.joe

    pour le cas 2
    # Copyright (c) 1993-1999 Microsoft Corp.
    #
    # Ceci est un exemple de fichier HOSTS utilisé par Microsoft TCP/IP
    # pour Windows.
    #
    # Ce fichier contient les correspondances des adresses IP aux noms d'hôtes.
    # Chaque entrée doit être sur une ligne propre. L'adresse IP doit être placée
    # dans la première colonne, suivie par le nom d'hôte correspondant. L'adresse
    # IP et le nom d'hôte doivent être séparés par au moins un espace.
    #
    # De plus, des commentaires (tels que celui-ci) peuvent être insérés sur des
    # lignes propres ou après le nom d'ordinateur. Ils sont indiqué par le
    # symbole '#'.
    #
    # Par exemple :
    #
    # 102.54.94.97 rhino.acme.com # serveur source
    # 38.25.63.10 x.acme.com # hôte client x
    127.0.0.1          localhost
    192.168.0.123   www.mon-domaine2.joe

    Une fois celà fait, on sauvegarde le fichier hosts et on redémarre Apache, enfin !

    On teste l'adresse www.mon-domaine2.joe et... Ooooh miracle ça marche !
    Dans le cas contraire, reprenez depuis le début, vous avez du sûrement rater une étape !

    Une fois que vous avez bien compris comment celà fonctionne, vous pouvez vous amusez à faire des Sub-Domains du genre forum.mondomaine.joe qui pointe en fait sur un sous-répertoire !

    Comme je suis bon et généreux, je vous donne même en prime la possibilité de télécharger le hosts et le vhost.conf de ce tutoriel !


    "http://httpd.apache.org/docs/1.3/images/sub.gif" alt="[APACHE DOCUMENTATION]" />

    Apache HTTP Server Version 1.3

    Is this the version you want? For more recent versions, check our documentation index.

    Virtual Host examples for common setups

    Base configuration

    • Simple name-based vhosting
    • More complicated name-based vhosts
    • IP-based vhosts
    • Mixed name-/IP-based vhosts
    • Port-based vhosts

    Additional features

    • Using _default_ vhosts
    • Migrating a named-based vhost to an IP-based vhost
    • Using the ServerPath directive

    Simple name-based vhosting

    • Compatibility: This syntax was added in Apache 1.3.13.
    • Setup: The server machine has a primary name server.domain.tld. There are two aliases (CNAMEs) www.domain.tld and www.sub.domain.tld for the address server.domain.tld.

      Server configuration:

       .
      Port 80
      ServerName server.domain.tld

      NameVirtualHost *:80


      DocumentRoot /www/domain
      ServerName www.domain.tld
      .



      DocumentRoot /www/subdomain
      ServerName www.sub.domain.tld
      .


      The asterisks match all addresses, so the main server serves no requests. Due to the fact that www.domain.tld is first in the configuration file, it has the highest priority and can be seen as the default or primary server.

    More complicated name-based vhosts

    • Setup 1: The server machine has one IP address (111.22.33.44) which resolves to the name server.domain.tld. There are two aliases (CNAMEs) www.domain.tld and www.sub.domain.tld for the address 111.22.33.44.

      Server configuration:

       .
      Port 80
      ServerName server.domain.tld

      NameVirtualHost 111.22.33.44


      DocumentRoot /www/domain
      ServerName www.domain.tld
      .



      DocumentRoot /www/subdomain
      ServerName www.sub.domain.tld
      .


      Apart from localhost there are no unspecified addresses/ports, therefore the main server only serves localhost requests. Due to the fact that www.domain.tld has the highest priority it can be seen as the default or primary server.
    • Setup 2: The server machine has two IP addresses (111.22.33.44 and 111.22.33.55) which resolve to the names server1.domain.tld and server2.domain.tld respectively. The alias www.domain.tld should be used for the main server which should also catch any unspecified addresses. We want to use a virtual host for the alias www.otherdomain.tld and another virtual host, with server name www.sub.domain.tld, should catch any request to hostnames of the form *.sub.domain.tld. The address 111.22.33.55 should be used for the virtual hosts.

      Server configuration:

       .
      Port 80
      ServerName www.domain.tld
      DocumentRoot /www/domain

      NameVirtualHost 111.22.33.55


      DocumentRoot /www/otherdomain
      ServerName www.otherdomain.tld
      .



      DocumentRoot /www/subdomain
      ServerName www.sub.domain.tld
      ServerAlias *.sub.domain.tld
      .


      Any request to an address other than 111.22.33.55 will be served from the main server. A request to 111.22.33.55 with an unknown or no Host: header will be served from www.otherdomain.tld.
    • Setup 3: The server machine has two IP addresses (192.168.1.1 and 111.22.33.55). The machine is sitting between an internal (intranet) network and an external (internet) network. Outside of the network, the name server1.domain.tld resolves to the external address (111.22.33.55), but inside the network, that same name resolves to the internal address (192.168.1.1).

      The server can be made to respond to internal and external requests with the same content, with just one VirtualHost section.

      Server configuration:

       .
      NameVirtualHost 192.168.1.1
      NameVirtualHost 111.22.33.55


      DocumentRoot /www/server1
      ServerName server1.domain.tld
      ServerAlias server1
      .


      Now requests from both networks will be served from the same VirtualHost
    • Setup 4: You have multiple domains going to the same IP and also want to serve multiple ports. By defining the ports in the 'NameVirtualHost' tag, you can allow this to work. If you try using without the NameVirtualHost name:port or you try to use the Port directive, your configuration will not work.

      Server configuration:

       . 
      NameVirtualHost 111.22.33.44:80
      NameVirtualHost 111.22.33.44:8080


      ServerName www.domain.tld
      DocumentRoot /www/domain-80



      ServerName www.domain.tld
      DocumentRoot /www/domain-8080



      ServerName www.otherdomain.tld
      DocumentRoot /www/otherdomain-80



      ServerName www.otherdomain.tld
      DocumentRoot /www/otherdomain-8080




    IP-based vhosts

    • Setup 1: The server machine has two IP addresses (111.22.33.44 and 111.22.33.55) which resolve to the names server.domain.tld and www.otherdomain.tld respectively. The hostname www.domain.tld is an alias (CNAME) for server.domain.tld and will represent the main server.

      Server configuration:

       .
      Port 80
      DocumentRoot /www/domain
      ServerName www.domain.tld


      DocumentRoot /www/otherdomain
      ServerName www.otherdomain.tld
      .


      www.otherdomain.tld can only be reached through the address 111.22.33.55, while www.domain.tld can only be reached through 111.22.33.44 (which represents our main server).
    • Setup 2: Same as setup 1, but we don't want to have a dedicated main server.

      Server configuration:

       .
      Port 80
      ServerName server.domain.tld


      DocumentRoot /www/domain
      ServerName www.domain.tld
      .



      DocumentRoot /www/otherdomain
      ServerName www.otherdomain.tld
      .


      The main server can never catch a request, because all IP addresses of our machine are in use for IP-based virtual hosts (only localhost requests can hit the main server).
    • Setup 3: The server machine has two IP addresses (111.22.33.44 and 111.22.33.55) which resolve to the names server.domain.tld and www-cache.domain.tld respectively. The hostname www.domain.tld is an alias (CNAME) for server.domain.tld and will represent the main server. www-cache.domain.tld will become our proxy-cache listening on port 8080, while the web server itself uses the default port 80.

      Server configuration:

       .
      Port 80
      Listen 111.22.33.44:80
      Listen 111.22.33.55:8080
      ServerName server.domain.tld


      DocumentRoot /www/domain
      ServerName www.domain.tld
      .



      ServerName www-cache.domain.tld
      .

      Order Deny,Allow
      Deny from all
      Allow from 111.22.33



      The main server can never catch a request, because all IP addresses (apart from localhost) of our machine are in use for IP-based virtual hosts. The web server can only be reached on the first address through port 80 and the proxy only on the second address through port 8080.

    Mixed name-/IP-based vhosts

    • Setup: The server machine has three IP addresses (111.22.33.44, 111.22.33.55 and 111.22.33.66) which resolve to the names server.domain.tld, www.otherdomain1.tld and www.otherdomain2.tld respectively. The address 111.22.33.44 should be used for a couple of name-based vhosts and the other addresses for IP-based vhosts.

      Server configuration:

       .
      Port 80
      ServerName server.domain.tld

      NameVirtualHost 111.22.33.44


      DocumentRoot /www/domain
      ServerName www.domain.tld
      .



      DocumentRoot /www/subdomain1
      ServerName www.sub1.domain.tld
      .



      DocumentRoot /www/subdomain2
      ServerName www.sub2.domain.tld
      .



      DocumentRoot /www/otherdomain1
      ServerName www.otherdomain1.tld
      .



      DocumentRoot /www/otherdomain2
      ServerName www.otherdomain2.tld
      .



    Port-based vhosts

    • Setup: The server machine has one IP address (111.22.33.44) which resolves to the name www.domain.tld. If we don't have the option to get another address or alias for our server we can use port-based vhosts if we need a virtual host with a different configuration.

      Server configuration:

       .
      Listen 80
      Listen 8080
      ServerName www.domain.tld
      DocumentRoot /www/domain


      DocumentRoot /www/domain2
      .


      A request to www.domain.tld on port 80 is served from the main server and a request to port 8080 is served from the virtual host.

    Using _default_ vhosts

    • Setup 1: Catching every request to any unspecified IP address and port, i.e., an address/port combination that is not used for any other virtual host.

      Server configuration:

       .

      DocumentRoot /www/default
      .


      Using such a default vhost with a wildcard port effectively prevents any request going to the main server.
      A default vhost never serves a request that was sent to an address/port that is used for name-based vhosts. If the request contained an unknown or no Host: header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file).
      You can use AliasMatch or RewriteRule to rewrite any request to a single information page (or script).
    • Setup 2: Same as setup 1, but the server listens on several ports and we want to use a second _default_ vhost for port 80.

      Server configuration:

       .

      DocumentRoot /www/default80
      .



      DocumentRoot /www/default
      .


      The default vhost for port 80 (which must appear before any default vhost with a wildcard port) catches all requests that were sent to an unspecified IP address. The main server is never used to serve a request.
    • Setup 3: We want to have a default vhost for port 80, but no other default vhosts.

      Server configuration:

       .

      DocumentRoot /www/default
      .


      A request to an unspecified address on port 80 is served from the default vhost any other request to an unspecified address and port is served from the main server.

    Migrating a name-based vhost to an IP-based vhost

    • Setup: The name-based vhost with the hostname www.otherdomain.tld (from our name-based example, setup 2) should get its own IP address. To avoid problems with name servers or proxies who cached the old IP address for the name-based vhost we want to provide both variants during a migration phase.
      The solution is easy, because we can simply add the new IP address (111.22.33.66) to the VirtualHost directive.

      Server configuration:

       .
      Port 80
      ServerName www.domain.tld
      DocumentRoot /www/domain

      NameVirtualHost 111.22.33.55


      DocumentRoot /www/otherdomain
      ServerName www.otherdomain.tld
      .



      DocumentRoot /www/subdomain
      ServerName www.sub.domain.tld
      ServerAlias *.sub.domain.tld
      .


      The vhost can now be accessed through the new address (as an IP-based vhost) and through the old address (as a name-based vhost).

    Using the ServerPath directive

    • Setup: We have a server with two name-based vhosts. In order to match the correct virtual host a client must send the correct Host: header. Old HTTP/1.0 clients do not send such a header and Apache has no clue what vhost the client tried to reach (and serves the request from the primary vhost). To provide as much backward compatibility as possible we create a primary vhost which returns a single page containing links with an URL prefix to the name-based virtual hosts.

      Server configuration:

       .
      NameVirtualHost 111.22.33.44


      # primary vhost
      DocumentRoot /www/subdomain
      RewriteEngine On
      RewriteRule ^/.* /www/subdomain/index.html
      .



      DocumentRoot /www/subdomain/sub1
      ServerName www.sub1.domain.tld
      ServerPath /sub1/
      RewriteEngine On
      RewriteRule ^(/sub1/.*) /www/subdomain$1
      .



      DocumentRoot /www/subdomain/sub2
      ServerName www.sub2.domain.tld
      ServerPath /sub2/
      RewriteEngine On
      RewriteRule ^(/sub2/.*) /www/subdomain$1
      .


      Due to the ServerPath directive a request to the URL http://www.sub1.domain.tld/sub1/ is always served from the sub1-vhost.
      A request to the URL http://www.sub1.domain.tld/ is only served from the sub1-vhost if the client sent a correct Host: header. If no Host: header is sent the client gets the information page from the primary host.
      Please note that there is one oddity: A request to http://www.sub2.domain.tld/sub1/ is also served from the sub1-vhost if the client sent no Host: header.
      The RewriteRule directives are used to make sure that a client which sent a correct Host: header can use both URL variants, i.e., with or without URL prefix.

    Apache HTTP Server Version 1.3

    "http://httpd.apache.org/docs/1.3/images/index.gif" alt="Index" /> "http://httpd.apache.org/docs/1.3/images/home.gif" alt="Home" />
    Rachat Credit Banque France - Lupaland
    apache vhosts directive © BMX - - 03:23:56@25186 -
    Blog, Sitemap, Projets, Videos XXX Allopass, Rachat de Credit