Skip to content

Instantly share code, notes, and snippets.

@brenopolanski
Forked from ibrahimtuzlak0295/xampp-virtualhost.md
Created September 27, 2020 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brenopolanski/23eec857242f03b40a61daa031d3f5fd to your computer and use it in GitHub Desktop.
Save brenopolanski/23eec857242f03b40a61daa031d3f5fd to your computer and use it in GitHub Desktop.
Create virtual host in XAMPP, Ubuntu 16.04/18.04/20.04

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.

Enable virtual hosts in apache configuration file

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

Create the files

$ mkdir /opt/lampp/htdocs/examplevhost
$ gedit /opt/lampp/htdocs/examplevhost/index.html

Add the host to /etc/hosts

$ sudo gedit /etc/hosts
  • Add this line to the bottom of the file:
127.0.1.1	examplevhost.local

Add the host to /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>
    ServerAdmin ibrahimtuzlak.0295@gmail.com
    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 and ServerName (ServerName must exist as an entry in /etc/hosts).

Note: It's safe to remove the example virtual hosts that come by default.

Test!

$ sudo /opt/lampp/lampp restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment