在 Yii 2 高级模板中,在 CentOS 7.2 中执行初始化命令,一些应用下的目录权限未设置为 777 的分析解决(根源在于 Docker 部署配置问题)

1、在 Windows 10 中执行初始化命令,设置了所有应用的目录权限,正常(总计 14 行),如图1

在 Windows 10 中执行初始化命令,设置了所有应用的目录权限,正常(总计 14 行)

图1

PS E:/wwwroot/channel-pub-api> ./init --env=Development --overwrite=All
Yii Application Initialization Tool v1.0


  Start initialization ...

      exist api/config/main-local.php
            ...overwrite? [Yes|No|All|Quit]   overwrite api/config/main-local.php
  unchanged api/config/params-local.php
  unchanged api/config/test-local.php
  unchanged api/web/index-test.php
  unchanged api/web/index.php
  unchanged api/web/robots.txt
  overwrite backend/config/main-local.php
  unchanged backend/config/params-local.php
  unchanged backend/config/test-local.php
  unchanged backend/web/index-test.php
  unchanged backend/web/index.php
  unchanged backend/web/robots.txt
  unchanged common/config/main-local.php
  unchanged common/config/params-local.php
  unchanged common/config/test-local.php
  unchanged console/config/main-local.php
  unchanged console/config/params-local.php
  overwrite frontend/config/main-local.php
  unchanged frontend/config/params-local.php
  unchanged frontend/config/test-local.php
  unchanged frontend/web/index-test.php
  unchanged frontend/web/index.php
  unchanged frontend/web/robots.txt
  overwrite qq/config/main-local.php
  unchanged qq/config/params-local.php
  unchanged qq/config/test-local.php
  unchanged qq/web/index-test.php
  unchanged qq/web/index.php
  unchanged qq/web/robots.txt
  overwrite rpc/config/main-local.php
  unchanged rpc/config/params-local.php
  unchanged rpc/config/test-local.php
  unchanged rpc/web/index-test.php
  unchanged rpc/web/index.php
  unchanged rpc/web/robots.txt
  overwrite wx/config/main-local.php
  unchanged wx/config/params-local.php
  unchanged wx/config/test-local.php
  unchanged wx/web/index-test.php
  unchanged wx/web/index.php
  unchanged wx/web/robots.txt
  unchanged yii
  unchanged yii_test
  unchanged yii_test.bat
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in frontend/config/main-local.php
   generate cookie validation key in api/config/main-local.php
   generate cookie validation key in rpc/config/main-local.php
   generate cookie validation key in qq/config/main-local.php
   generate cookie validation key in wx/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0777 api/runtime
      chmod 0777 api/web/assets
      chmod 0777 rpc/runtime
      chmod 0777 rpc/web/assets
      chmod 0777 qq/runtime
      chmod 0777 qq/web/assets
      chmod 0777 wx/runtime
      chmod 0777 wx/web/assets
      chmod 0755 yii
      chmod 0755 yii_test

  ... initialization completed.

2、在 CentOS 7.2 中执行初始化命令,一些应用下的目录权限未设置,不正常(总计 8 行),如图2

在 CentOS 7.2 中执行初始化命令,一些应用下的目录权限未设置,不正常(总计 8 行)

图2

[root@579789e72a51 /]# php /sobey/www/channel-pub-api/init --env=Development --overwrite=All
Yii Application Initialization Tool v1.0


  Start initialization ...

      exist api/config/main-local.php
            ...overwrite? [Yes|No|All|Quit]   overwrite api/config/main-local.php
  unchanged api/config/params-local.php
  unchanged api/config/test-local.php
  unchanged api/web/index-test.php
  unchanged api/web/index.php
  unchanged api/web/robots.txt
  overwrite backend/config/main-local.php
  unchanged backend/config/params-local.php
  unchanged backend/config/test-local.php
  unchanged backend/web/index-test.php
  unchanged backend/web/index.php
  unchanged backend/web/robots.txt
  unchanged common/config/main-local.php
  unchanged common/config/params-local.php
  unchanged common/config/test-local.php
  unchanged console/config/main-local.php
  unchanged console/config/params-local.php
  overwrite frontend/config/main-local.php
  unchanged frontend/config/params-local.php
  unchanged frontend/config/test-local.php
  unchanged frontend/web/index-test.php
  unchanged frontend/web/index.php
  unchanged frontend/web/robots.txt
  unchanged qq/config/main-local.php
  unchanged qq/config/params-local.php
  unchanged qq/config/test-local.php
  unchanged qq/web/index-test.php
  unchanged qq/web/index.php
  unchanged qq/web/robots.txt
  unchanged rpc/config/main-local.php
  unchanged rpc/config/params-local.php
  unchanged rpc/config/test-local.php
  unchanged rpc/web/index-test.php
  unchanged rpc/web/index.php
  unchanged rpc/web/robots.txt
  unchanged wx/config/main-local.php
  unchanged wx/config/params-local.php
  unchanged wx/config/test-local.php
  unchanged wx/web/index-test.php
  unchanged wx/web/index.php
  unchanged wx/web/robots.txt
  unchanged yii
  unchanged yii_test
  unchanged yii_test.bat
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in frontend/config/main-local.php
   generate cookie validation key in api/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0777 api/runtime
      chmod 0777 api/web/assets
      chmod 0755 yii
      chmod 0755 yii_test

  ... initialization completed.

3、进入 /api 目录查看,发现 runtime 的目录权限设置为 777,但是 /qq 目录下的 runtime 的目录权限未设置为 777,如图3

进入 /api 目录查看,发现 runtime 的目录权限设置为 777,但是 /qq 目录下的 runtime 的目录权限未设置为 777

图3

4、在 Windows 10 中编辑 /init,打印 $envs,符合预期

PS E:/wwwroot/channel-pub-api> ./init --env=Development --overwrite=All
Array
(
    [Development] => Array
        (
            [path] => dev
            [setWritable] => Array
                (
                    [0] => backend/runtime
                    [1] => backend/web/assets
                    [2] => frontend/runtime
                    [3] => frontend/web/assets
                    [4] => api/runtime
                    [5] => api/web/assets
                    [6] => rpc/runtime
                    [7] => rpc/web/assets
                    [8] => qq/runtime
                    [9] => qq/web/assets
                    [10] => wx/runtime
                    [11] => wx/web/assets
                )

            [setExecutable] => Array
                (
                    [0] => yii
                    [1] => yii_test
                )

            [setCookieValidationKey] => Array
                (
                    [0] => backend/config/main-local.php
                    [1] => frontend/config/main-local.php
                    [2] => api/config/main-local.php
                    [3] => rpc/config/main-local.php
                    [4] => qq/config/main-local.php
                    [5] => wx/config/main-local.php
                )

        )

    [Production] => Array
        (
            [path] => prod
            [setWritable] => Array
                (
                    [0] => backend/runtime
                    [1] => backend/web/assets
                    [2] => frontend/runtime
                    [3] => frontend/web/assets
                    [4] => api/runtime
                    [5] => api/web/assets
                    [6] => rpc/runtime
                    [7] => rpc/web/assets
                    [8] => qq/runtime
                    [9] => qq/web/assets
                    [10] => wx/runtime
                    [11] => wx/web/assets
                )

            [setExecutable] => Array
                (
                    [0] => yii
                )

            [setCookieValidationKey] => Array
                (
                    [0] => backend/config/main-local.php
                    [1] => frontend/config/main-local.php
                    [2] => api/config/main-local.php
                    [3] => rpc/config/main-local.php
                    [4] => qq/config/main-local.php
                    [5] => wx/config/main-local.php
                )

        )

)

5、在 CentOS 7.2 中编辑 /init,打印 $env,不符合预期,发现缺少 qq/runtime 等值

[root@579789e72a51 /]# php /sobey/www/channel-pub-api/init --env=Development --overwrite=All
Array
(
    [Development] => Array
        (
            [path] => dev
            [setWritable] => Array
                (
                    [0] => backend/runtime
                    [1] => backend/web/assets
                    [2] => frontend/runtime
                    [3] => frontend/web/assets
                    [4] => api/runtime
                    [5] => api/web/assets
                )

            [setExecutable] => Array
                (
                    [0] => yii
                    [1] => yii_test
                )

            [setCookieValidationKey] => Array
                (
                    [0] => backend/config/main-local.php
                    [1] => frontend/config/main-local.php
                    [2] => api/config/main-local.php
                )

        )

    [Production] => Array
        (
            [path] => prod
            [setWritable] => Array
                (
                    [0] => backend/runtime
                    [1] => backend/web/assets
                    [2] => frontend/runtime
                    [3] => frontend/web/assets
                    [4] => api/runtime
                    [5] => api/web/assets
                )

            [setExecutable] => Array
                (
                    [0] => yii
                )

            [setCookieValidationKey] => Array
                (
                    [0] => backend/config/main-local.php
                    [1] => frontend/config/main-local.php
                    [2] => api/config/main-local.php
                )

        )

)

6、原因在于 Docker 部署时,是将 /build/c_files/ 目录下的所有文件拷贝至系统根目录,/build/c_files/sobey/www/channel-pub-api/environments/index.php 文件中缺少相应目录配置

<?php
/**
 * The manifest of files that are local to specific environment.
 * This file returns a list of environments that the application
 * may be installed under. The returned data must be in the following
 * format:
 *
 * ```php
 * return [
 *     'environment name' => [
 *         'path' => 'directory storing the local files',
 *         'skipFiles'  => [
 *             // list of files that should only copied once and skipped if they already exist
 *         ],
 *         'setWritable' => [
 *             // list of directories that should be set writable
 *         ],
 *         'setExecutable' => [
 *             // list of files that should be set executable
 *         ],
 *         'setCookieValidationKey' => [
 *             // list of config files that need to be inserted with automatically generated cookie validation keys
 *         ],
 *         'createSymlink' => [
 *             // list of symlinks to be created. Keys are symlinks, and values are the targets.
 *         ],
 *     ],
 * ];
 * ```
 */
return [
    'Development' => [
        'path' => 'dev',
        'setWritable' => [
            'backend/runtime',
            'backend/web/assets',
            'frontend/runtime',
            'frontend/web/assets',
            'api/runtime',
            'api/web/assets',
        ],
        'setExecutable' => [
            'yii',
            'yii_test',
        ],
        'setCookieValidationKey' => [
            'backend/config/main-local.php',
            'frontend/config/main-local.php',
            'api/config/main-local.php',
        ],
    ],
    'Production' => [
        'path' => 'prod',
        'setWritable' => [
            'backend/runtime',
            'backend/web/assets',
            'frontend/runtime',
            'frontend/web/assets',
            'api/runtime',
            'api/web/assets',
        ],
        'setExecutable' => [
            'yii',
        ],
        'setCookieValidationKey' => [
            'backend/config/main-local.php',
            'frontend/config/main-local.php',
            'api/config/main-local.php',
        ],
    ],
];

7、复制 /environments/index.php 至 /build/c_files/sobey/www/channel-pub-api/environments/index.php,重新升级,在 CentOS 7.2 中执行初始化命令,符合预期,如图4

复制 /environments/index.php 至 /build/c_files/sobey/www/channel-pub-api/environments/index.php,重新升级,在 CentOS 7.2 中执行初始化命令,符合预期

图4

[root@579789e72a51 /]# php /sobey/www/channel-pub-api/init --env=Development --overwrite=All
Yii Application Initialization Tool v1.0


  Start initialization ...

   generate api/config/main-local.php
   generate api/config/params-local.php
   generate api/config/test-local.php
   generate api/web/index-test.php
   generate api/web/index.php
   generate api/web/robots.txt
   generate backend/config/main-local.php
   generate backend/config/params-local.php
   generate backend/config/test-local.php
   generate backend/web/index-test.php
   generate backend/web/index.php
   generate backend/web/robots.txt
   generate common/config/main-local.php
   generate common/config/params-local.php
   generate common/config/test-local.php
   generate console/config/main-local.php
   generate console/config/params-local.php
   generate frontend/config/main-local.php
   generate frontend/config/params-local.php
   generate frontend/config/test-local.php
   generate frontend/web/index-test.php
   generate frontend/web/index.php
   generate frontend/web/robots.txt
   generate qq/config/main-local.php
   generate qq/config/params-local.php
   generate qq/config/test-local.php
   generate qq/web/index-test.php
   generate qq/web/index.php
   generate qq/web/robots.txt
   generate rpc/config/main-local.php
   generate rpc/config/params-local.php
   generate rpc/config/test-local.php
   generate rpc/web/index-test.php
   generate rpc/web/index.php
   generate rpc/web/robots.txt
   generate wx/config/main-local.php
   generate wx/config/params-local.php
   generate wx/config/test-local.php
   generate wx/web/index-test.php
   generate wx/web/index.php
   generate wx/web/robots.txt
   generate yii
   generate yii_test
   generate yii_test.bat
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in frontend/config/main-local.php
   generate cookie validation key in api/config/main-local.php
   generate cookie validation key in rpc/config/main-local.php
   generate cookie validation key in qq/config/main-local.php
   generate cookie validation key in wx/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0777 api/runtime
      chmod 0777 api/web/assets
      chmod 0777 rpc/runtime
      chmod 0777 rpc/web/assets
      chmod 0777 qq/runtime
      chmod 0777 qq/web/assets
      chmod 0777 wx/runtime
      chmod 0777 wx/web/assets
      chmod 0755 yii
      chmod 0755 yii_test

  ... initialization completed.

8、进入 /api 目录查看,发现 runtime 的目录权限设置为 777,/qq 目录下的 runtime 的目录权限也设置为 777,如图5

进入 /api 目录查看,发现 runtime 的目录权限设置为 777,/qq 目录下的 runtime 的目录权限也设置为 777

图5

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/250443.html

(0)
上一篇 2022年4月29日 22:48
下一篇 2022年4月29日 22:48

相关推荐

发表回复

登录后才能评论