I’ll go step-by-step on how to create a virtual host in the XAMPP environment.
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 16.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 gedit /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
$ mkdir /opt/lampp/htdocs/examplevhost
$ gedit /opt/lampp/htdocs/examplevhost/index.html
$ sudo gedit /etc/hosts
- Add this line to the bottom of the file:
127.0.1.1 examplevhost.local
- Since we enabled this file in the first step we can start editing it. Add this:
<VirtualHost *:80>
DocumentRoot "/opt/lampp/htdocs/examplevhost"
ServerName examplevhost.local
ErrorLog "logs/examplevhost.local-error_log"
CustomLog "logs/examplevhost.local-access_log" common
</VirtualHost>
Note: You can re-use this snipped for any number of virtual hosts. The directives that need changing are
DocumentRoot
andServerName
(ServerName
must exist as an entry in/etc/hosts
, andDocumentRoot
must exist as a directory in/opt/lampp/htdocs/
) and that's it to see the site working. However, consider adjustingErrorLog
andCustomLog
to find your error and access logs easily.
Note: It's safe to remove the example virtual hosts that come by default.
$ sudo /opt/lampp/lampp restart
- Visit http://examplevhost.local and you should see the content of
/opt/lampp/htdocs/examplevhost/index.html
.
Use this(without comment line):
ServerName examplevhost.local
instead of:
ServerName examplevhost.local # entry in /etc/hosts
if you are getting this error:
AH00526: Syntax error on line 44 of /opt/lampp/etc/extra/httpd-vhosts.conf: ServerName takes one argument, The hostname and port of the server