How to configure a subdomain in xampp for linux OS (ubuntu, etc ) for your laravel/php application

This solution is for xampp users on linux(local)OS (ubuntu, etc) Let me just mention that, I researched alot of articles and Stackoverflow answers about this problem but none provided a solution peculiar to linux os / ubuntu. So, yeah, I had to gather my solution which were in bits in one article, this article !

Lets begin...
Normally, for your laravel application, you can access the traditional domain when you run php artisan serve,

http://127.0.0.1:8000 //  or http://127.0.0.1:8081 etc

Now, imagine we need to locally run a part of our laravel app feature on a subdomain say, Admin, so we should have something like admin.site.com/, right ?

Two basic things we are going to do;

  1. We will edit the linux host file so we can point our subdomain names to the default IP address.
  2. Then we will create a virtual host in a xammp file.

Editing hosts file

Go to your computer (root folder of your linux file manager, usually named "computer", open the etc folder, then inside that folder look for the file named, hosts.

Right click on the hosts file then open it up with a text editor (I will recommend vscode or anyother text editor that lets you edit the file with an interface to enter your password incase that file requires a super or an admin user permissions)

Upon opening of hosts file, you should see something like this :

Next, Unlike the screenshot above, in our own situation, we assign site to 127.0.0.1 and same thing for admin.site to 127.0.0.1

127.0.0.1 site
127.0.0.1 admin.site

Make sure you save the changes (I beg of you !...lol )

Let's move...Next !

Creating a virtual host

To create a virtual host, the files we will be editing are httpd-vhosts.conf and httpd.conf

We can locate httpd-vhosts.conf in this path opt\lamp\etc\extra\httpd-vhosts.conf

In that file we create two copies for virtual host using the code snippet like mine :

//for site (root domain)
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/opt/lampp/htdocs/laravel/siteproject/public"
    ServerName site
    ServerAlias *.site
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common
    <Directory "/opt/lampp/htdocs/laravel/siteproject/public">
      Require all granted
    </Directory>
</VirtualHost>

//for admin.site (subdomain)
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/opt/lampp/htdocs/laravel/siteproject/public"
    ServerName admin.site
    ServerAlias www.admin.site
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common
    <Directory "/opt/lampp/htdocs/laravel/siteproject/public">
      Require all granted
    </Directory>
</VirtualHost>

Please note the following:

The DocumentRoot variable or section in side the virtualHost tag should point to the specific folder where your laravel project is; and yes, even to the public folder of your laravel projec too

"/opt/lampp/htdocs/laravel/siteproject/public"

The ServerName refers to the domains we want to use.

Lastly, we edit httpd.conf. The likely path should be,opt\lamp\etc\httpd.conf

The only thing we will do here is to uncomment this line # Include etc/extra/httpd-vhosts.conf

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

Restart xammp

For my own machine, I use this command to restart xammp
sudo /opt/lampp/lampp restart. So restart your xammp in your preferred way.

Then in your web.php file :

Route::domain('admin.explore')->group(function () {
      Route::get('/', function () {
        return "I will only trigger when domain is admin.explore.";
    });
});


Route::get('/', function () {
    return view('welcome');
});

_ Heeeeeeeeeyyyyyyyyy....Note one veeeeerrrrrryyyy Important thing: _

In order to ensure your subdomain routes are reachable, you should register subdomain routes before registering root domain routes. This will prevent root domain routes from overwriting subdomain routes which have the same URI path.

_The following paragraph explains the quote above but if you do understand, you can skip this part.
_

The quoted laravel documentation simple implies that you place (register) your subdomain routes right above any root domain route.

What you should NOT do :

//Rooot Domain route(s)
    Route::get('/', function () {
    return view('welcome'); 
});

//Sub Domain route(s)

Route::domain('admin.site')->group(function () {
      Route::get('/', function () {
        return "I will only trigger when domain is admin.site.";
    });
});

What you should DO :

//Sub Domain route(s)

Route::domain('admin.site')->group(function () {
      Route::get('/', function () {
        return "I will only trigger when domain is admin.site.";
    });
});

//Root Domain route(s)
    Route::get('/', function () {
    return view('welcome'); 
});

This is my answer from a Stackoverflow question

Pheeeew.... Glad you were able to make it to this extent... I will be super glad to know this article helped you !

48