Skip to content
● Critical PHP security fixes are out: update PHP on your Grav sites. What to do →

Multisite setup [domains]

Started by Sergei Filatov 9 years ago · 10 replies · 3577 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 David Meissner, 9 minutes ago
0 2 9 minutes ago
Installation & Hosting · by David Meissner, 16 hours ago
0 25 16 hours ago
Installation & Hosting · by Anthony J Cervo, 5 days ago
3 117 3 days ago
Installation & Hosting · by motorambler, 2 months ago
9 790 4 weeks ago
Installation & Hosting · by jeremycherfas, 1 month ago
1 398 1 month ago