Configure virtual hosts for XAMPP in Mac OS X

Lets suppose we want to have 2 virtual hosts in our server wich will be accesible via http://web1.com and http://web2.com and their content will be stored respectively in:

/Users/user/Documents/Webs/web1.com

and

/Users/user/Documents/Webs/web2.com

Firts we have to configure web1.com y web2.com to be resolved to our computer. We could configure a DNS server for that task but it’s much simpler to edit the /etc/hosts file and add:

127.0.0.1 web1.com
127.0.0.1 web2.com

Check in a terminal that doing ping to those names they are resolved as 127.0.0.1:

$ ping web1.com
PING web1.com (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.044 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.068 ms

Download XAMPP and install it.

Edit /Applications/XAMPP/etc/httpd.conf and change User and Group parameters to your user name and staff group.

User user
Group staff

Uncomment the line that includes the virtual hosts configuration file:

# Virtual hosts
Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf

Edit Applications/XAMPP/etc/extra/httpd-vhosts.conf and delete 2 example VirtualHost that come by default and add the following:


    ServerAdmin webmaster
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
    ServerName localhost
    ServerAlias localhost
    ErrorLog "logs/localhost-error_log"
    CustomLog "logs/localhost-access_log" common

 

    ServerAdmin webmaster
    DocumentRoot "/Users/user/Documents/Webs/web1.com"
    ServerName web1.com
    ServerAlias web1.com
    ErrorLog "logs/web1.com-error_log"
    CustomLog "logs/web1.com-access_log" common
    DirectoryIndex index.html
    
        #IndexOptions +FancyIndexing NameWidth=*
        #Options Includes FollowSymLinks Indexes
        #AllowOverride All
        Order allow,deny
        Allow from all
    

 

    ServerAdmin webmaster
    DocumentRoot "/Users/user/Documents/Webs/web2.com"
    ServerName web2.com
    ServerAlias web2.com
    ErrorLog "logs/web2.com-error_log"
    CustomLog "logs/web2.com-access_log" common
    DirectoryIndex index.html
    
        #IndexOptions +FancyIndexing NameWidth=*
        #Options Includes FollowSymLinks Indexes
        #AllowOverride All
        Order allow,deny
        Allow from all
    

As you can see there is also a VirtualHost for the root of XAMPP thus we can access status info of the server and phpMyAdmin console opening http://localhost.

Put inside our 2 web folders a index.html file with some content, start XAMPP and check you can access both virtual hosts URLs and that you see index.html content. You can change DirectoryIndex value to a list of files and XAMPP will load the first one it finds. For example to try loading first index.php and then index.html:

DirectoryIndex index.php index.html

If you want to access the folder content of the directories just uncomment first 3 lines in ... tag. Other way you would get a 403 error.

You might also like

Apache 2 + MySQL 5 + PHP 5.3 + phpMyAdmin 3.5.4 installation script for Mac OS X using MacPorts
Based on the instructions provided by Gilles Fabio I've created a complete installation script to install...
Install a Symfony2 application using Composer
Before installing any Symfony2 project you have to meet the minimum requirements for it in your server....
Password protect a folder in Apache (XAMPP and Mac OS X)
First you have to create a file with the user and the password to be used with htpasswd: /Applications/XAMPP/xamppfiles/bin/htpasswd...
Password protect a Location, LocationMatch or VirtualHost in Apache (XAMPP and Mac OS X)
Protecting a Location or VirtualHost in Apache is very similar to protecting a folder as I explained...
Leave a comment ?

7 Comments.

  1. Thanks for your tutorial. The hint to change the User and Group saved my day. Now all is running well on my Mac.
    Cheers Mike

  2. Buenas

    No se si pillo un poco tarde la entrada de este post, ya que es del 2009.

    Tengo un problemas y me seria de gran utilidad una pequeña ayuda. Instalé xampp y al poner localhost en el navegador no me iba a la pagina inicial de xampp donde poder configurar la base de datos y demas… asi que seguí este tutorial a ver si me servia, de echo me sirvió y pude instalar wordpress que era lo que pretendia. Al finalizar el tutorial el paso de “Meter dentro de las carpetas de nuestras 2 webs un index.html con algún contenido…” no lo hice sino que directamente escribi web1.com en el navegador y me funcionó, así que continue con la instalación de worpress, todo correcta, hasta que al dia siguiente arranque xampp escribi web1.com en el navegador y me da un erro “OBJETO NO ENCONTRADO” ERROR 404″ La cosa es que no se que hacer, no se donde he fallado ;(

    • Es muy raro que un día te funcione y al siguiente no. Se me ocurre que pueden ser una de estas 2 cosas:

      - Permisos de los ficheros. El usuario que ejecuta el servidor web no es capaz de leer los ficheros en la carpeta donde están alojados.
      - Por algún motivo se ha perdido la configuración de web1.com que tenías hecha en el fichero de hosts para que apuntara a localhost.

  3. woow que rapidez… ;)

    Ahora que recuerdo hice otro tutorial para dar permisos a wordpress ya que no podia subie fotos a la web, este http://gamelote.net/2012/10/12/una-solucion-al-problema-de-permisos-con-wordpress-en-local/ no se si tendra algo q ver…

    Muchas gracias

    • Pues probablemente sea eso. Asegúrate de que la carpeta donde tienes el wordpress tenga los permisos bien. El usuario propietario debe ser tu usuario y el grupo propietario el “staff”. Los permisos para los ficheros que sean 644 y para las carpetas 755. En Linux se cambiaría así, en Mac ahora mismo no se si sirve exactamente igual:

      find /carpeta -type d -print -exec chmod 755 {} \;
      find /carpeta -type f -print -exec chmod 644 {} \;
      find /carpeta -type d -print -exec sudo chown usuario:staff {} \;
      find /carpeta -type f -print -exec sudo chown usuario:staff {} \;

      Ojo que tienes que modificar en los comandos el nombre de tu usuario y la carpeta sobre la que ejecutas el comando eh

Leave a Comment