没有不值得去解决的问题,也没有不值得去学习的技术!

How to Set Directory Permissions in an Advanced Yii2 Template

1. Confirm the web server user with the following command: confirmed to be the www user. The system has a root user and a www user (the web server user).

Plaintext

[root@iZ2zeaj7tnbv8d3gsoy1w5Z wwwroot]# ps aux | grep nginx
root      316309  0.0  0.3  20544  6504 ?        Ss   Mar08   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       317674  0.0  1.8  50788 32196 ?        S    Mar08   0:01 nginx: worker process
www       317675  0.0  1.8  50652 31680 ?        S    Mar08   0:00 nginx: worker process
root      322874  0.0  0.1   6408  2304 pts/0    S+   09:56   0:00 grep --color=auto nginx
[root@iZ2zeaj7tnbv8d3gsoy1w5Z wwwroot]#


2. Set the owner of the project directory to the root user:

Plaintext

[root@iZ2zeaj7tnbv8d3gsoy1w5Z wwwroot]# chown -R root:root /home/wwwroot/object
chown: changing ownership of '/home/wwwroot/object/.user.ini': Operation not permitted
[root@iZ2zeaj7tnbv8d3gsoy1w5Z wwwroot]# ls -l
total 0
drwxr-xr-x 2 www  www  23 Mar  8 19:17 default
drwxr-xr-x 4 root root 81 Mar  8 17:48 object
[root@iZ2zeaj7tnbv8d3gsoy1w5Z wwwroot]#


3. Set the directory permissions to 755:

Plaintext

[root@iZ2zeaj7tnbv8d3gsoy1w5Z wwwroot]# chmod -R 755 /home/wwwroot/object
chmod: changing permissions of '/home/wwwroot/object/.user.ini': Operation not permitted
[root@iZ2zeaj7tnbv8d3gsoy1w5Z wwwroot]# ls -l
total 0
drwxr-xr-x 2 www  www  23 Mar  8 19:17 default
drwxr-xr-x 4 root root 81 Mar  8 17:48 object
[root@iZ2zeaj7tnbv8d3gsoy1w5Z wwwroot]#


4. Set the owner of the runtime directories to the www user:

Plaintext

