Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Installation & Hosting

Multisite setup [domains]

Started by Sergei Filatov 9 years ago · 10 replies · 3121 views
9 years ago

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
<?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! 😦

9 years ago

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/
9 years ago

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.

9 years ago

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

9 years ago

hi,
My setup.php is :

PHP
<?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?

9 years ago

Ok, i managed to get an error

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

with this code:

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, ':'));
$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}"],
              ]
          ]
        ]
    ]
];

9 years ago

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

last edited 09/28/17 by Sergei Filatov

Suggested topics

Topic Participants Replies Views Activity
Installation & Hosting · by antoinep, 15 hours ago
5 61 9 hours ago
Installation & Hosting · by Jürgen Dietrich, 7 months ago
0 63 7 months ago
Installation & Hosting · by rappluk, 8 months ago
0 62 8 months ago
Installation & Hosting · by N, 12 months ago
3 73 12 months ago
Installation & Hosting · by Youle, 1 year ago
1 62 1 year ago