Multisite setup [domains]

The case:

I have multiple domains: A,B,C with pointers to domain D.
Domain D has Grav with folder user/sites/A; user/sites/B; user/sites/C; user/sites/D;

The theme, plugins folders for ALL domains are the same, everything else is different: pages, config, etc.

What is the correct setup.php?

Currently i have, but not working:

<?php

use Grav\Common\Utils;

// Get subsite name from sub-domain
$environment = isset($_SERVER['HTTP_HOST'])
    ? $_SERVER['HTTP_HOST']
    : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');
// Remove port from HTTP_HOST generated $environment
$environment = strtolower(Utils::substrToString($environment, ':'));
$folder = "sites/{$environment}";

$themes = ["user/themes"];
if (is_dir(__DIR__ . "/user/{$folder}/themes")) {
    array_unshift($themes, "user/{$folder}/themes");
}

$plugins = ["user/plugins"];
if (is_dir(__DIR__ . "/user/{$folder}/plugins")) {
    array_unshift($plugins, "user/{$folder}/plugins");
}


if ($environment === 'localhost' || !is_dir(ROOT_DIR . "user/{$folder}")) {
     return [];
}

return [
    'environment' => $environment,
    'streams' => [
        'schemes' => [
            'user' => [
               'type' => 'ReadOnlyStream',
               'prefixes' => [
                   '' => ["user/{$folder}"],
               ]
            ],
            'environment' => [
                'type' => 'ReadOnlyStream',
                'prefixes' => [
                    '' => ["user/{$folder}"],
                ]
            ],
            'themes' => [
                'type' => 'ReadOnlyStream',
                'prefixes' => [
                    '' => $themes
                ]
            ],
            'plugins' => [
                'type' => 'ReadOnlyStream',
                'prefixes' => [
                    '' => $plugins
                ]
            ],
            'plugin' => [
                'type' => 'ReadOnlyStream',
                'prefixes' => [
                    '' => ['plugins://']
                ]
            ]
        ]
    ]
];

Please HEEELP! :frowning:

Hello,
How did you call your folder?
They must match the domain.
for example for site A :

  • sitea.domain.tdl
  • /user/site/sitea.domain.tdl/

Have you read the information about which objects to debug can help you?

I’m testing on localhost, environment name is localhost, the subdirectory of Sites has folder “localhost”, but grav still trying to reach the config, pages, templates inside Users folder.

Yes, but it did not help a lots. Definitely i’m doing something wrong, but i can’t understand what…

Check out this screenshot.

hi,
My setup.php is :

<?php
    use Grav\Common\Utils;
    
    $environment = isset($_SERVER['HTTP_HOST'])
        ? $_SERVER['HTTP_HOST']
        : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');

    $environment = strtolower(Utils::substrToString($environment, ':'));
    $folder = "sites/{$environment}";

    if ($environment === 'localhost' || !is_dir(ROOT_DIR . "user/{$folder}")) {
            return [];
        }

    return [
        'environment' => $environment,
        'streams' => [
            'schemes' => [
                'user' => [
                    'type' => 'Stream',
                    'prefixes' => [
			            '' => ["user/{$folder}"],
		            ]
                ],
                'cache' => [
                    'type' => 'Stream',
                    'prefixes' => [
                        '' => ["user/{$folder}/cache"],
                    ]
                ],
        ]
    ];
    echo $folder;
?>

On the other hand I never put it into production but it worked. There can be better.

You have your vhost of the same name as your folder in user, which point to the root of grav?

Ok, i managed to get an error

E_WARNING - array_keys() expects parameter 1 to be array, null given

with this code:


use Grav\Common\Utils;

// Get subsite name from sub-domain
$environment = isset($_SERVER['HTTP_HOST'])
    ? $_SERVER['HTTP_HOST']
    : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');
// Remove port from HTTP_HOST generated $environment
$environment = strtolower(Utils::substrToString($environment, ':'));
$subdir = current(explode('.', $environment));
$dir = "user/sites/{$subdir}";



$themes = "user/themes";
if (is_dir(__DIR__ . "/{$dir}/themes")) {
    $themes = "{$dir}/themes";
}

$plugins = "user/plugins";
if (is_dir(__DIR__ . "/{$dir}/plugins")) {
    $plugins = "{$dir}/plugins";
}

$accounts = "user/accounts";
if (is_dir(__DIR__ . "/{$dir}/accounts")) {
    $accounts = "{$dir}/accounts";
}

$config = "user/config";
if (is_dir(__DIR__ . "/{$dir}/config")) {
    $config = "{$dir}/config";
}

$pages = "user/pages";
if (is_dir(__DIR__ . "/{$dir}/pages")) {
    $pages = "{$dir}/pages";
}

// if ($environment === 'localhost' || !is_dir(ROOT_DIR . "user/{$folder}")) {
//     return [];
// }

return [
    'environment' => $environment,
    'streams' => [
        'schemes' => [
          'user' => [
              'type' => 'ReadOnlyStream',
              'prefixes' => [
                  '' => ["{$dir}"],
              ]
          ],
          'accounts' => [
              'type' => 'ReadOnlyStream',
              'prefixes' => [
                  '' => ["{$accounts}"],
              ]
          ],
          'config' => [
              'type' => 'ReadOnlyStream',
              'prefixes' => [
                  '' => ["{$config}"],
              ]
          ],
          'pages' => [
              'type' => 'ReadOnlyStream',
              'prefixes' => [
                  '' => ["{$pages}"],
              ]
          ],
          'plugins' => [
              'type' => 'ReadOnlyStream',
              'prefixes' => [
                  '' => ["{$plugins}"],
              ]
          ],
          'themes' => [
              'type' => 'ReadOnlyStream',
              'prefixes' => [
                  '' => ["{$themes}"],
              ]
          ]
        ]
    ]
];

Looks like something is missing in return[], any ideas?

I used apache to configure multiple domain mappings to each grav site.
https://httpd.apache.org/docs/current/vhosts/examples.html

I added an entry for each site and this allows all features of grav to work without modification. You will probably have admin issues doing it the way you have above.

What your configuration apache for vhost ?