Come configurare VirtualHosts in XAMPP su un Mac

Eravamo un grande fan di AMPPS, ma dall’aggiornamento a macOS Catalina, non era più possibile utilizzare questa applicazione. Sono sicuro che lo aggiorneranno di nuovo in futuro, ma il problema è che è un’applicazione a 32 bit, non è più compatibile sull’ultimo sistema operativo. È stato allora che abbiamo fatto il passaggio a XAMPP. Non è così semplice da usare come AMPPS, ma è probabilmente una delle migliori applicazioni per lo sviluppo di localhost.

L’installazione è stata un gioco da ragazzi, ma le cose sono diventate un po’ più complicate durante la configurazione di Apache VirtualHosts. Quindi ecco alcuni passi che abbiamo fatto per far funzionare tutto come volevamo. Quindi prima le cose prima…

Che cos’è VirtualHosts?

Ok, quindi spiegheremo cosa volevamo cambiare per far uscire localhost esattamente come lo volevamo…

“VirtualHosts allow Apache to map a hostname to a directory on the filesystem. You can set up as many VirtualHosts as you need so that each website operates under its own hostname.”

Quindi, come facciamo a fare questo cambiamento?

Come abilitare VirtualHosts

Firstly, you’ll need to open the following file; /Applications/XAMPP/xamppfiles/etc/httpd.conf in your preferred text editor. An easy way to get here is by going to ‘Finder’ –> ‘Go’ –> ‘Go to Folder’ and then simply pasting in the location.

Ora che hai il file aperto, dovrai cercare queste righe;

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

Ora, decommenta la seconda riga rimuovendo l’hash (#) in modo che Apache carichi i tuoi file VirtualHosts personalizzati come segue:

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

Creiamo i tuoi VirtualHosts

Apriamo ora il seguente file;//Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf. In fondo al file vedrai alcuni esempi di VirtualHosts, che dovresti commentare o eliminare.

Nella parte inferiore del file, aggiungere “localhost” come predefinito denominato VirtualHost in questo modo:

# localhost
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
    <Directory "/Applications/XAMPP/xamppfiles/htdocs">
        Options Indexes FollowSymLinks Includes execCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

What this does is allows http://localhost to point at XAMMP’s htdocs directory once we have created our VirtualHosts.

Fatto questo, ora possiamo creare i nostri VirtualHost. Quindi, dopo il localhost predefinito, ora possiamo aggiungere:

# My Custom Host per esempio.local
<VirtualHost *:80>
    ServerName yoursite.silva
    DocumentRoot "/applications/XAMPP/xamppfiles/htdocs/esempio.local"
    <Directory "/applications/XAMPP/xamppfiles/htdocs/esempio.local">
        Options Indexes FollowSymLinks Includes execCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

With the example above, you should replace yoursite.silva with your own hostname, this can be pretty much anything you like. The exception is using a hostname that will not conflict with a real domain name, like yoursite.dev. We used to use yoursite.devbut since it became owned by Google we can no longer use this along with quite a few others.

Ora abbiamo un altro passo da fare per farlo funzionare completamente…

Modifica il tuo file hosts

Ora dobbiamo andare nella seguente posizione; /etc/hosts, modificheremo il file hosts in modo che sappia come gestire il nuovo nome del server. Il file hosts viene utilizzato da OS C per mappare i nomi host agli indirizzi IP. Nel nostro caso, dal momento che useremo localhosts, vogliamo mappare il nome del server all’indirizzo IP 127.0.0.1 (questo è il tuo IP localhost).

Dobbiamo far corrispondere il 127.0.0.1 anche a esempio.local

Quindi in htdocs devi mettere la cartella del tuo sito esempio.local

Pro Tip: You can actually map any website to any IP address. We tend to do this for website migrations, let’s say we moved silvawebdesigns.com to a new IP address, we could simply add 192.123.123.124 silvawebdesigns.com (add www. if your website contact this in the web address). The advantage of this is that we can test the website on the new server before updating the Nameservers or DNS, this way we can be 100% the site will work perfectly before the migration.

Quindi, ora che abbiamo mappato il nome del server al tuo host locale, il passo successivo è…

Riavvia Apache

Quando si aggiorna uno di questi file, affinché le modifiche abbiano effetto, dobbiamo riavviare Apache. Questo può essere fatto tramite il controllo XAMPP che si trova qui:/Applications/XAMPP/XAMPP Control.app

Now, let’s point your browser to http://yoursite.silva (or whichever server name you chose) and you should see your website.

Se tutto funziona ora, allora giorni felici! Ma se hai un problema come un errore 403, vedi sotto:

Oh no, ho un errore 403!

Poiché Apache viene eseguito come utente “nessuno” per impostazione predefinita, potrebbe non avere le autorizzazioni necessarie per sfogliare la directory utente di OS X o le sue sottodirectory. In questo caso, riceverai un errore 403 “accesso vietato” quando tenti di visualizzare il tuo sito all’interno del tuo localhost. In altri casi, potresti vedere che puoi visualizzare il sito, ma ottieni alcuni errori PHP quando tenti di scrivere file o creare directory all’interno del tuo filesystem.

So, in order to fix this, you can configure Apache to run as your OS X user. To do this, open the following file; /Applications/XAMPP/xamppfiles/etc/httpd.conf and look for the following lines:

# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User daemon
Group daemon

Now, change User to your OS X username, and save the file:

Riavvia di nuovo Apache e ora dovresti essere in grado di navigare nel tuo sito senza problemi, compresa la manipolazione di file e cartelle usando PHP.

Se hai altri problemi, prova a impostare i privilegi di lettura e scrittura ( chmod 7777 ) dell’utente sul seguente file;/Applications/XAMPP/xamppfiles/htdocs/xampp/lang.tmp