Skip to content

Commit

Permalink
Merge pull request #21 from hughbris/multisite-subdir
Browse files Browse the repository at this point in the history
Add support for multisite installs using subdirectories
  • Loading branch information
dsavell committed Jan 9, 2021
2 parents eec2532 + 27d6934 commit 9ccc339
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile.gravcore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ RUN \
EXPOSE 80 443

COPY root/ /
COPY env/ /tmp/env/

WORKDIR /var/www/grav

Expand Down
1 change: 1 addition & 0 deletions Dockerfile.gravcoreadmin
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ EXPOSE 80 443
COPY root/ /

WORKDIR /var/www/grav
COPY env/ /tmp/env/

CMD ["/init-admin"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ A Docker image based on minideb:buster linux with Grav CMS and PHP7.3/nginx.
+ nginx
+ GRAV Core
+ GRAV Admin Plugin
+ optional [multisite installation](https://learn.getgrav.org/16/advanced/multisite-setup) using subdirectories

## Usage

Expand All @@ -44,6 +45,8 @@ docker create \
docker start grav
```

> Use the `-e GRAV_MULTISITE=dir` flag for a Grav multisite installation using subdirectories (see below). Multisite using subdomains is not yet supported.
## Tags

[latest, core, 1.6.28, core-1.6.28 (Dockerfile)](https://github.com/dsavell/docker-grav/blob/master/Dockerfile.gravcore)
Expand All @@ -70,6 +73,7 @@ Container images are configured using parameters passed at runtime (such as thos
| `-p 80` | http |
| `-e DUID=1000` | for UserID - see below for explanation |
| `-e DGID=1000` | for GroupID - see below for explanation |
| `-e GRAV_MULTISITE=dir` | Deploy a Grav multisite (subdirectory) installation |
| `-v /var/www/backup` | Contains your location for Grav backups |
| `-v /var/www/logs` | Contains your location for your Grav log files |
| `-v /var/www/user` | Contains your Grav content |
Expand Down
40 changes: 40 additions & 0 deletions env/setup_subdirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Multisite setup for sub-directories or path based
* URLs for subsites.
*
* DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!
*/

use Grav\Common\Filesystem\Folder;

// Get relative path from Grav root.
$path = isset($_SERVER['PATH_INFO'])
? $_SERVER['PATH_INFO']
: Folder::getRelativePath($_SERVER['REQUEST_URI'], ROOT_DIR);

// Extract name of subsite from path
$name = Folder::shift($path);
$folder = "sites/{$name}";
$prefix = "/{$name}";

if (!$name || !is_dir(ROOT_DIR . "user/{$folder}")) {
return [];
}

// Prefix all pages with the name of the subsite
$container['pages']->base($prefix);

return [
'environment' => $name,
'streams' => [
'schemes' => [
'user' => [
'type' => 'ReadOnlyStream',
'prefixes' => [
'' => ["user/{$folder}"],
]
]
]
]
];
34 changes: 34 additions & 0 deletions env/setup_subdomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Multisite setup for subsites accessible via sub-domains.
*
* DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!
*/

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

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

return [
'environment' => $environment,
'streams' => [
'schemes' => [
'user' => [
'type' => 'ReadOnlyStream',
'prefixes' => [
'' => ["user/{$folder}"],
]
]
]
]
];
9 changes: 9 additions & 0 deletions root/init-admin
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,18 @@ if [[ ! -e /var/www/grav/user/config/site.yaml ]]; then
cp -r /grav/grav-admin/user /var/www/grav
fi

# check for supported GRAV_MULTISITE setting
echo "Checking for multisite environment .."
if [[ ! -z "${GRAV_MULTISITE}" && "$GRAV_MULTISITE" == "dir" ]]; then
echo "Copying multisite setup.php .."
cp /tmp/env/setup_subdirectory.php /var/www/grav/setup.php
mkdir -p /var/www/grav/user/sites
fi

# Clean
rm -rf /grav
rm -rf /var/www/html
rm -rf /tmp/env

DUID=${DUID:-911}
DGID=${DGID:-911}
Expand Down
9 changes: 9 additions & 0 deletions root/init-core
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,18 @@ if [[ ! -e /var/www/grav/user/config/site.yaml ]]; then
cp -r /grav/grav/user /var/www/grav
fi

# check for supported GRAV_MULTISITE setting
echo "Checking for multisite environment .."
if [[ ! -z "${GRAV_MULTISITE}" && "$GRAV_MULTISITE" == "dir" ]]; then
echo "Copying multisite setup.php .."
cp /tmp/env/setup_subdirectory.php /var/www/grav/setup.php
mkdir -p /var/www/grav/user/sites
fi

# Clean
rm -rf /grav
rm -rf /var/www/html
rm -rf /tmp/env

DUID=${DUID:-911}
DGID=${DGID:-911}
Expand Down

0 comments on commit 9ccc339

Please sign in to comment.