Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I configure the webserver for multiple subdomains in Laravel
    Laravel Service Provider


How do I configure the webserver for multiple subdomains in Laravel


To configure the web server for multiple subdomains in Laravel, you need to set up the web server to route the subdomains to the appropriate Laravel application. Here are the steps for setting up multiple subdomains in Laravel using Nginx:

Step 1: Configure Nginx

1. Create a New Nginx Configuration File:

bash
   sudo nano /etc/nginx/sites-available/mysite
   

2. Configure Nginx:

nginx
   server {
       listen 80;
       server_name mysite.com www.mysite.com;
       root /var/www/mysite/public;

       index index.php index.html index.htm;

       location / {
           try_files $uri $uri/ /index.php?_share=++;
       }

       location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
           fastcgi_param SCRIPT_FILENAME $request_filename;
           include fastcgi_params;
       }
   }
   

3. Create a Link to the Configuration File:

bash
   sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
   

4. Restart Nginx:

bash
   sudo service nginx restart
   

Step 2: Configure Laravel

1. Update the `routes/web.php` File:

php
   Route::domain('mysite.com')->group(function () {
       // Routes for mysite.com
   });

   Route::domain('admin.mysite.com')->group(function () {
       // Routes for admin.mysite.com
   });

   Route::domain('api.mysite.com')->group(function () {
       // Routes for api.mysite.com
   });
   

2. Update the `RouteServiceProvider`:

php
   protected function mapWebRoutes()
   {
       Route::domain('mysite.com')->middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));
   }

   protected function mapAdminRoutes()
   {
       Route::domain('admin.mysite.com')->middleware('web')->namespace($this->namespace)->group(base_path('routes/admin.php'));
   }

   protected function mapApiRoutes()
   {
       Route::domain('api.mysite.com')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php'));
   }
   

Step 3: Configure DNS

1. Update the DNS Records:
- Create DNS records for each subdomain pointing to your server’s IP address.

Step 4: Test the Setup

1. Test the Setup:
- Visit each subdomain to ensure that the correct routes are being served.

By following these steps, you can set up multiple subdomains in Laravel using Nginx.

Citations:
[1] https://www.youtube.com/watch?v=iIDnoAkXMlE
[2] https://www.reddit.com/r/laravel/comments/lg2jqg/anyone_know_how_to_setup_multiple_subdomains_with/?rdt=64313
[3] https://pusher.com/blog/laravel-subdomain-routing/
[4] https://laracasts.com/discuss/channels/general-discussion/connecting-multiple-subdomains-to-single-laravel-project
[5] https://www.linkedin.com/pulse/how-setup-specific-subdomain-routing-laravel-adi-sk