Skip to content

Instantly share code, notes, and snippets.

@takshaktiwari
Last active October 31, 2024 00:30
Show Gist options
  • Save takshaktiwari/f8e5bd83cf5004e0909b70be6d567ccc to your computer and use it in GitHub Desktop.
Save takshaktiwari/f8e5bd83cf5004e0909b70be6d567ccc to your computer and use it in GitHub Desktop.
vHosts in LAMPP - Ubuntu

1. Allow the usage of custom virtual hosts

sudo gedit /opt/lampp/etc/httpd.conf

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

Uncomment above line to enable vhosts.

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

2. Create a custom domain in the hosts file of your system

sudo gedit /etc/hosts

127.0.0.1	localhost
127.0.0.2	myproject # the custom domain added

#don't touch other existent values
#The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

3. Create your first virtual host

sudo gedit /opt/lampp/etc/extra/httpd-vhosts.conf

<VirtualHost 127.0.0.2:80>
DocumentRoot "/opt/lampp/htdocs/myproject"
DirectoryIndex index.php

<Directory "/opt/lampp/htdocs/myproject">
    Options All
    AllowOverride All
    Require all granted
</Directory>
</VirtualHost>

4. Test your virtual host

To test it, in the folder /opt/lampp/htdocs/my-first-project, create a simple PHP file (index.php) that will contain the following PHP code:

<?php
    echo "Hello world!";
?>

Start apache, mysql (entire XAMPP) using the following command (or whatever the way you start apache and the other required services):

sudo /opt/lampp/lampp start

Navigate in your favorite browser to http://myproject/ or http://127.0.0.2/ and you should get as output "Hello World" in the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment