Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setup-domain-multistore script #697

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,34 @@ bin/restart
open https://magento.test
```

#### Existing multistore projects
```bash

# Run bin/n98-magerun2 at least once in order to download the file
bin/n98-magerun2

# Creates a nginx mapping file and updates the base URLs in the database table `core_config_data` for each store.
# Admin panel base URL: https://magento.test
# Base URLs of each store: https://magento-store-code.test where store-code is the value found in `store.code` database column.
# e.g. two stores with the following codes: `en_US` and `fr_FR` will have base URLs: `https://magento-en-us.test` and `https://magento-fr-fr.test`
# Note that since we need to write `/etc/hosts` for DNS resolution, you will be prompted for your system password.
bin/setup-multistore


# If using Linux uncomment mount bindings in `docker-compose.dev.linux.yml` file and copy the file to `docker-compose.dev.yml`
sed -i 's/# - .\/config\/nginx\/nginx.conf/- .\/config\/nginx\/nginx.conf/' docker-compose.dev-linux.yml
sed -i 's/# - .\/config\/nginx\/default.conf/- .\/config\/nginx\/default.conf/' docker-compose.dev-linux.yml
cp docker-compose.dev-linux.yml docker-compose.dev.yml

# If using macOS (un)comment mount bindings in `docker-compose.dev.yml` file
sed -i 's/- .\/src\/nginx.conf.sample/#- .\/src\/nginx.conf.sample/' docker-compose.dev.yml
sed -i 's/# - .\/config\/nginx\/nginx.conf/- .\/config\/nginx\/nginx.conf/' docker-compose.dev.yml
sed -i 's/# - .\/config\/nginx\/default.conf/- .\/config\/nginx\/default.conf/' docker-compose.dev.yml

# Restart containers
bin/restart
```

## Updates

To update your project to the latest version of `docker-magento`, run:
Expand Down
2 changes: 2 additions & 0 deletions compose/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
src/
config/nginx/nginx.conf
config/nginx/default.conf
24 changes: 24 additions & 0 deletions compose/bin/add-base-urls-to-hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

STORES=$(bin/n98-magerun2 sys:store:list --format=csv | grep -v Warning | sed '/^$/d' | tail -n+2)

NEWLINE_PRINTED=false
for STORE in $STORES; do
SEPARATOR=""
STORE_CODE=""
STORE_ID=$(echo "$STORE" | cut -d ',' -f1)
if [[ ${STORE_ID} -gt 1 ]]; then
SEPARATOR="-"
STORE_CODE=$(echo "$STORE" | cut -d ',' -f2)
STORE_CODE="${STORE_CODE//_/-}"
fi
HOSTS_URL=magento"${SEPARATOR}""${STORE_CODE}".test
URL_IN_HOSTS_FILE=$(grep -c "$HOSTS_URL" /etc/hosts)
if [[ $URL_IN_HOSTS_FILE -eq 0 ]]; then
if [[ $NEWLINE_PRINTED == false ]]; then
echo | sudo tee -a /etc/hosts
NEWLINE_PRINTED=true
fi
echo 127.0.0.1"$(printf "\t")""$HOSTS_URL" | sudo tee -a /etc/hosts
fi
done
15 changes: 15 additions & 0 deletions compose/bin/generate-nginx-mapping
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e

STORES=$(bin/n98-magerun2 sys:store:list --format=csv | grep -v Warning | sed '/^$/d' | tail -n+2)

printf "map \$http_host \$MAGE_RUN_CODE {\n"
printf ' %s\n' "default $(echo "$STORES" | head -n1 | cut -d ',' -f2)"';'

for STORE in $STORES; do
STORE_CODE=$(echo "$STORE" | cut -d ',' -f2)
STORE_WITH_DASHES=${STORE_CODE//_/-}
printf ' %s\n' 'magento-'"$STORE_WITH_DASHES"'.test '"$STORE_CODE"';'
done

printf '%s\n' "}"
62 changes: 62 additions & 0 deletions compose/bin/generate-store-setters
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
set -e

STORES=$(bin/n98-magerun2 sys:store:list --format=csv | grep -v Warning | sed '/^$/d' | tail -n+2)

# echo "use magento;"
echo "DELETE FROM core_config_data WHERE path LIKE 'web/%/base%url';"

for STORE in $STORES; do
STORE_CODE=""
SEPARATOR=""
SCOPE="default"
STORE_ID=$(echo "$STORE" | cut -d ',' -f1)
if [[ ${STORE_ID} -gt 1 ]]; then
SEPARATOR="-"
STORE_CODE=$(echo "$STORE" | cut -d ',' -f2)
STORE_CODE=${STORE_CODE//_/-}
SCOPE="stores"
fi
SECURE_BASE_URL="https://magento${SEPARATOR}${STORE_CODE}.test/"
UNSECURE_BASE_URL="https://magento${SEPARATOR}${STORE_CODE}.test/"

SECURE_BASE_LINK_URL="https://magento${SEPARATOR}${STORE_CODE}.test/"
UNSECURE_BASE_LINK_URL="https://magento${SEPARATOR}${STORE_CODE}.test/"

SECURE_BASE_MEDIA_URL=$SECURE_BASE_URL"media/"
UNSECURE_BASE_MEDIA_URL=$UNSECURE_BASE_URL"media/"

SECURE_BASE_STATIC_URL=$SECURE_BASE_URL"static/"
UNSECURE_BASE_STATIC_URL=$UNSECURE_BASE_URL"static/"

SQL_SECURE_BASE_URL="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('${SECURE_BASE_URL}', 'web/secure/base_url', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_SECURE_BASE_URL"

SQL_UNSECURE_BASE_URL="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('${UNSECURE_BASE_URL}', 'web/unsecure/base_url', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_UNSECURE_BASE_URL"

SQL_SECURE_BASE_LINK_URL="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('${SECURE_BASE_LINK_URL}', 'web/secure/base_link_url', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_SECURE_BASE_LINK_URL"

SQL_UNSECURE_BASE_LINK_URL="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('${UNSECURE_BASE_LINK_URL}', 'web/unsecure/base_link_url', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_UNSECURE_BASE_LINK_URL"

SQL_SECURE_BASE_MEDIA_URL="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('${SECURE_BASE_MEDIA_URL}', 'web/secure/base_media_url', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_SECURE_BASE_MEDIA_URL"

SQL_UNSECURE_BASE_MEDIA_URL="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('${UNSECURE_BASE_MEDIA_URL}', 'web/unsecure/base_media_url', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_UNSECURE_BASE_MEDIA_URL"

SQL_SECURE_BASE_STATIC_URL="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('${SECURE_BASE_STATIC_URL}', 'web/secure/base_static_url', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_SECURE_BASE_STATIC_URL"

SQL_UNSECURE_BASE_STATIC_URL="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('${UNSECURE_BASE_STATIC_URL}', 'web/unsecure/base_static_url', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_UNSECURE_BASE_STATIC_URL"

SQL_WEB_COOKIE_DOMAIN="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('${UNSECURE_BASE_URL}', 'web/cookie/cookie_domain', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_WEB_COOKIE_DOMAIN"

SQL_WEB_COOKIE_PATH="REPLACE INTO core_config_data (value, path, scope_id, scope) VALUES ('/', 'web/cookie/cookie_path', '${STORE_ID}', '${SCOPE}');"
echo "$SQL_WEB_COOKIE_PATH"

done
7 changes: 7 additions & 0 deletions compose/bin/setup-domain-multistore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# shellcheck source=../env/db.env
source env/db.env
bin/generate-store-setters | bin/mysql
bin/update-nginx-config
bin/add-base-urls-to-hosts
5 changes: 5 additions & 0 deletions compose/bin/update-nginx-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -e

bin/generate-nginx-mapping >config/nginx/default.conf
cd config/nginx && cat default.conf.multistore.sample >>default.conf && cp nginx.conf.multistore.sample nginx.conf
6 changes: 4 additions & 2 deletions compose/compose.dev-linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ version: "3"
services:
app:
volumes: &appvolumes
- ./src/nginx.conf.sample:/var/www/html/nginx.conf:cached
- ./src:/var/www/html:cached
# If you want to run a multistore Magento installation uncomment the two lines below and restart the app service
# - ./config/nginx/nginx.conf:/var/www/html/nginx.conf:delegated
# - ./config/nginx/default.conf:/etc/nginx/conf.d/default.conf:delegated
- ./src:/var/www/html

phpfpm:
volumes: *appvolumes
3 changes: 3 additions & 0 deletions compose/compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ services:
- ./src/dev/tools/grunt/configs:/var/www/html/dev/tools/grunt/configs:cached
- ./src/nginx.conf.sample:/var/www/html/nginx.conf:cached
- ./src/package.json.sample:/var/www/html/package.json:cached
# If you want to run a multistore Magento installation uncomment the two lines below, comment the one above and restart the app service
# - ./config/nginx/nginx.conf:/var/www/html/nginx.conf:delegated
# - ./config/nginx/default.conf:/etc/nginx/conf.d/default.conf:delegated
#- ./src/auth.json:/var/www/html/auth.json:cached
#- ./src/m2-hotfixes:/var/www/html/m2-hotfixes:cached
#- ./src/patches:/var/www/html/patches:cached
Expand Down
23 changes: 23 additions & 0 deletions compose/config/nginx/default.conf.multistore.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
upstream fastcgi_backend {
server unix:/sock/docker.sock;
}

server {
listen 8000;
return 301 https://$host$request_uri;
}

server {
listen [::]:8443 ssl http2 ipv6only=on;
listen 8443 ssl http2;

ssl_certificate /etc/nginx/certs/nginx.crt;
ssl_certificate_key /etc/nginx/certs/nginx.key;

set $MAGE_ROOT /var/www/html;

fastcgi_buffer_size 64k;
fastcgi_buffers 8 128k;

include /var/www/html/nginx[.]conf;
}
Loading