I’ll go step-by-step on how to create a virtual host in the XAMPP environment For Linux.
As we know, the default http://localhost points to /opt/lampp/htdocs
as the root directory. The desired result is to be able to visit http://examplevhost.local, with the root directory being /opt/lampp/htdocs/examplevhost
.
Note: The steps below are done on Ubuntu 20.04, but they should also work on most other Linux distributions (Debian, Mint, Arch).
Note: I’ll assume that XAMPP is installed in
/opt/lampp/
. If it’s different on your setup, please read carefully and adjust accordingly.
Note: This should be done only once per XAMPP installation. If you want to add another virtual host later you can skip to the next step.
sudo nano /opt/lampp/etc/httpd.conf
- Find the line that looks like this:
# Virtual hosts
# Include etc/extra/httpd-vhosts.conf
- Uncomment the one that starts with "Include":
# Virtual hosts
Include etc/extra/httpd-vhosts.conf
sudo mkdir /opt/lampp/htdocs/examplevhost
sudo touch /opt/lampp/htdocs/examplevhost/index.html
sudo nano /etc/hosts
- Add this line to the bottom of the file:
127.0.0.1 examplevhost.local
Note: You should add this line
127.0.0.1 localhost
once in the/etc/hosts
file (if it is not already exists)
sudo nano /opt/lampp/etc/extra/httpd-vhosts.conf
- Since we enabled this file in the first step we can start editing it. Add this:
<VirtualHost *:80>
<Directory "/opt/lampp/htdocs/examplevhost">
AllowOverride All
</Directory>
ServerAdmin your_mail@provider.com
ServerName examplevhost.local
ServerAlias www.examplevhost.local
DocumentRoot "/opt/lampp/htdocs/examplevhost"
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Note: You can re-use this snipped for any number of virtual hosts. The directives that need changing are
DocumentRoot
,ServerAlias
, andServerName
(ServerName
must exist as an entry in/etc/hosts
).
Note: It's safe to remove the example virtual hosts that come by default.
- Restart XAMPP
sudo /opt/lampp/lampp restart
- Visit http://examplevhost.local and you should see the content of
/opt/lampp/htdocs/examplevhost/index.html
.