From 71fa6f72992ecee6f4f980479f3fe997b71c4dff Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Tue, 26 Apr 2022 23:49:02 +0200 Subject: [PATCH 01/12] Add setup-domain-multistore script --- compose/.gitignore | 2 + compose/bin/add-base-urls-to-hosts | 21 ++ compose/bin/generate-nginx-mapping | 20 ++ compose/bin/generate-store-setters | 65 +++++ compose/bin/setup-domain-multistore | 6 + compose/bin/update-nginx-config | 5 + compose/config/nginx/default.conf.sample | 23 ++ compose/config/nginx/nginx.conf.sample | 247 ++++++++++++++++++ compose/config/nginx/nginx.conf.store.sample | 255 +++++++++++++++++++ compose/docker-compose.dev-linux.yml | 6 +- compose/sql/get_stores.sql | 30 +++ 11 files changed, 678 insertions(+), 2 deletions(-) create mode 100755 compose/bin/add-base-urls-to-hosts create mode 100755 compose/bin/generate-nginx-mapping create mode 100755 compose/bin/generate-store-setters create mode 100755 compose/bin/setup-domain-multistore create mode 100755 compose/bin/update-nginx-config create mode 100644 compose/config/nginx/default.conf.sample create mode 100644 compose/config/nginx/nginx.conf.sample create mode 100644 compose/config/nginx/nginx.conf.store.sample create mode 100644 compose/sql/get_stores.sql diff --git a/compose/.gitignore b/compose/.gitignore index 8eba6c8dd..f998215fd 100644 --- a/compose/.gitignore +++ b/compose/.gitignore @@ -1 +1,3 @@ src/ +config/nginx/nginx.conf +config/nginx/default.conf diff --git a/compose/bin/add-base-urls-to-hosts b/compose/bin/add-base-urls-to-hosts new file mode 100755 index 000000000..21e2e2f44 --- /dev/null +++ b/compose/bin/add-base-urls-to-hosts @@ -0,0 +1,21 @@ +#!/bin/bash + +STORES=$(bin/mysql config/nginx/default.conf +cd config/nginx && cat default.conf.sample >>default.conf && cp nginx.conf.store.sample nginx.conf diff --git a/compose/config/nginx/default.conf.sample b/compose/config/nginx/default.conf.sample new file mode 100644 index 000000000..4ef3f1dfe --- /dev/null +++ b/compose/config/nginx/default.conf.sample @@ -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; +} diff --git a/compose/config/nginx/nginx.conf.sample b/compose/config/nginx/nginx.conf.sample new file mode 100644 index 000000000..b2d6d29ee --- /dev/null +++ b/compose/config/nginx/nginx.conf.sample @@ -0,0 +1,247 @@ +## Example configuration: +# upstream fastcgi_backend { +# # use tcp connection +# # server 127.0.0.1:9000; +# # or socket +# server unix:/var/run/php/php7.4-fpm.sock; +# } +# server { +# listen 80; +# server_name mage.dev; +# set $MAGE_ROOT /var/www/magento2; +# set $MAGE_DEBUG_SHOW_ARGS 0; +# include /vagrant/magento2/nginx.conf.sample; +# } +# +## Optional override of deployment mode. We recommend you use the +## command 'bin/magento deploy:mode:set' to switch modes instead. +## +## set $MAGE_MODE default; # or production or developer +## +## If you set MAGE_MODE in server config, you must pass the variable into the +## PHP entry point blocks, which are indicated below. You can pass +## it in using: +## +## fastcgi_param MAGE_MODE $MAGE_MODE; +## +## In production mode, you should uncomment the 'expires' directive in the /static/ location block + +# Modules can be loaded only at the very beginning of the Nginx config file, please move the line below to the main config file +# load_module /etc/nginx/modules/ngx_http_image_filter_module.so; + +root $MAGE_ROOT/pub; + +index index.php; +autoindex off; +charset UTF-8; +error_page 404 403 = /errors/404.php; +#add_header "X-UA-Compatible" "IE=Edge"; + + +# Deny access to sensitive files +location /.user.ini { + deny all; +} + +# PHP entry point for setup application +location ~* ^/setup($|/) { + root $MAGE_ROOT; + location ~ ^/setup/index.php { + fastcgi_pass fastcgi_backend; + + fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off"; + fastcgi_param PHP_VALUE "memory_limit=756M \n max_execution_time=600"; + fastcgi_read_timeout 600s; + fastcgi_connect_timeout 600s; + + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; + } + + location ~ ^/setup/(?!pub/). { + deny all; + } + + location ~ ^/setup/pub/ { + add_header X-Frame-Options "SAMEORIGIN"; + } +} + +# PHP entry point for update application +location ~* ^/update($|/) { + root $MAGE_ROOT; + + location ~ ^/update/index.php { + fastcgi_split_path_info ^(/update/index.php)(/.+)$; + fastcgi_pass fastcgi_backend; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + include fastcgi_params; + } + + # Deny everything but index.php + location ~ ^/update/(?!pub/). { + deny all; + } + + location ~ ^/update/pub/ { + add_header X-Frame-Options "SAMEORIGIN"; + } +} + +location / { + try_files $uri $uri/ /index.php$is_args$args; +} + +location /pub/ { + location ~ ^/pub/media/(downloadable|customer|import|custom_options|theme_customization/.*\.xml) { + deny all; + } + alias $MAGE_ROOT/pub/; + add_header X-Frame-Options "SAMEORIGIN"; +} + +location /static/ { + # Uncomment the following line in production mode + # expires max; + + # Remove signature of the static files that is used to overcome the browser cache + location ~ ^/static/version { + rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last; + } + + location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|html|json)$ { + add_header Cache-Control "public"; + add_header X-Frame-Options "SAMEORIGIN"; + expires +1y; + + if (!-f $request_filename) { + rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last; + } + } + location ~* \.(zip|gz|gzip|bz2|csv|xml)$ { + add_header Cache-Control "no-store"; + add_header X-Frame-Options "SAMEORIGIN"; + expires off; + + if (!-f $request_filename) { + rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last; + } + } + if (!-f $request_filename) { + rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last; + } + add_header X-Frame-Options "SAMEORIGIN"; +} + +location /media/ { + +## The following section allows to offload image resizing from Magento instance to the Nginx. +## Catalog image URL format should be set accordingly. +## See https://docs.magento.com/user-guide/configuration/general/web.html#url-options +# location ~* ^/media/catalog/.* { +# +# # Replace placeholders and uncomment the line below to serve product images from public S3 +# # See examples of S3 authentication at https://github.com/anomalizer/ngx_aws_auth +# # resolver 8.8.8.8; +# # proxy_pass https://..amazonaws.com; +# +# set $width "-"; +# set $height "-"; +# if ($arg_width != '') { +# set $width $arg_width; +# } +# if ($arg_height != '') { +# set $height $arg_height; +# } +# image_filter resize $width $height; +# image_filter_jpeg_quality 90; +# } + + try_files $uri $uri/ /get.php$is_args$args; + + location ~ ^/media/theme_customization/.*\.xml { + deny all; + } + + location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ { + add_header Cache-Control "public"; + add_header X-Frame-Options "SAMEORIGIN"; + expires +1y; + try_files $uri $uri/ /get.php$is_args$args; + } + location ~* \.(zip|gz|gzip|bz2|csv|xml)$ { + add_header Cache-Control "no-store"; + add_header X-Frame-Options "SAMEORIGIN"; + expires off; + try_files $uri $uri/ /get.php$is_args$args; + } + add_header X-Frame-Options "SAMEORIGIN"; +} + +location /media/customer/ { + deny all; +} + +location /media/downloadable/ { + deny all; +} + +location /media/import/ { + deny all; +} + +location /media/custom_options/ { + deny all; +} + +location /errors/ { + location ~* \.xml$ { + deny all; + } +} + +# PHP entry point for main application +location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ { + try_files $uri =404; + fastcgi_pass fastcgi_backend; + fastcgi_buffers 16 16k; + fastcgi_buffer_size 32k; + + fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off"; + fastcgi_param PHP_VALUE "memory_limit=756M \n max_execution_time=18000"; + fastcgi_read_timeout 600s; + fastcgi_connect_timeout 600s; + + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; +} + +gzip on; +gzip_disable "msie6"; + +gzip_comp_level 6; +gzip_min_length 1100; +gzip_buffers 16 8k; +gzip_proxied any; +gzip_types + text/plain + text/css + text/js + text/xml + text/javascript + application/javascript + application/x-javascript + application/json + application/xml + application/xml+rss + image/svg+xml; +gzip_vary on; + +# Banned locations (only reached if the earlier PHP entry point regexes don't match) +location ~* (\.php$|\.phtml$|\.htaccess$|\.git) { + deny all; +} diff --git a/compose/config/nginx/nginx.conf.store.sample b/compose/config/nginx/nginx.conf.store.sample new file mode 100644 index 000000000..ff7e32d6a --- /dev/null +++ b/compose/config/nginx/nginx.conf.store.sample @@ -0,0 +1,255 @@ +## Example configuration: +# upstream fastcgi_backend { +# # use tcp connection +# # server 127.0.0.1:9000; +# # or socket +# server unix:/var/run/php/php7.4-fpm.sock; +# } +# server { +# listen 80; +# server_name mage.dev; +# set $MAGE_ROOT /var/www/magento2; +# set $MAGE_DEBUG_SHOW_ARGS 0; +# include /vagrant/magento2/nginx.conf.sample; +# } +# +## Optional override of deployment mode. We recommend you use the +## command 'bin/magento deploy:mode:set' to switch modes instead. +## +## set $MAGE_MODE default; # or production or developer +## +## If you set MAGE_MODE in server config, you must pass the variable into the +## PHP entry point blocks, which are indicated below. You can pass +## it in using: +## +## fastcgi_param MAGE_MODE $MAGE_MODE; +## +## In production mode, you should uncomment the 'expires' directive in the /static/ location block + +# Modules can be loaded only at the very beginning of the Nginx config file, please move the line below to the main config file +# load_module /etc/nginx/modules/ngx_http_image_filter_module.so; + +root $MAGE_ROOT/pub; + +index index.php; +autoindex off; +charset UTF-8; +error_page 404 403 = /errors/404.php; +#add_header "X-UA-Compatible" "IE=Edge"; + + +# Deny access to sensitive files +location /.user.ini { + deny all; +} + +# PHP entry point for setup application +location ~* ^/setup($|/) { + root $MAGE_ROOT; + location ~ ^/setup/index.php { + fastcgi_pass fastcgi_backend; + + fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off"; + fastcgi_param PHP_VALUE "memory_limit=756M \n max_execution_time=600"; + + fastcgi_param MAGE_RUN_TYPE store; + fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE; + + fastcgi_read_timeout 600s; + fastcgi_connect_timeout 600s; + + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; + } + + location ~ ^/setup/(?!pub/). { + deny all; + } + + location ~ ^/setup/pub/ { + add_header X-Frame-Options "SAMEORIGIN"; + } +} + +# PHP entry point for update application +location ~* ^/update($|/) { + root $MAGE_ROOT; + + location ~ ^/update/index.php { + fastcgi_split_path_info ^(/update/index.php)(/.+)$; + fastcgi_pass fastcgi_backend; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + include fastcgi_params; + } + + # Deny everything but index.php + location ~ ^/update/(?!pub/). { + deny all; + } + + location ~ ^/update/pub/ { + add_header X-Frame-Options "SAMEORIGIN"; + } +} + +location / { + try_files $uri $uri/ /index.php$is_args$args; +} + +location /pub/ { + location ~ ^/pub/media/(downloadable|customer|import|custom_options|theme_customization/.*\.xml) { + deny all; + } + alias $MAGE_ROOT/pub/; + add_header X-Frame-Options "SAMEORIGIN"; +} + +location /static/ { + # Uncomment the following line in production mode + # expires max; + + # Remove signature of the static files that is used to overcome the browser cache + location ~ ^/static/version { + rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last; + } + + location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|html|json)$ { + add_header Cache-Control "public"; + add_header X-Frame-Options "SAMEORIGIN"; + expires +1y; + + if (!-f $request_filename) { + rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last; + } + } + location ~* \.(zip|gz|gzip|bz2|csv|xml)$ { + add_header Cache-Control "no-store"; + add_header X-Frame-Options "SAMEORIGIN"; + expires off; + + if (!-f $request_filename) { + rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last; + } + } + if (!-f $request_filename) { + rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last; + } + add_header X-Frame-Options "SAMEORIGIN"; +} + +location /media/ { + +## The following section allows to offload image resizing from Magento instance to the Nginx. +## Catalog image URL format should be set accordingly. +## See https://docs.magento.com/user-guide/configuration/general/web.html#url-options +# location ~* ^/media/catalog/.* { +# +# # Replace placeholders and uncomment the line below to serve product images from public S3 +# # See examples of S3 authentication at https://github.com/anomalizer/ngx_aws_auth +# # resolver 8.8.8.8; +# # proxy_pass https://..amazonaws.com; +# +# set $width "-"; +# set $height "-"; +# if ($arg_width != '') { +# set $width $arg_width; +# } +# if ($arg_height != '') { +# set $height $arg_height; +# } +# image_filter resize $width $height; +# image_filter_jpeg_quality 90; +# } + + try_files $uri $uri/ /get.php$is_args$args; + + location ~ ^/media/theme_customization/.*\.xml { + deny all; + } + + location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ { + add_header Cache-Control "public"; + add_header X-Frame-Options "SAMEORIGIN"; + expires +1y; + try_files $uri $uri/ /get.php$is_args$args; + } + location ~* \.(zip|gz|gzip|bz2|csv|xml)$ { + add_header Cache-Control "no-store"; + add_header X-Frame-Options "SAMEORIGIN"; + expires off; + try_files $uri $uri/ /get.php$is_args$args; + } + add_header X-Frame-Options "SAMEORIGIN"; +} + +location /media/customer/ { + deny all; +} + +location /media/downloadable/ { + deny all; +} + +location /media/import/ { + deny all; +} + +location /media/custom_options/ { + deny all; +} + +location /errors/ { + location ~* \.xml$ { + deny all; + } +} + +# PHP entry point for main application +location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ { + try_files $uri =404; + fastcgi_pass fastcgi_backend; + fastcgi_buffers 16 16k; + fastcgi_buffer_size 32k; + + fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off"; + fastcgi_param PHP_VALUE "memory_limit=756M \n max_execution_time=18000"; + + fastcgi_param MAGE_RUN_TYPE store; + fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE; + + fastcgi_read_timeout 600s; + fastcgi_connect_timeout 600s; + + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; +} + +gzip on; +gzip_disable "msie6"; + +gzip_comp_level 6; +gzip_min_length 1100; +gzip_buffers 16 8k; +gzip_proxied any; +gzip_types + text/plain + text/css + text/js + text/xml + text/javascript + application/javascript + application/x-javascript + application/json + application/xml + application/xml+rss + image/svg+xml; +gzip_vary on; + +# Banned locations (only reached if the earlier PHP entry point regexes don't match) +location ~* (\.php$|\.phtml$|\.htaccess$|\.git) { + deny all; +} diff --git a/compose/docker-compose.dev-linux.yml b/compose/docker-compose.dev-linux.yml index 8df350fa0..e82ca6be3 100644 --- a/compose/docker-compose.dev-linux.yml +++ b/compose/docker-compose.dev-linux.yml @@ -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 + # Run the `bin/setup-domain-multistore` script, uncomment the two lines below and restart 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 diff --git a/compose/sql/get_stores.sql b/compose/sql/get_stores.sql new file mode 100644 index 000000000..7b563edd7 --- /dev/null +++ b/compose/sql/get_stores.sql @@ -0,0 +1,30 @@ +DELIMITER // + +DROP FUNCTION IF EXISTS JSON_ARRAYAGG// + +CREATE AGGREGATE FUNCTION IF NOT EXISTS JSON_ARRAYAGG(next_value TEXT) RETURNS TEXT +BEGIN + + DECLARE json TEXT DEFAULT '[""]'; + DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN json_remove(json, '$[0]'); + LOOP + FETCH GROUP NEXT ROW; + SET json = json_array_append(json, '$', next_value); + END LOOP; + +END // +DELIMITER ; + +SELECT JSON_ARRAYAGG( + JSON_OBJECT( + 'name', + name, + 'code', + code, + 'store_id', + store_id, + 'is_active', + is_active + ) + ) +from magento.store; \ No newline at end of file From 68c666b214f6b72fec88dcc15aa480ac215ae50c Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Wed, 27 Apr 2022 00:19:51 +0200 Subject: [PATCH 02/12] Refactor multistore scripts --- compose/bin/add-base-urls-to-hosts | 2 +- compose/bin/generate-nginx-mapping | 2 +- compose/bin/generate-store-setters | 2 +- compose/bin/get-stores | 3 +++ 4 files changed, 6 insertions(+), 3 deletions(-) create mode 100755 compose/bin/get-stores diff --git a/compose/bin/add-base-urls-to-hosts b/compose/bin/add-base-urls-to-hosts index 21e2e2f44..39341c421 100755 --- a/compose/bin/add-base-urls-to-hosts +++ b/compose/bin/add-base-urls-to-hosts @@ -1,6 +1,6 @@ #!/bin/bash -STORES=$(bin/mysql Date: Wed, 27 Apr 2022 10:53:09 +0200 Subject: [PATCH 03/12] Simplify setup-domain-multistore script Remove redundant code --- compose/bin/add-base-urls-to-hosts | 12 ++--- compose/bin/generate-nginx-mapping | 13 ++--- compose/bin/generate-store-setters | 84 ++++++++++++++---------------- compose/bin/get-stores | 3 -- compose/sql/get_stores.sql | 30 ----------- 5 files changed, 50 insertions(+), 92 deletions(-) delete mode 100755 compose/bin/get-stores delete mode 100644 compose/sql/get_stores.sql diff --git a/compose/bin/add-base-urls-to-hosts b/compose/bin/add-base-urls-to-hosts index 39341c421..eee6e8cbb 100755 --- a/compose/bin/add-base-urls-to-hosts +++ b/compose/bin/add-base-urls-to-hosts @@ -1,21 +1,21 @@ #!/bin/bash -STORES=$(bin/get-stores | jq) +STORES=$(bin/n98-magerun2 sys:store:list --format=json | grep -iv warning | jq) -COUNTER=0 -for STORE_ID in $(echo "$STORES" | jq -r '.[].store_id'); do +echo | tee -a /etc/hosts +for STORE_ID in $(echo "$STORES" | jq -r '.[].id'); do SEPARATOR="" STORE_CODE="" if [[ ${STORE_ID} -gt 1 ]]; then SEPARATOR="-" - STORE_CODE=$(echo "$STORES" | jq -r ".[${COUNTER}].code") + STORE_CODE=$(echo "$STORES" | jq -r ".[\"$STORE_ID\"].code") STORE_CODE=$(echo "$STORE_CODE" | sed -e 's/_/-/g') 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 - echo "$(printf "\n\n")"127.0.0.1"$(printf "\t")""$HOSTS_URL" | tee -a /etc/hosts + + echo 127.0.0.1"$(printf "\t")""$HOSTS_URL" | tee -a /etc/hosts fi - COUNTER=$(($COUNTER + 1)) done diff --git a/compose/bin/generate-nginx-mapping b/compose/bin/generate-nginx-mapping index 333060824..5d3cd1d7f 100755 --- a/compose/bin/generate-nginx-mapping +++ b/compose/bin/generate-nginx-mapping @@ -1,20 +1,15 @@ #!/bin/bash set -e -STORES=$(bin/get-stores | jq) +STORES=$(bin/n98-magerun2 sys:store:list --format=json | grep -iv warning | jq) # NUMBER_OF_STORES=$(echo "$STORES" | jq 'length') printf 'map $http_host $MAGE_RUN_CODE {\n' -printf ' %s\n' "default $(echo "$STORES" | jq -r '.[1].code')"';' +printf ' %s\n' "default $(echo "$STORES" | jq -r '.[].code' | head -n1)"';' -COUNTER=0 for STORE_CODE in $(echo "$STORES" | jq -r '.[].code'); do - IS_ACTIVE=$(echo "$STORES" | jq -r '.['$COUNTER'].is_active') - if [ "$IS_ACTIVE" -eq 1 ] && [ "$COUNTER" -gt 0 ]; then - STORE_WITH_DASHES=$(echo "$STORE_CODE" | sed -e 's/_/-/g') - printf ' %s\n' 'magento-'"$STORE_WITH_DASHES"'.test '"$STORE_CODE"';' - fi - COUNTER=$(($COUNTER + 1)) + STORE_WITH_DASHES=$(echo "$STORE_CODE" | sed -e 's/_/-/g') + printf ' %s\n' 'magento-'"$STORE_WITH_DASHES"'.test '"$STORE_CODE"';' done printf '%s\n' "}" diff --git a/compose/bin/generate-store-setters b/compose/bin/generate-store-setters index ff449045f..cec007dd1 100755 --- a/compose/bin/generate-store-setters +++ b/compose/bin/generate-store-setters @@ -1,65 +1,61 @@ #!/bin/bash set -e -STORES=$(bin/get-stores | jq) +STORES=$(bin/n98-magerun2 sys:store:list --format=json | grep -iv warning | jq) # echo "use magento;" echo "DELETE FROM core_config_data WHERE path LIKE 'web/%/base%url';" -COUNTER=0 -for STORE_ID in $(echo "$STORES" | jq -r '.[].store_id'); do - IS_ACTIVE=$(echo "$STORES" | jq -r '.['$COUNTER'].is_active') - if [[ "$IS_ACTIVE" == 1 ]]; then - STORE_CODE="" - SEPARATOR="" - SCOPE="default" - if [[ ${STORE_ID} -gt 1 ]]; then - SEPARATOR="-" - STORE_CODE=$(echo "$STORES" | jq -r ".[${COUNTER}].code") - STORE_CODE=$(echo "$STORE_CODE" | sed -e 's/_/-/g') - SCOPE="stores" - fi - SECURE_BASE_URL="https://magento${SEPARATOR}${STORE_CODE}.test/" - UNSECURE_BASE_URL="https://magento${SEPARATOR}${STORE_CODE}.test/" +for STORE_ID in $(echo "$STORES" | jq -r '.[].id'); do + STORE_CODE="" + SEPARATOR="" + SCOPE="default" + if [[ ${STORE_ID} -gt 1 ]]; then + SEPARATOR="-" + STORE_CODE=$(echo "$STORES" | jq -r ".[\"${STORE_ID}\"].code") + STORE_CODE=$(echo "$STORE_CODE" | sed -e 's/_/-/g') + 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_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_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/" + 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_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_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_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_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_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_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_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_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_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" - 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" - fi - COUNTER=$(($COUNTER + 1)) done diff --git a/compose/bin/get-stores b/compose/bin/get-stores deleted file mode 100755 index 1d45c7308..000000000 --- a/compose/bin/get-stores +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -echo $(bin/mysql Date: Thu, 28 Apr 2022 00:14:28 +0200 Subject: [PATCH 04/12] Replace jq calls with comon *nix commands --- compose/bin/add-base-urls-to-hosts | 8 ++++---- compose/bin/generate-nginx-mapping | 8 ++++---- compose/bin/generate-store-setters | 7 ++++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/compose/bin/add-base-urls-to-hosts b/compose/bin/add-base-urls-to-hosts index eee6e8cbb..4b1e7c917 100755 --- a/compose/bin/add-base-urls-to-hosts +++ b/compose/bin/add-base-urls-to-hosts @@ -1,14 +1,15 @@ #!/bin/bash -STORES=$(bin/n98-magerun2 sys:store:list --format=json | grep -iv warning | jq) +STORES=$(bin/n98-magerun2 sys:store:list --format=csv | tail -n+2) echo | tee -a /etc/hosts -for STORE_ID in $(echo "$STORES" | jq -r '.[].id'); do +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 "$STORES" | jq -r ".[\"$STORE_ID\"].code") + STORE_CODE=$(echo "$STORE" | cut -d ',' -f2) STORE_CODE=$(echo "$STORE_CODE" | sed -e 's/_/-/g') fi HOSTS_URL=magento"${SEPARATOR}""${STORE_CODE}".test @@ -17,5 +18,4 @@ for STORE_ID in $(echo "$STORES" | jq -r '.[].id'); do echo 127.0.0.1"$(printf "\t")""$HOSTS_URL" | tee -a /etc/hosts fi - done diff --git a/compose/bin/generate-nginx-mapping b/compose/bin/generate-nginx-mapping index 5d3cd1d7f..14e22e0b3 100755 --- a/compose/bin/generate-nginx-mapping +++ b/compose/bin/generate-nginx-mapping @@ -1,13 +1,13 @@ #!/bin/bash set -e -STORES=$(bin/n98-magerun2 sys:store:list --format=json | grep -iv warning | jq) -# NUMBER_OF_STORES=$(echo "$STORES" | jq 'length') +STORES=$(bin/n98-magerun2 sys:store:list --format=csv | tail -n+2) printf 'map $http_host $MAGE_RUN_CODE {\n' -printf ' %s\n' "default $(echo "$STORES" | jq -r '.[].code' | head -n1)"';' +printf ' %s\n' "default $(echo "$STORES" | head -n1 | cut -d ',' -f2)"';' -for STORE_CODE in $(echo "$STORES" | jq -r '.[].code'); do +for STORE in $STORES; do + STORE_CODE=$(echo "$STORE" | cut -d ',' -f2) STORE_WITH_DASHES=$(echo "$STORE_CODE" | sed -e 's/_/-/g') printf ' %s\n' 'magento-'"$STORE_WITH_DASHES"'.test '"$STORE_CODE"';' done diff --git a/compose/bin/generate-store-setters b/compose/bin/generate-store-setters index cec007dd1..98dc2f97b 100755 --- a/compose/bin/generate-store-setters +++ b/compose/bin/generate-store-setters @@ -1,18 +1,19 @@ #!/bin/bash set -e -STORES=$(bin/n98-magerun2 sys:store:list --format=json | grep -iv warning | jq) +STORES=$(bin/n98-magerun2 sys:store:list --format=csv | tail -n+2) # echo "use magento;" echo "DELETE FROM core_config_data WHERE path LIKE 'web/%/base%url';" -for STORE_ID in $(echo "$STORES" | jq -r '.[].id'); do +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 "$STORES" | jq -r ".[\"${STORE_ID}\"].code") + STORE_CODE=$(echo "$STORE" | cut -d ',' -f2) STORE_CODE=$(echo "$STORE_CODE" | sed -e 's/_/-/g') SCOPE="stores" fi From d79afe779822b38de961d76ae625541ea1d0b834 Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Tue, 10 May 2022 21:41:38 +0200 Subject: [PATCH 05/12] fix: ignore warning messages printed by n98-magerun sys:store:list --- compose/bin/add-base-urls-to-hosts | 3 +-- compose/bin/generate-nginx-mapping | 2 +- compose/bin/generate-store-setters | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/compose/bin/add-base-urls-to-hosts b/compose/bin/add-base-urls-to-hosts index 4b1e7c917..1fd5e5c36 100755 --- a/compose/bin/add-base-urls-to-hosts +++ b/compose/bin/add-base-urls-to-hosts @@ -1,6 +1,6 @@ #!/bin/bash -STORES=$(bin/n98-magerun2 sys:store:list --format=csv | tail -n+2) +STORES=$(bin/n98-magerun2 sys:store:list --format=csv | grep -v Warning | sed '/^$/d' | tail -n+2) echo | tee -a /etc/hosts for STORE in $STORES; do @@ -15,7 +15,6 @@ for STORE in $STORES; do 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 - echo 127.0.0.1"$(printf "\t")""$HOSTS_URL" | tee -a /etc/hosts fi done diff --git a/compose/bin/generate-nginx-mapping b/compose/bin/generate-nginx-mapping index 14e22e0b3..92e0608b6 100755 --- a/compose/bin/generate-nginx-mapping +++ b/compose/bin/generate-nginx-mapping @@ -1,7 +1,7 @@ #!/bin/bash set -e -STORES=$(bin/n98-magerun2 sys:store:list --format=csv | tail -n+2) +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)"';' diff --git a/compose/bin/generate-store-setters b/compose/bin/generate-store-setters index 98dc2f97b..364f9447f 100755 --- a/compose/bin/generate-store-setters +++ b/compose/bin/generate-store-setters @@ -1,7 +1,7 @@ #!/bin/bash set -e -STORES=$(bin/n98-magerun2 sys:store:list --format=csv | tail -n+2) +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';" From 167a3467121734f58e467f2bf97b9f665cc0a4a9 Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Tue, 10 May 2022 21:42:50 +0200 Subject: [PATCH 06/12] chore: add word multistore to nginx conf file --- compose/bin/update-nginx-config | 2 +- .../{default.conf.sample => default.conf.multistore.sample} | 0 .../{nginx.conf.store.sample => nginx.conf.multistore.sample} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename compose/config/nginx/{default.conf.sample => default.conf.multistore.sample} (100%) rename compose/config/nginx/{nginx.conf.store.sample => nginx.conf.multistore.sample} (100%) diff --git a/compose/bin/update-nginx-config b/compose/bin/update-nginx-config index 9d75eaf8b..bf61d4168 100755 --- a/compose/bin/update-nginx-config +++ b/compose/bin/update-nginx-config @@ -2,4 +2,4 @@ set -e bin/generate-nginx-mapping >config/nginx/default.conf -cd config/nginx && cat default.conf.sample >>default.conf && cp nginx.conf.store.sample nginx.conf +cd config/nginx && cat default.conf.multistore.sample >>default.conf && cp nginx.conf.multistore.sample nginx.conf diff --git a/compose/config/nginx/default.conf.sample b/compose/config/nginx/default.conf.multistore.sample similarity index 100% rename from compose/config/nginx/default.conf.sample rename to compose/config/nginx/default.conf.multistore.sample diff --git a/compose/config/nginx/nginx.conf.store.sample b/compose/config/nginx/nginx.conf.multistore.sample similarity index 100% rename from compose/config/nginx/nginx.conf.store.sample rename to compose/config/nginx/nginx.conf.multistore.sample From 0cbf91d0f0ba5aeea6fe5733fd94d89e55e66add Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Tue, 10 May 2022 21:43:22 +0200 Subject: [PATCH 07/12] docs: update readme --- README.md | 24 ++++++++++++++++++++++++ compose/docker-compose.dev-linux.yml | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 223b539fe..a28ecda9e 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,30 @@ 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` +bin/setup-multistore + +# Adds the same URLs into the /etc/hosts file +sudo bin/add-base-urls-to-hosts + +# If using Linux uncomment two 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 + +# Restart containers +bin/restart +``` + ## Updates To update your project to the latest version of `docker-magento`, run: diff --git a/compose/docker-compose.dev-linux.yml b/compose/docker-compose.dev-linux.yml index e82ca6be3..537250da4 100644 --- a/compose/docker-compose.dev-linux.yml +++ b/compose/docker-compose.dev-linux.yml @@ -3,7 +3,7 @@ version: "3" services: app: volumes: &appvolumes - # Run the `bin/setup-domain-multistore` script, uncomment the two lines below and restart app service + # 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 From bf2b54b1f0bb613191f4c4e80bebaa923d105a61 Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Tue, 10 May 2022 21:53:53 +0200 Subject: [PATCH 08/12] docs: update readme for macos --- README.md | 7 ++++++- compose/docker-compose.dev.yml | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a28ecda9e..c8b2ca006 100644 --- a/README.md +++ b/README.md @@ -227,11 +227,16 @@ bin/setup-multistore # Adds the same URLs into the /etc/hosts file sudo bin/add-base-urls-to-hosts -# If using Linux uncomment two mount bindings in `docker-compose.dev.linux.yml` file and copy the file to `docker-compose.dev.yml` +# 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 ``` diff --git a/compose/docker-compose.dev.yml b/compose/docker-compose.dev.yml index fd9b0d38d..e623e4fa9 100644 --- a/compose/docker-compose.dev.yml +++ b/compose/docker-compose.dev.yml @@ -10,6 +10,9 @@ services: - ./src/composer.json:/var/www/html/composer.json:cached - ./src/composer.lock:/var/www/html/composer.lock:cached - ./src/nginx.conf.sample:/var/www/html/nginx.conf:cached + # If you want to run a multistore Magento installation uncomment the two lines below and 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 From ef38d8709bda8883f7135e764d678a216f0eedd5 Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Tue, 10 May 2022 21:56:35 +0200 Subject: [PATCH 09/12] docs: update readme --- compose/docker-compose.dev.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compose/docker-compose.dev.yml b/compose/docker-compose.dev.yml index e623e4fa9..4e9f7d7e4 100644 --- a/compose/docker-compose.dev.yml +++ b/compose/docker-compose.dev.yml @@ -10,7 +10,7 @@ services: - ./src/composer.json:/var/www/html/composer.json:cached - ./src/composer.lock:/var/www/html/composer.lock:cached - ./src/nginx.conf.sample:/var/www/html/nginx.conf:cached - # If you want to run a multistore Magento installation uncomment the two lines below and the one above and restart the app service + # 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 From dab39895d6dbddd246514f1dea37e19634c96511 Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Sun, 15 May 2022 16:38:20 +0200 Subject: [PATCH 10/12] refactor: streamline /etc/hosts update for multistore setup --- README.md | 3 +-- compose/bin/add-base-urls-to-hosts | 2 +- compose/bin/setup-domain-multistore | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c8b2ca006..07e2feff3 100644 --- a/README.md +++ b/README.md @@ -222,10 +222,9 @@ bin/n98-magerun2 # 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 -# Adds the same URLs into the /etc/hosts file -sudo bin/add-base-urls-to-hosts # 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 diff --git a/compose/bin/add-base-urls-to-hosts b/compose/bin/add-base-urls-to-hosts index 1fd5e5c36..9cd6eceac 100755 --- a/compose/bin/add-base-urls-to-hosts +++ b/compose/bin/add-base-urls-to-hosts @@ -15,6 +15,6 @@ for STORE in $STORES; do 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 - echo 127.0.0.1"$(printf "\t")""$HOSTS_URL" | tee -a /etc/hosts + echo 127.0.0.1"$(printf "\t")""$HOSTS_URL" | sudo tee -a /etc/hosts fi done diff --git a/compose/bin/setup-domain-multistore b/compose/bin/setup-domain-multistore index 0f72e1c10..92b02e3fa 100755 --- a/compose/bin/setup-domain-multistore +++ b/compose/bin/setup-domain-multistore @@ -4,3 +4,4 @@ source env/db.env bin/generate-store-setters | bin/mysql bin/update-nginx-config +bin/add-base-urls-to-hosts From 341236d6a82a99f5a399a0fcca396cae4d948add Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Sun, 22 May 2022 17:59:42 +0200 Subject: [PATCH 11/12] refactor: make shellcheck happy --- compose/bin/add-base-urls-to-hosts | 2 +- compose/bin/generate-nginx-mapping | 4 ++-- compose/bin/generate-store-setters | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compose/bin/add-base-urls-to-hosts b/compose/bin/add-base-urls-to-hosts index 9cd6eceac..fa9c975fe 100755 --- a/compose/bin/add-base-urls-to-hosts +++ b/compose/bin/add-base-urls-to-hosts @@ -10,7 +10,7 @@ for STORE in $STORES; do if [[ ${STORE_ID} -gt 1 ]]; then SEPARATOR="-" STORE_CODE=$(echo "$STORE" | cut -d ',' -f2) - STORE_CODE=$(echo "$STORE_CODE" | sed -e 's/_/-/g') + STORE_CODE="echo \"${STORE_CODE//_/-}\"" fi HOSTS_URL=magento"${SEPARATOR}""${STORE_CODE}".test URL_IN_HOSTS_FILE=$(grep -c "$HOSTS_URL" /etc/hosts) diff --git a/compose/bin/generate-nginx-mapping b/compose/bin/generate-nginx-mapping index 92e0608b6..07e415408 100755 --- a/compose/bin/generate-nginx-mapping +++ b/compose/bin/generate-nginx-mapping @@ -3,12 +3,12 @@ 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 "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=$(echo "$STORE_CODE" | sed -e 's/_/-/g') + STORE_WITH_DASHES="echo \"${STORE_CODE//_/-}\"" printf ' %s\n' 'magento-'"$STORE_WITH_DASHES"'.test '"$STORE_CODE"';' done diff --git a/compose/bin/generate-store-setters b/compose/bin/generate-store-setters index 364f9447f..e6d3876f8 100755 --- a/compose/bin/generate-store-setters +++ b/compose/bin/generate-store-setters @@ -14,7 +14,7 @@ for STORE in $STORES; do if [[ ${STORE_ID} -gt 1 ]]; then SEPARATOR="-" STORE_CODE=$(echo "$STORE" | cut -d ',' -f2) - STORE_CODE=$(echo "$STORE_CODE" | sed -e 's/_/-/g') + STORE_CODE="echo \"${STORE_CODE//_/-}\"" SCOPE="stores" fi SECURE_BASE_URL="https://magento${SEPARATOR}${STORE_CODE}.test/" From f0e02bfc7eef03aa320d0c9a6674b0b13694869a Mon Sep 17 00:00:00 2001 From: Goran Ninkovic Date: Tue, 13 Dec 2022 18:17:18 +0100 Subject: [PATCH 12/12] Refactor variable assignment --- compose/bin/add-base-urls-to-hosts | 8 ++++++-- compose/bin/generate-nginx-mapping | 2 +- compose/bin/generate-store-setters | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/compose/bin/add-base-urls-to-hosts b/compose/bin/add-base-urls-to-hosts index fa9c975fe..550695fdb 100755 --- a/compose/bin/add-base-urls-to-hosts +++ b/compose/bin/add-base-urls-to-hosts @@ -2,7 +2,7 @@ STORES=$(bin/n98-magerun2 sys:store:list --format=csv | grep -v Warning | sed '/^$/d' | tail -n+2) -echo | tee -a /etc/hosts +NEWLINE_PRINTED=false for STORE in $STORES; do SEPARATOR="" STORE_CODE="" @@ -10,11 +10,15 @@ for STORE in $STORES; do if [[ ${STORE_ID} -gt 1 ]]; then SEPARATOR="-" STORE_CODE=$(echo "$STORE" | cut -d ',' -f2) - STORE_CODE="echo \"${STORE_CODE//_/-}\"" + 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 diff --git a/compose/bin/generate-nginx-mapping b/compose/bin/generate-nginx-mapping index 07e415408..27f666338 100755 --- a/compose/bin/generate-nginx-mapping +++ b/compose/bin/generate-nginx-mapping @@ -8,7 +8,7 @@ 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="echo \"${STORE_CODE//_/-}\"" + STORE_WITH_DASHES=${STORE_CODE//_/-} printf ' %s\n' 'magento-'"$STORE_WITH_DASHES"'.test '"$STORE_CODE"';' done diff --git a/compose/bin/generate-store-setters b/compose/bin/generate-store-setters index e6d3876f8..15fdb4afe 100755 --- a/compose/bin/generate-store-setters +++ b/compose/bin/generate-store-setters @@ -14,7 +14,7 @@ for STORE in $STORES; do if [[ ${STORE_ID} -gt 1 ]]; then SEPARATOR="-" STORE_CODE=$(echo "$STORE" | cut -d ',' -f2) - STORE_CODE="echo \"${STORE_CODE//_/-}\"" + STORE_CODE=${STORE_CODE//_/-} SCOPE="stores" fi SECURE_BASE_URL="https://magento${SEPARATOR}${STORE_CODE}.test/"