Docker 环境下 Yii2 项目配置

1. 修改虚拟站点配置:

server {
   listen       80;
   server_name  pintuu.ddd;
   root   /www/web/pintuu/web;
   index  index.php index.html index.htm;

   location / {
       # 将所有不是真实文件的路由重定向到 index.php
       try_files $uri $uri/ /index.php$is_args$args;
   }

   location /backend {
       # 将所有不是真实文件的路由重定向到 index.php
       try_files $uri $uri/ /backend/index.php$is_args$args;
   }

   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /www/web/pintuu/web;
   }

   location ~ \.php$ {
       fastcgi_pass   php72:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
   }

   location ~ /\.ht {
       deny  all;
   }
}

2. 修改数据库配置:

# host 要写 mysql 容器的名字
return [
   'components' => [
       'db' => [
           'class' => 'yii\db\Connection',
           'dsn' => 'mysql:host=mysql;dbname=pintuu',
           'username' => 'root',
           'password' => '123456',
           'charset' => 'utf8',
           //'tablePrefix' => '',
       ],
   ],
];

3. 设置runtimeassetsuploads等目录可写;

chown -R www web/assets/ web/backend/assets/ web/uploads/ backend/runtime/ console/runtime/ frontend/runtime/

4. 重置nginx:

docker restart nginx

# 重载配置文件
docker exec nginx nginx -s reload

# 测试配置文件是否正确
docker exec nginx nginx -t