Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Multisite setup [domains]

Started by Sergei Filatov 9 years ago · 10 replies · 3391 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 jeremycherfas, 24 hours ago
1 49 20 hours ago
Installation & Hosting · by motorambler, 2 weeks ago
8 359 6 days ago
Installation & Hosting · by antoinep, 3 months ago
8 938 3 months ago
Installation & Hosting · by Jürgen Dietrich, 9 months ago
0 602 9 months ago
Installation & Hosting · by rappluk, 11 months ago
0 647 11 months ago