Laradock for windows 安装配置
Hyper-v 和 Docker Desktop 配置
1. 开始菜单搜索 Hyper-v 管理器
2. 配置虚拟机和磁盘目录
3. 到官网下载安装 Docker Desktop
4. 右键托盘区 Docker 图标,settings -> Shared Drives,勾选需要的目录,C盘必须有
5. 设置磁盘目录 Settings -> Advanced
6. 设置代理 Settings -> Proxies(仅在需要时,开启nginx服务需要关掉这个配置),或者有办法到墙外也行
7. 设置 Docker 源 Settings -> Daemon
{ "registry-mirrors": [ "http://hub-mirror.c.163.com" ], "insecure-registries": [], "debug": true, "experimental": false }
Laradock 安装
-
首先将 Laradock 项目代码克隆到本地(项目目录):
git clone https://github.com/Laradock/laradock.git laradock-my_project
-
进入 laradock-my_project 目录将
env-example
重命名为.env
:cp env-example .env
-
打开项目的
.env
文件并添加如下配置:DB_HOST=mysql
REDIS_HOST=redis
QUEUE_HOST=beanstalkd
-
然后我们需要到 laradock 下编辑 .env 中的 APP_CODE_PATH_HOST 配置项:
APP_CODE_PATH_HOST=../
APP_CODE_PATH_CONTAINER=/var/www/my_project
这样就相当于为
wwwroot
与 Docker 的/var/www
目录建立了软链接,然后我们在laradock/nginx/sites
目录下新增一个my_project.conf
配置server { listen 80; listen [::]:80; # For https # listen 443 ssl; # listen [::]:443 ssl ipv6only=on; # ssl_certificate /etc/nginx/ssl/default.crt; # ssl_certificate_key /etc/nginx/ssl/default.key; server_name my_project.local; root /var/www/my_project/public; index index.php index.html index.htm; client_max_body_size 100m; location / { try_files $uri $uri/ /index.php$is_args$args; } error_page 404 /index.php; # redirect server error pages to the static page /50x.html # error_page 500 501 502 503 504 508 /50x.html; location = /50x.html { root html; } location = /robots.txt { access_log off; log_not_found off; } location ~ .*\.(gif|jpg|jpeg|ico|png)?$ { access_log off; } location ~ ^/assets|css|js.*\.js|css { access_log off; } location /socket.io { proxy_pass http://laravel-echo-server:6001; #could be localhost if Echo and NginX are on the same box proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_pass php-upstream; fastcgi_index index.php; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fixes timeouts fastcgi_read_timeout 600; include fastcgi_params; } location ~ /\.ht { deny all; } location /.well-known/acme-challenge/ { root /var/www/letsencrypt/; log_not_found off; } error_log /var/log/nginx/my_project_error.log; # access_log /var/log/nginx/my_project_access.log; }
接下来需要在
/etc/hosts
(Windows 下对应文件路径是C:\Windows\System32\drivers\etc\hosts
)文件中新增如下这行配置:127.0.0.1 my_project.local
-
其它配置
PHP_VERSION=7.3
WORKSPACE_TIMEZONE=PRC
-
修改 laravel-echo-server 配置
修改 laradock-my_project/laravel-echo-server/package.json
"dependencies": { "laravel-echo-server": "^1.5.7" }
修改 laradock-my_project/laravel-echo-server/laravel-echo-server.json
"authHost": "my_project.local"
-
修改 laravel-horizon 配置
编辑 laradock-my_project/docker-compose.yml
找到 laravel-horizon 配置
为 args 添加
- INSTALL_PHPREDIS=${PHP_FPM_INSTALL_PHPREDIS}
### Laravel Horizon ############################################ laravel-horizon: build: context: ./laravel-horizon args: - PHP_VERSION=${PHP_VERSION} - INSTALL_PGSQL=${PHP_FPM_INSTALL_PGSQL} - INSTALL_BCMATH=${PHP_FPM_INSTALL_BCMATH} - INSTALL_MEMCACHED=${PHP_FPM_INSTALL_MEMCACHED} - INSTALL_SOCKETS=${LARAVEL_HORIZON_INSTALL_SOCKETS} - INSTALL_CASSANDRA=${PHP_FPM_INSTALL_CASSANDRA} - INSTALL_PHPREDIS=${PHP_FPM_INSTALL_PHPREDIS} # 此为新添加行 volumes: - ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER} - ./laravel-horizon/supervisord.d:/etc/supervisord.d depends_on: - workspace extra_hosts: - "dockerhost:${DOCKER_HOST_IP}" networks: - backend
编辑 laradock-my_project/laravel-horizon/Dockerfile
在 PHP Memcached 代码块上方添加
########################################################################### # PHP REDIS EXTENSION ########################################################################### ARG INSTALL_PHPREDIS=false RUN if [ ${INSTALL_PHPREDIS} = true ]; then \ printf "\n" | pecl install -o -f redis \ && rm -rf /tmp/pear \ && docker-php-ext-enable redis \ ;fi
-
最后,启动 Docker:
docker-compose up -d nginx mysql redis laravel-echo-server laravel-horizon
如果指定端口已经被占用,运行上述命令会报错,关闭相应的服务再重新运行上述命令即可。
注:安装过程中,由于某些资源需要FQ才能下载,建议安装并启用 VPN 后再执行上述命令。如果出现需要认证的下载资源无权下载,可以通过 Docker ID/密码 登录到 Docker 应用(点击状态栏 Docker 应用小图标就能看到登录菜单),注意这里必须用 Docker ID,不能用注册邮箱。在 Windows 下如果出现目录挂载失败,可以尝试在 Docker 设置中重新设置 Shared Drives。
TIPS :
-
进入项目代码容器,可以运行 artisan, gulp, composer, phpunit 等
docker-compose exec workspace bash
也可以用此方式进入任何其它容器
docker exec -it {workspace-container-id} bash
-
了解更多请参考官方文档 https://laradock.io/
-
访问网站时 MySql 出错
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
请使用ALTER USER修改已有账户的验证插件:ALTER USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password
或
ALTER USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘new_password‘
也可以更改 MySql 版本,此处不再详写