Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mstdmstd's avatar

How to use subdomains in laravel app on local ubuntu?

Looking at https://laravel.com/docs/10.x/routing#route-group-subdomain-routing I try to make subdomains for my laravel 10 / livewire 3 app on my home kubuntu 22.04

In routes/web.php I added :

Route::domain('{company}.'.config('app.url'))->group(function () {
    Route::get('/', HomePage::Class);
    Route::get('/news-details-page/{slug}', NewsDetailsPage::Class)->name('news.details');
    Route::get('/news{news}', NewsListingPage::Class)->name('news.listing');
});

and in app/Livewire/HomePage.php :

class HomePage extends Component
{
    public string $companySlug = '';
    public $selectedCompany;

    public function mount(?string $companySlug = '')
    {
        $this->companySlug = $companySlug;
        $this->selectedCompany = Company::getBySlug($this->companySlug)->first();

        if (empty($this->selectedCompany)) {
            $this->selectedCompany = Company::getByActive(CompanyActiveEnum::ACTIVE)->first();
        }
        ...
    }

I run server with command :

php artisan serve

But url in browser

http://main-office.127.0.0.1:8000

where "main-office" is slug of one of companies.

does not work, browser use it for search.

In route aputputs I have :

GET|HEAD  {company}.127.0.0.1:8000/ ................................................................................................................................................ generated::UVdHOKqavkOQwvfV › App\Livewire\HomePage

GET|HEAD  {company}.127.0.0.1:8000/news-details-page/{slug} ................................................................................................................................ news.details › App\Livewire\NewsDetailsPage

GET|HEAD  {company}.127.0.0.1:8000/news{news} .............................................................................................................................................. news.listing › App\Livewire\NewsListingPage

Which way is correct? Have I to add any subdomain/company in /etc/hosts of my OS ?

In .env I changed :

APP_URL=localhost

and cleared cache

In browser try to run url

http://main-office.localhost

Then apache default page is opened

On

http://main-office.localhost:8000/

My App is opened but shows

404 NOT FOUND

error

Which way is correct ?

0 likes
11 replies
s4muel's avatar
  • edit your /etc/hosts file and put 127.0.0.1 yourapp.test in it
  • set the APP_URL like this: APP_URL=http://yourapp.test
  • start the server with hostname parameter php artisan serve --host yourapp.host

you should be fine with subdomains now

the yourapp.test hostname part is optional, it just makes it more clear. you just cannot use the 127.0.0.1 (ip) in combination with subdomains. so basically just set the APP_URL=localhost and start the server with a host name php artisan serve --host localhost

mstdmstd's avatar

@s4muel Do you mean only 1 row/new host in /etc/hosts or 1 row for any company ?

s4muel's avatar

@mstdmstd just one row for the domain and tld (yourapp.test in my exmple), that will match all subdomains (main-office.yourapp.test, secondary-office.yourapp.test, backup-office.yourapp.test...)

s4muel's avatar

@mstdmstd tweak your ServerAlias directive in the apache config

ServerAlias *.local-news-publishers.com
mstdmstd's avatar

@s4muel I modified a /etc/apache2/sites-available/000-default.conf file adding a line in the end :

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

ServerAlias *.local-news-publishers.com

Is it what you mean ?

But restarting apache I got errors :

root@master-at-home:/etc/php/8.2/apache2/conf.d# sudo service apache2 restart
Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xeu apache2.service" for details.
root@master-at-home:/etc/php/8.2/apache2/conf.d# systemctl status apache2.service
× apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Thu 2024-03-28 09:07:44 EET; 7s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 18245 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE)
        CPU: 25ms

