XAMPP:Configurare un virtual server con XAMPP in Windows

← Torna a Windows

fonte: https://www.simogrima.com/tools/aggiungere-dei-virtual-hosts-in-xampp/

Aggiungere dei Virtual Hosts in XAMPP

La soluzione a tutti questi problemi si chiama Virtual Hosts (hosts virtuali), che possiamo creare con 2 semplici passaggi.

  1. Aggiungere degli Hosts Virtuali nella configurazione di apache: questa configurazione serve per informare Apache dove sono localizzati i files di una determinata richiesta (es: http://sitoA).
  2. Aggiornare il file host di Windows: questa configurazione serve per associare uno specifico indirizzo IP ad un nome di dominio. In questo modo (specificando il 127.0.0.1) forzeremo che il nostro ipotetico sito http://sitoA si cercato sul server web locale piuttosto che su internet (dove molto probabilmente non sarebbe trovato oppure corrisponderebbe ad un altro sito).

Aggiornare la configurazione di Apache

  1. Aprire con il notepad il seguente file: C:\xampp\apache\conf\extra\httpd-vhosts.conf
  2. Aggiungere le seguenti righe:
NameVirtualHost *:80
  <VirtualHost *:80>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
  </VirtualHost>
 
<VirtualHost www.sitoA.local:80>
   DocumentRoot "C:\wwww\siteA"
   ServerName www.sitoA.local
   <Directory "C:\wwww\siteA">
      DirectoryIndex index.php
      AllowOverride All
      Order allow,deny
      Allow from all
   </Directory>
</VirtualHost>

Le prime cinque righe servono ad attivare i Virtual Hosts su Apache ed a specificare C:\xampp\htdocs come percorso di default per http://localhost.

Questo ci permetterà di continuare a raggiungere le pagine di configurazione di XAMPP attraverso l’indirizzo http://localhost (quindi è molto importante).

Le righe successive servono a creare un Host Virtuale per il nuovo sito, non sto a spiegarle in dettaglio perchè piuttosto intuitive, il risultato finale sarà che digitando sulla barra degli indirizzi http://sitoA.locl sarà caricata la pagina: C:/wwww/siteA/index.php. Un trucchetto per essere sicuri di non “sovrascrivere” nessun sito realmente esistente su internet, perdendo così la possibilità di poterlo visualizzare, e quello di aggiungere al nostro dominio locale un suffisso o un prefisso particolare. Assegnare un dominio .locl può essere un’alternativa più che valida.

Naturalmente ne creeremo uno per ogni sito.

Aggiornare il file host di Windows

Ora non ci rimane che aggiungere una riga nuova per ogni Virtual Host creato sul file host:

  1. Editare il file .host con il notepad: C:\Windows\System32\drivers\etc\host
    Tenete presente che su Vista (o successivi) per poter modificare questo file è necessario editarlo come amministratori, quindi aprire il notepad cliccando con il desto e scegliendo “Esegui come amministratore”.
  2. Aggiungervi la seguente riga:
127.0.0.1	www.sitoA.local

l’indirizzo 127.0.0.1 si riferisce al tuo PC, quindi come spiegato in precedenza, aggiungendo questa riga non faremo altro che indicare a Windows di cercare l’indirizzo siteA.local sul PC locale anzichè sulla rete.

Aggiungere dei Virtual Hosts in XAMPP che lavori in HTTPS

fonte: https://florianbrinkmann.com/en/https-virtual-hosts-xampp-4215/

  1. Creating an SSL certificate
  2. Set up the virtual host

Creating an SSL certificate

A good tutorial about creating a cert is on robsnotebook.com. It is from 2007, but works. To create a certificate, you can follow these steps on the command line:

  1. Go to the Apache directory C:\xampp\apache.
  2. Run makecert.
  3. Enter a PEM passphrase and the other information you are asked for. For Common Name, you should enter the domain you want to use for the virtual host, so the certificate is signed for that domain.
  4. After processing all steps, you maybe want to import the cert into your browser (it lives under C:/xampp/apache/conf/ssl.crt/server.crt). Nevertheless, you will get a warning about insecure self-signed certificate after loading your website – you need to add it as an exception.

Set up virtual host with HTTPS

Entry in the Windows hosts file

To let Windows know, for example, that the domain florianbrinkmann.test should point to the IP address 127.0.0.1 (localhost), we have to insert an entry in the Windows hosts file. The file can be found in C:\Windows\System32\drivers\etc. To edit it, you need admin privileges (search for the editor in Windows, right-click on it and choose Run as administrator).

At the end of the hosts file, add an entry with the following pattern:

127.0.0.1 florianbrinkmann.test

You have to replace the domain with your own development domain. Afterwards, you can save and close the file.

Creating the Virtual Host in Apache

The virtual hosts in Apache can be found in the C:\xampp\apache\conf\extra\httpd-vhosts.conf file. Open the file and insert an entry according to the following pattern (an answer from stackoverflow.com was very helpful – the related question also, it brought me to the article on SSL certificate setup):

<VirtualHost florianbrinkmann.test:80>
	DocumentRoot "C:\xampp\htdocs\florianbrinkmann.test"
	ServerName florianbrinkmann.test
	<Directory "C:\xampp\htdocs\florianbrinkmann.test">
	Order allow,deny
	Allow from all
	</Directory>
</VirtualHost>

<VirtualHost florianbrinkmann.test:443>
	DocumentRoot "C:\xampp\htdocs\florianbrinkmann.test"
	ServerName florianbrinkmann.test
	SSLEngine On
	SSLCertificateFile "C:/xampp/apache/conf/ssl.crt/server.crt"
	SSLCertificateKeyFile "C:/xampp/apache/conf/ssl.key/server.key"
	<Directory "C:\xampp\htdocs\florianbrinkmann.test">
	Order allow,deny
	Allow from all
	</Directory>
</VirtualHost>

Here you have to adjust the settings for DocumentRoot, ServerName, Directory, and the domain in the VirtualHost element. The first VirtualHost is for HTTP connections, the second for HTTPS connections.

Maybe you have to uncomment the following line in the C:\xampp\apache\conf\httpd.conf:

LoadModule ssl_module modules/mod_ssl.so

Now you can restart Apache and open your site with HTTPS (including browser warning…).