[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# chown -R www:www backend/runtime
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# ls -l
total 700
drwxr-xr-x 12 root root    193 Mar  8 17:48 api
drwxr-xr-x  9 root root    141 Mar  8 17:48 backend
-rwxr-xr-x  1 root root    167 Mar  8 17:48 codeception.yml
drwxr-xr-x 13 root root    190 Mar  8 17:48 common
-rwxr-xr-x  1 root root   2419 Mar  8 17:48 composer.json
-rwxr-xr-x  1 root root 352792 Mar  8 17:48 composer.lock
-rwxr-xr-x  1 root root 293762 Mar  8 17:48 composer.lock.back
drwxr-xr-x  7 root root     86 Mar  8 17:48 console
-rwxr-xr-x  1 root root    864 Mar  8 17:48 docker-compose.yml
drwxr-xr-x  4 root root     46 Mar  8 17:48 environments
drwxr-xr-x 10 root root    160 Mar  8 17:48 frontend
drwxr-xr-x  3 root root     58 Mar  8 17:48 global
-rwxr-xr-x  1 root root  10194 Mar  8 17:48 init
-rwxr-xr-x  1 root root    319 Mar  8 17:48 init.bat
-rwxr-xr-x  1 root root   1524 Mar  8 17:48 LICENSE.md
-rwxr-xr-x  1 root root   1528 Mar  8 20:07 main-local.php
drwxr-xr-x 11 root root    175 Mar  8 17:48 management
-rwxr-xr-x  1 root root   2187 Mar  8 17:48 README.md
-rwxr-xr-x  1 root root   5791 Mar  8 17:48 requirements.php
drwxr-xr-x  5 root root     50 Mar  8 17:48 vagrant
-rwxr-xr-x  1 root root   2914 Mar  8 17:48 Vagrantfile
drwxr-xr-x 50 root root   4096 Mar  8 18:20 vendor
-rwxr-xr-x  1 root root    719 Mar  8 20:08 yii
-rwxr-xr-x  1 root root    323 Mar  8 17:48 yii.bat
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# cd backend/
[root@iZ2zeaj7tnbv8d3gsoy1w5Z backend]# ls -l
total 8
drwxr-xr-x  2 root root  26 Mar  8 17:48 assets
-rwxr-xr-x  1 root root 313 Mar  8 17:48 codeception.yml
drwxr-xr-x  2 root root 141 Mar  8 20:08 config
-rwxr-xr-x  1 root root 163 Mar  8 17:48 Dockerfile
drwxr-xr-x  2 root root  22 Mar  8 17:48 models
drwxr-xr-x  2 www  www   24 Mar  8 17:48 runtime
drwxr-xr-x  7 root root 152 Mar  8 17:48 tests
drwxr-xr-x 13 root root 207 Mar  8 17:48 views
drwxr-xr-x  4 root root  85 Mar  8 20:08 web
[root@iZ2zeaj7tnbv8d3gsoy1w5Z backend]# cd ..
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# sudo chown -R www:www backend/web/assets
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# sudo chown -R www:www console/runtime
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# sudo chown -R www:www frontend/runtime
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# sudo chown -R www:www frontend/web/assets
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# sudo chown -R www:www api/runtime
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# sudo chown -R www:www api/web/assets
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# sudo chown -R www:www management/runtime
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# sudo chown -R www:www management/web/assets
[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]#


5. Set the web-accessible directory permissions (ensure that the frontend/web and backend/web directories allow the web server user to read files. chmod 755 frontend/web). Set the runtime directory permissions (ensure that the frontend/runtime, backend/runtime, frontend/web/assets, and backend/web/assets directories allow the web server user to read and write. chmod -R 775 frontend/runtime). Set other directory permissions (ensure that the vendor and console directories allow the current user to read and write: sudo chmod -R 755 vendor sudo chmod -R 755 console). Set the environment configuration file permissions (the environments/ directory contains environment configuration files, ensure its permissions are secure: sudo chmod -R 755 environments). This can be completed by executing the ./init command.

Plaintext

[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]# ./init
Yii Application Initialization Tool v1.0

Which environment do you want the application to be initialized in?

  [0] Development
  [1] Production

  Your choice [0-1, or "q" to quit] 0

  Initialize the application under 'Development' environment? [yes|no] yes

  Start initialization ...

   generate api/config/codeception-local.php
      exist api/config/main-local.php
            ...overwrite? [Yes|No|All|Quit] All
  overwrite api/config/main-local.php
  unchanged api/config/params-local.php
   generate api/config/test-local.php
   generate api/web/index-test.php
  overwrite api/web/index.php
  unchanged api/web/robots.txt
   generate backend/config/codeception-local.php
  overwrite backend/config/main-local.php
  unchanged backend/config/params-local.php
   generate backend/config/test-local.php
   generate backend/web/index-test.php
  overwrite backend/web/index.php
  unchanged backend/web/robots.txt
   generate common/config/codeception-local.php
  overwrite common/config/main-local.php
  unchanged common/config/params-local.php
   generate common/config/test-local.php
  overwrite console/config/main-local.php
  unchanged console/config/params-local.php
   generate console/config/test-local.php
   generate frontend/config/codeception-local.php
  overwrite frontend/config/main-local.php
  unchanged frontend/config/params-local.php
   generate frontend/config/test-local.php
   generate frontend/web/index-test.php
  overwrite frontend/web/index.php
  overwrite frontend/web/robots.txt
   generate management/config/codeception-local.php
  overwrite management/config/main-local.php
  overwrite management/config/params-local.php
   generate management/config/test-local.php
   generate management/web/index-test.php
  overwrite management/web/index.php
  overwrite management/web/robots.txt
  overwrite yii
   generate yii_test
   generate yii_test.bat
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in common/config/codeception-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 management/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 console/runtime
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0777 api/runtime
      chmod 0777 api/web/assets
      chmod 0777 management/runtime
      chmod 0777 management/web/assets
      chmod 0755 yii
      chmod 0755 yii_test

  ... initialization completed.

[root@iZ2zeaj7tnbv8d3gsoy1w5Z src]#


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系