Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the steps to set up multiple subdomains using Laravel Forge
    Laravel Forge


What are the steps to set up multiple subdomains using Laravel Forge


Here are the steps to set up multiple subdomains using Laravel Forge:

1. Create a New Site in Laravel Forge:
- Log in to your Laravel Forge account.
- Click on the "Create Site" button.
- Enter the primary domain for your application (e.g., `myapp.com`).

2. Configure Subdomain Routing in Laravel:
- In your Laravel application, open the `app/Providers/RouteServiceProvider.php` file.
- Update the `mapWebRoutes()` method to handle subdomain routing:

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

- Create separate route files for each subdomain (e.g., `routes/admin.php`, `routes/api.php`) and update the `RouteServiceProvider` accordingly.

3. Configure Nginx in Laravel Forge:
- In the Laravel Forge dashboard, navigate to the "Sites" section.
- Click on the site you created earlier.
- Click on the "Nginx" tab.
- In the "Server Blocks" section, click on the "Add Server Block" button.
- Enter the subdomain you want to configure (e.g., `admin.myapp.com`).
- Update the Nginx configuration as needed, such as setting the document root and index files.
- Repeat this process for each additional subdomain you want to set up.

4. Configure DNS:
- In your domain registrar's DNS settings, add A records for each subdomain pointing to the IP address of your server.

5. Test the Subdomains:
- Visit each subdomain (e.g., `admin.myapp.com`, `api.myapp.com`) to ensure they are working as expected.

By following these steps, you can set up multiple subdomains using Laravel Forge. The key points are:

1. Configure subdomain routing in your Laravel application's `RouteServiceProvider`.
2. Set up the Nginx server blocks for each subdomain in Laravel Forge.
3. Update the DNS records to point the subdomains to your server.
4. Test the subdomains to ensure they are working correctly.

This approach allows you to manage multiple subdomains within a single Laravel application using Laravel Forge for easy deployment and configuration.

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