бер 28 09:07:44 master-at-home systemd[1]: Starting The Apache HTTP Server...
бер 28 09:07:44 master-at-home apachectl[18249]: AH00526: Syntax error on line 33 of /etc/apache2/sites-enabled/000-default.conf:
бер 28 09:07:44 master-at-home apachectl[18249]: ServerAlias only used in <VirtualHost>
бер 28 09:07:44 master-at-home apachectl[18245]: Action 'start' failed.
бер 28 09:07:44 master-at-home apachectl[18245]: The Apache error log may have more information.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Failed with result 'exit-code'.
бер 28 09:07:44 master-at-home systemd[1]: Failed to start The Apache HTTP Server.
root@master-at-home:/etc/php/8.2/apache2/conf.d# journalctl -xeu apache2.service
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit apache2.service has entered the 'failed' state with result 'exit-code'.
бер 28 09:07:44 master-at-home systemd[1]: Stopped The Apache HTTP Server.
░░ Subject: A stop job for unit apache2.service has finished
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A stop job for unit apache2.service has finished.
░░
░░ The job identifier is 5728 and the job result is done.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Consumed 29.561s CPU time.
░░ Subject: Resources consumed by unit runtime
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit apache2.service completed and consumed the indicated resources.
бер 28 09:07:44 master-at-home systemd[1]: Starting The Apache HTTP Server...
░░ Subject: A start job for unit apache2.service has begun execution
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A start job for unit apache2.service has begun execution.
░░
░░ The job identifier is 5728.
бер 28 09:07:44 master-at-home apachectl[18249]: AH00526: Syntax error on line 33 of /etc/apache2/sites-enabled/000-default.conf:
бер 28 09:07:44 master-at-home apachectl[18249]: ServerAlias only used in <VirtualHost>
бер 28 09:07:44 master-at-home apachectl[18245]: Action 'start' failed.
бер 28 09:07:44 master-at-home apachectl[18245]: The Apache error log may have more information.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ An ExecStart= process belonging to unit apache2.service has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 1.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit apache2.service has entered the 'failed' state with result 'exit-code'.
бер 28 09:07:44 master-at-home systemd[1]: Failed to start The Apache HTTP Server.
░░ Subject: A start job for unit apache2.service has failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A start job for unit apache2.service has finished with a failure.
░░
░░ The job identifier is 5728 and the job result is failed.
...skipping...
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit apache2.service has entered the 'failed' state with result 'exit-code'.
бер 28 09:07:44 master-at-home systemd[1]: Stopped The Apache HTTP Server.
░░ Subject: A stop job for unit apache2.service has finished
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A stop job for unit apache2.service has finished.
░░
░░ The job identifier is 5728 and the job result is done.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Consumed 29.561s CPU time.
░░ Subject: Resources consumed by unit runtime
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit apache2.service completed and consumed the indicated resources.
бер 28 09:07:44 master-at-home systemd[1]: Starting The Apache HTTP Server...
░░ Subject: A start job for unit apache2.service has begun execution
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A start job for unit apache2.service has begun execution.
░░
░░ The job identifier is 5728.
бер 28 09:07:44 master-at-home apachectl[18249]: AH00526: Syntax error on line 33 of /etc/apache2/sites-enabled/000-default.conf:
бер 28 09:07:44 master-at-home apachectl[18249]: ServerAlias only used in <VirtualHost>
бер 28 09:07:44 master-at-home apachectl[18245]: Action 'start' failed.
бер 28 09:07:44 master-at-home apachectl[18245]: The Apache error log may have more information.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ An ExecStart= process belonging to unit apache2.service has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 1.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit apache2.service has entered the 'failed' state with result 'exit-code'.
бер 28 09:07:44 master-at-home systemd[1]: Failed to start The Apache HTTP Server.
░░ Subject: A start job for unit apache2.service has failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A start job for unit apache2.service has finished with a failure.
░░
░░ The job identifier is 5728 and the job result is failed.
...skipping...
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit apache2.service has entered the 'failed' state with result 'exit-code'.
бер 28 09:07:44 master-at-home systemd[1]: Stopped The Apache HTTP Server.
░░ Subject: A stop job for unit apache2.service has finished
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A stop job for unit apache2.service has finished.
░░
░░ The job identifier is 5728 and the job result is done.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Consumed 29.561s CPU time.
░░ Subject: Resources consumed by unit runtime
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit apache2.service completed and consumed the indicated resources.
бер 28 09:07:44 master-at-home systemd[1]: Starting The Apache HTTP Server...
░░ Subject: A start job for unit apache2.service has begun execution
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A start job for unit apache2.service has begun execution.
░░
░░ The job identifier is 5728.
бер 28 09:07:44 master-at-home apachectl[18249]: AH00526: Syntax error on line 33 of /etc/apache2/sites-enabled/000-default.conf:
бер 28 09:07:44 master-at-home apachectl[18249]: ServerAlias only used in <VirtualHost>
бер 28 09:07:44 master-at-home apachectl[18245]: Action 'start' failed.
бер 28 09:07:44 master-at-home apachectl[18245]: The Apache error log may have more information.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ An ExecStart= process belonging to unit apache2.service has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 1.
бер 28 09:07:44 master-at-home systemd[1]: apache2.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit apache2.service has entered the 'failed' state with result 'exit-code'.
бер 28 09:07:44 master-at-home systemd[1]: Failed to start The Apache HTTP Server.
░░ Subject: A start job for unit apache2.service has failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A start job for unit apache2.service has finished with a failure.
░░
░░ The job identifier is 5728 and the job result is failed.
lines 1547-1600/1600 (END)

