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

Multisite setup [domains]

Started by Sergei Filatov 9 years ago · 10 replies · 3295 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

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}"],
              ]
          ]
        ]
    ]
];

Suggested topics

Topic Participants Replies Views Activity
Installation & Hosting · by antoinep, 1 month ago
8 603 1 month ago
Installation & Hosting · by Jürgen Dietrich, 8 months ago
0 368 8 months ago
Installation & Hosting · by rappluk, 9 months ago
0 421 9 months ago
Installation & Hosting · by N, 1 year ago
3 350 1 year ago
Installation & Hosting · by Youle, 1 year ago
1 279 1 year ago