Which options are valid ?

s4muel's avatar

@mstdmstd no, the ServerAlias directive should be in the same place from your first example

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /_wwwroot/lar/NewsPublisher/public
    ServerName local-news-publishers.com
    ServerAlias *.local-news-publishers.com

    <Directory /_wwwroot/lar/NewsPublisher/public>
    AllowOverride All
    Order Deny,Allow
    Allow from all
    Require all granted
    </Directory>

    Options FollowSymLinks
    DirectoryIndex index.php

    ErrorLog /_wwwroot/lar/NewsPublisher/storage/logs/error.log
    CustomLog /_wwwroot/lar/NewsPublisher/storage/logs/access.log combined
</VirtualHost>
mstdmstd's avatar

@s4muel With this option I got 404 NOT FOUND error Above I show routes/web.php of the app

s4muel's avatar

@mstdmstd since you change your examples. i am not sure what you put where. anyway, just start with minimum working setup and work the way further up:

1 - do not use the php artisan serve, use apache as you later wrote you use.

2 - set up the apache config as from my example above (seems you have done this correctly, since you are getting 404 NOT FOUND and not a DNS_PROBE_FINISHED_NXDOMAIN in browser)

3 - forget about the app_urr setting for now and hardcode the domain and a testing route in routes/web.php file something like this:

main-office.local-news-publishers.com
Route::domain('{company}.local-news-publishers.com')->group(function () {
    Route::get('test', function (string $company) {
        dd($company);
    });
});

visit main-office.local-news-publishers.com in your browser and check whether it works. if so, try using the app url from config. if that works, try using one of your existing routes, etc. baby steps

mstdmstd's avatar

@s4muel Noit does not work. I still got DNS_PROBE_FINISHED_NXDOMAIN. Are there something else I have to pay attention at ?

mstdmstd's avatar

I added new hosting with /etc/apache2/sites-available/local-news-publishers.com.conf file :

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /_wwwroot/lar/NewsPublisher/public
    ServerName local-news-publishers.com
    ServerAlias local-news-publishers.com

    <Directory /_wwwroot/lar/NewsPublisher/public>
    AllowOverride All
    Order Deny,Allow
    Allow from all
    Require all granted
    </Directory>

    Options FollowSymLinks
    DirectoryIndex index.php

    ErrorLog /_wwwroot/lar/NewsPublisher/storage/logs/error.log
    CustomLog /_wwwroot/lar/NewsPublisher/storage/logs/access.log combined
</VirtualHost>

and added line in /etc/hosts :

127.0.0.1	localhost
127.0.1.1	master-at-home

# 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


...

127.0.0.7  local-quizzes.com

127.0.0.8   local-news-publishers.com

in .env of the app :

    APP_URL=http://local-news-publishers.com

I restarted apache and clear cache of the app.

But running in my browser :

http://main-office.local-news-publishers.com

I got error in my browser :

This site can’t be reachedCheck if there is a typo in main-office.local-news-publishers.com.
DNS_PROBE_FINISHED_NXDOMAIN

But As I wrote above :

On

http://main-office.localhost:8000/

My app was opened but shows app error :

404 NOT FOUND

Which setting have I to use ?

Please or to participate in this conversation.