(更新中)从零开始搭建 laravel Sail 开发环境

laravel

之前的 laravel 开发环境均为 windows + homestead,一直都用得挺好没问题,但由于 Windows 11 的稳定性越来越好,并且内置的 WSL 也更新到了第二代,再加上 laravel 官方也将推荐的统一开发环境换成了 laravel Sail,啥也不说了,本着终生学习的态度,是时候放弃虚机,拥抱 docker 了。

通过几天的实际体验,给我最大的感受就是快,方方面面的快,环境启停迅速,配置也快,页面响应的也快,没别的,巴适的很。同时在 homestead 做的一切事情,在 docker 下也全能做到,完美

在开始之前,我们需要检查如下几点是否已经做到

  • Windows 10 or 11,并升级到系统最新版本
  • 受够虚机缓慢的相应和繁琐的配置
  • 热与学习新技术,不局限于当下
  • 爱折腾的心

发稿时,我当前版本环境为

  • windows 11 22H2
  • docker 4.19.0
  • laravel 10

ok,让我们正式开始配置之旅吧,很简单

1. 启用 wsl2

Win + R 。输入指令 appwiz.cpl 回车。

在弹出窗的左侧点击 启动或关闭 Windows 功能 ,在弹出窗内勾选 适用于 Linux 的 Windows 子系统Hyper-V虚拟机平台

点击确定电脑会及自动安装,期间根据提示会有几次重启。

重启完后,打开终端应用 Windows Terminal (以管理员方式运行),接下来我们需要设置 WSL 版本为 WSL2 ,并对系统进行更新

1
2
wsl --set-default-version 2
wsl --update

当然,关于更新这块,你也可以打开电脑设置中的 Windows更新,点击检查更新即可对 wsl 进行更新

2. 安装 Ubuntu

当前我们的 Ubuntu TLS 版本为 22.04,我们在 Windows Terminal 中继续操作

1
2
wsl --list --online    // 列出所有可安装的 linux 版本
wsl --install -d Ubuntu-22.04 // 这行指令会安装 Ubuntu-22.04。该指令可能会提示网络错误,因为需要访问 ``https://raw.githubusercontent.com/microsoft/WSL/master/distributions/DistributionInfo.json`` 这个资源,请科学上网后重试该指令

系统安装完毕后会自动启动,进入 ubuntu 界面,等待初始化完毕后,需要设置一个 linux 账号,为了下文演示,我设置的账号密码为 wangkai Admin@123

完成后便可进入熟悉的命令行,关闭即可。以下是具体的执行效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
PS C:\Users\djsyw> wsl --list --online
以下是可安装的有效分发的列表。
使用 'wsl.exe --install <Distro>' 安装。

NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
OracleLinux_8_5 Oracle Linux 8.5
OracleLinux_7_9 Oracle Linux 7.9
SUSE-Linux-Enterprise-Server-15-SP4 SUSE Linux Enterprise Server 15 SP4
openSUSE-Leap-15.4 openSUSE Leap 15.4
openSUSE-Tumbleweed openSUSE Tumbleweed

PS C:\Users\djsyw> wsl --install -d Ubuntu-22.04
正在安装: Ubuntu 22.04 LTS
已安装 Ubuntu 22.04 LTS。
正在启动 Ubuntu 22.04 LTS...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: wangkai
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.90.1-microsoft-standard-WSL2 x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage


This message is shown once a day. To disable it please create the
/home/wangkai/.hushlogin file.
wangkai@Surface-Pro-7:~$

以上安装 ubuntu 的步骤,也可以在应用商店直接搜索安装,也是一样的,相比指令更加直观。

3. 安装 docker

前往 [Docker 官网][1] 下载最新的 Docker Desktop ,下载可能需要注册个免费账号,下载完成后打开安装包,一直下一步即可。

安装完毕会提示注销一下电脑,根据提示操作即可

4. 配置 docker

打开 docker 后,选择上方的齿轮(即设置)- Docker Engine,在右侧我们需要填写一下 docker 仓库的加速镜像,就是下方代码的 registry-mirrors 这块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"features": {
"buildkit": true
},
"registry-mirrors": [
"https://hub-mirror.c.163.com"
]
}

最后点击 Apply & Restart ,进行重启,至此我们的 docker 配置就完事儿了。

若你能够魔法上网,这一大步你啥也不用配置,官方的就是最好的,镜像会有几率出问题,我遇到很多次了。

5. 创建测试项目

为了解决 linux 子系统与 windows 之间文件传输缓慢的问题,我们的代码直接放在linux中,不要放在 windows 里,我们打开 vs code,安装 remote - WSL 扩展,完成后点击窗口左下角,选择 new WSL window,vs code 会下载必要的数据,完成后,在左侧选择打开文件夹,我默认代码放在 /home/wangkai 的家目录中,点击确定即可

在左侧右键新建 code 文件夹,并对其右键,选择在集成终端中打开,在命令行中输入

1
curl -s https://laravel.build/example-app | bash

接下来就会自动安装代码到本地

6. 部署已有项目(已适配docker)并启动

更多情况下,我们的工程项目一般不是在 docker 环境跑的,基本都是在 lnmp 的环境下开发和部署(win 环境?异端!烧了!),比如我们经常使用的“本地 homestead 开发 + 线上 lnmp 部署”思路。下面我们就切换我们的开发方式,修改为“本地 docker 开发+线上 docker or lnmp 环境部署”的思路。

首先我们先在 /home/wangkai 目录下新建一个名为 code 的目录,我们之后项目代码都放在这。这里我多说一些为啥这样操作,我们把代码放在 linux 中,而非放在 windows 中,目的就是解决了这两个系统数据传输的缓慢问题,这一点在我们使用 homestead 环境的时候尤其明显,用过的都知道,homestead 是吧 virtualbox 的 linux 虚机的一个目录 与 windows 的一个文件夹做了同步,你在任意一个系统中操作的代码都会同步到另一端,这期间就牵扯到跨系统,固然牵扯到跨系统带来的读写缓慢,而这里我们的一切都在 linux 中操作,你的代码、数据库、redis、nginx 都在 Linux
中,不与 windows 做交互,速度自然高。

1
2
cd /home/wangkai
mkdir code

然后我们再从远端 git 仓库 clone 下一份全新的代码

1
2
cd ~/code
git clone git@github.com:NightingaleWK/yanjiv2.git yanjiv2

注意,这里我使用的是 ssh-key 的方式拉的代码到本地,你也可以使用 https 的方式,只不过需要每次都输入账号密码,用 ssh-key 的方法是实现了免密,比较方便。关于如何实现 ssh-key 的方式操作 git,请自行百度或者等我佛系更新一下。

之后我们复制一份 .env 出来,并根据实际修改其中的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
cd yanjiv2
cp .env.example .env
vim .env

然后开始你的修改就行了,修改完毕保存退出 .env,下面是修改的例子

APP_NAME=yanjiv2
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=yanjiv2
DB_USERNAME=sail
DB_PASSWORD=password

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

# 环境是HTTPS环境,则为 true,否则请填写 false
ADMIN_HTTPS=false

# 环境若为windows,则为 true,否则请填写 false。这里影响维修工单导出时图片的显示
MAINTENANCE_EXPORT_DEV_MODE=false

然后我们在 ubunut 中,跳转到项目根目录,执行如下代码来运行 composer install 指令

1
2
3
4
5
6
docker run --rm \
-u "$(id -u):$(id -g)" \
-v "$(pwd):/var/www/html" \
-w /var/www/html \
laravelsail/php82-composer:latest \
composer install --ignore-platform-reqs

这样我们就可以在 ubuntu 没装 composer 的情况下运行composer命令,这个命令使用一个包含 PHP 与 Composer 的小型 Docker 容器进行应用程序依赖的安装。

当你使用 laravelsail/phpXX-composer 镜像时,你应该选择和你的应用程序所用环境相同的 PHP 版本(74、80、81 或 82)

默认情况下,Sail 命令使用 vendor/bin/sail 脚本调用,该脚本已包含在所有新建的 laravel 应用程序中:

1
./vendor/bin/sail up

但与其重复的输入 vendor/bin/sail 来执行 Sail 命令,你可能会希望配置一个 Shell 别名方便你更容易的执行 Sail 命令:

1
alias sail='[ -f sail ] && sh sail || sh vendor/bin/sail'

为了确保这一点始终可用,你可以把它添加到你的主目录下的 shell 配置文件中,如 ~/.zshrc 或 ~/.bashrc ,然后重新启动你的 shell。

一旦配置了 shell 别名,你可以通过简单地输入 sail 来执行 Sail 命令。本文接下来的示例都假定你已经配置了此别名:

1
sail up

具体执行效果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
wangkai@PRECISION-7920:~$ cd code/yanjiv2/
wangkai@PRECISION-7920:~/code/yanjiv2$ sail up
[+] Running 65/7
✔ redis 6 layers [⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 25.3s
✔ mailpit 2 layers [⣿⣿] 0B/0B Pulled 44.8s
! laravel.test Warning 2.5s
✔ memcached 5 layers [⣿⣿⣿⣿⣿] 0B/0B Pulled 52.5s
✔ selenium 33 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 146.5s
✔ meilisearch 5 layers [⣿⣿⣿⣿⣿] 0B/0B Pulled 58.0s
✔ mysql 7 layers [⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 60.4s
[+] Building 568.7s (16/16) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 2.74kB 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:22.04 2.9s
=> [internal] load build context 0.0s
=> => transferring context: 907B 0.0s
=> [ 1/11] FROM docker.io/library/ubuntu:22.04@sha256:9dc05cf19a5745c33b9327dba850480dae80310972dea9b05052162e7c7f2763 7.1s
=> => resolve docker.io/library/ubuntu:22.04@sha256:9dc05cf19a5745c33b9327dba850480dae80310972dea9b05052162e7c7f2763 0.0s
=> => sha256:9dc05cf19a5745c33b9327dba850480dae80310972dea9b05052162e7c7f2763 1.20kB / 1.20kB 0.0s
=> => sha256:965fbcae990b0467ed5657caceaec165018ef44a4d2d46c7cdea80a9dff0d1ea 529B / 529B 0.0s
=> => sha256:6b7dfa7e8fdbe18ad425dd965a1049d984f31cf0ad57fa6d5377cca355e65f03 1.46kB / 1.46kB 0.0s
=> => sha256:6e3729cf69e0ce2de9e779575a1fec8b7fb5efdfa822829290ab6d5d1bc3e797 30.43MB / 30.43MB 6.2s
=> => extracting sha256:6e3729cf69e0ce2de9e779575a1fec8b7fb5efdfa822829290ab6d5d1bc3e797 0.7s
=> [ 2/11] WORKDIR /var/www/html 1.5s
=> [ 3/11] RUN ln -snf /usr/share/zoneinfo/UTC /etc/localtime && echo UTC > /etc/timezone 0.8s
=> [ 4/11] RUN apt-get update && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 dnsutils librsvg2-bin && curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&sear 542.9s
=> [ 5/11] RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.2 0.7s
=> [ 6/11] RUN groupadd --force -g 1000 sail 0.6s
=> [ 7/11] RUN useradd -ms /bin/bash --no-user-group -g 1000 -u 1337 sail 0.7s
=> [ 8/11] COPY start-container /usr/local/bin/start-container 0.1s
=> [ 9/11] COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf 0.1s
=> [10/11] COPY php.ini /etc/php/8.2/cli/conf.d/99-sail.ini 0.1s
=> [11/11] RUN chmod +x /usr/local/bin/start-container 0.7s
=> exporting to image 10.5s
=> => exporting layers 10.5s
=> => writing image sha256:ba56461e171b7ca24bba5c554ab4fa9af1dfee46f15e432132b6999b94bd07b4 0.0s
=> => naming to sail-8.2/app 0.0s
[+] Running 11/11
✔ Network yanjiv2_sail Created 0.1s
✔ Volume "yanjiv2_sail-meilisearch" Created 0.0s
✔ Volume "yanjiv2_sail-mysql" Created 0.0s
✔ Volume "yanjiv2_sail-redis" Created 0.0s
✔ Container yanjiv2-mysql-1 Created 0.2s
✔ Container yanjiv2-selenium-1 Created 0.2s
✔ Container yanjiv2-redis-1 Created 0.2s
✔ Container yanjiv2-memcached-1 Created 0.2s
✔ Container yanjiv2-meilisearch-1 Created 0.2s
✔ Container yanjiv2-mailpit-1 Created 0.2s
✔ Container yanjiv2-laravel.test-1 Created 0.1s
Attaching to yanjiv2-laravel.test-1, yanjiv2-mailpit-1, yanjiv2-meilisearch-1, yanjiv2-memcached-1, yanjiv2-mysql-1, yanjiv2-redis-1, yanjiv2-selenium-1
yanjiv2-selenium-1 | 2023-05-05 06:47:12,672 INFO Included extra file "/etc/supervisor/conf.d/selenium.conf" during parsing
yanjiv2-selenium-1 | 2023-05-05 06:47:12,676 INFO RPC interface 'supervisor' initialized
yanjiv2-selenium-1 | 2023-05-05 06:47:12,677 CRIT Server 'unix_http_server' running without any HTTP authentication checking
yanjiv2-selenium-1 | 2023-05-05 06:47:12,677 INFO supervisord started with pid 8
yanjiv2-redis-1 | 1:C 05 May 2023 06:47:12.892 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
yanjiv2-redis-1 | 1:C 05 May 2023 06:47:12.892 # Redis version=7.0.11, bits=64, commit=00000000, modified=0, pid=1, just started
yanjiv2-redis-1 | 1:C 05 May 2023 06:47:12.892 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
yanjiv2-redis-1 | 1:M 05 May 2023 06:47:12.893 * monotonic clock: POSIX clock_gettime
yanjiv2-redis-1 | 1:M 05 May 2023 06:47:12.893 * Running mode=standalone, port=6379.
yanjiv2-redis-1 | 1:M 05 May 2023 06:47:12.893 # Server initialized
yanjiv2-redis-1 | 1:M 05 May 2023 06:47:12.893 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
yanjiv2-redis-1 | 1:M 05 May 2023 06:47:12.894 * Ready to accept connections
yanjiv2-mysql-1 | [Entrypoint] MySQL Docker Image 8.0.32-1.2.11-server
yanjiv2-mailpit-1 | INFO[2023/05/05 06:47:13] [smtpd] starting on 0.0.0.0:1025
yanjiv2-mailpit-1 | INFO[2023/05/05 06:47:13] [http] starting server on http://0.0.0.0:8025/
yanjiv2-mysql-1 | [Entrypoint] Initializing database
yanjiv2-mysql-1 | 2023-05-05T06:47:13.388634Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
yanjiv2-mysql-1 | 2023-05-05T06:47:13.388730Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.32) initializing of server in progress as process 17
yanjiv2-mysql-1 | 2023-05-05T06:47:13.399393Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
yanjiv2-selenium-1 | 2023-05-05 06:47:13,681 INFO spawned: 'xvfb' with pid 10
yanjiv2-selenium-1 | 2023-05-05 06:47:13,683 INFO spawned: 'vnc' with pid 11
yanjiv2-selenium-1 | 2023-05-05 06:47:13,686 INFO spawned: 'novnc' with pid 13
yanjiv2-selenium-1 | 2023-05-05 06:47:13,691 INFO spawned: 'selenium-standalone' with pid 18
yanjiv2-selenium-1 | Setting up SE_NODE_GRID_URL...
yanjiv2-selenium-1 | 2023-05-05 06:47:13,705 INFO success: xvfb entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
yanjiv2-selenium-1 | 2023-05-05 06:47:13,705 INFO success: vnc entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
yanjiv2-selenium-1 | 2023-05-05 06:47:13,705 INFO success: novnc entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
yanjiv2-selenium-1 | 2023-05-05 06:47:13,705 INFO success: selenium-standalone entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
yanjiv2-selenium-1 | Selenium Grid Standalone configuration:
yanjiv2-selenium-1 | [network]
yanjiv2-selenium-1 | relax-checks = true
yanjiv2-selenium-1 |
yanjiv2-selenium-1 | [node]
yanjiv2-selenium-1 | session-timeout = "300"
yanjiv2-selenium-1 | override-max-sessions = false
yanjiv2-selenium-1 | detect-drivers = false
yanjiv2-selenium-1 | drain-after-session-count = 0
yanjiv2-selenium-1 | max-sessions = 1
yanjiv2-selenium-1 |
yanjiv2-selenium-1 | [[node.driver-configuration]]
yanjiv2-selenium-1 | display-name = "chrome"
yanjiv2-selenium-1 | stereotype = '{"browserName": "chrome", "browserVersion": "112.0", "platformName": "Linux"}'
yanjiv2-selenium-1 | max-sessions = 1
yanjiv2-selenium-1 |
yanjiv2-selenium-1 | Starting Selenium Grid Standalone...
yanjiv2-selenium-1 | Tracing is disabled
yanjiv2-mysql-1 | 2023-05-05T06:47:14.006754Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
yanjiv2-selenium-1 | 06:47:14.251 INFO [LoggingOptions.configureLogEncoding] - Using the system default encoding
yanjiv2-selenium-1 | 06:47:14.258 INFO [OpenTelemetryTracer.createTracer] - Using OpenTelemetry for tracing
yanjiv2-laravel.test-1 | 2023-05-05 06:47:14,290 INFO Set uid to user 0 succeeded
yanjiv2-laravel.test-1 | 2023-05-05 06:47:14,292 INFO supervisord started with pid 1
yanjiv2-selenium-1 | 06:47:15.076 INFO [NodeOptions.getSessionFactories] - Detected 20 available processors
yanjiv2-selenium-1 | 06:47:15.134 INFO [NodeOptions.report] - Adding chrome for {"browserVersion": "112.0","se:noVncPort": 7900,"browserName": "chrome","platformName": "LINUX","se:vncEnabled": true} 1 times (Host)
yanjiv2-selenium-1 | 06:47:15.153 INFO [Node.<init>] - Binding additional locator mechanisms: relative
yanjiv2-selenium-1 | 06:47:15.176 INFO [GridModel.setAvailability] - Switching Node 9fac02ef-1d70-409d-8b15-a74e93fb0f25 (uri: http://172.18.0.2:4444) from DOWN to UP
yanjiv2-selenium-1 | 06:47:15.177 INFO [LocalDistributor.add] - Added node 9fac02ef-1d70-409d-8b15-a74e93fb0f25 at http://172.18.0.2:4444. Health check every 120s
yanjiv2-laravel.test-1 | 2023-05-05 06:47:15,295 INFO spawned: 'php' with pid 16
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | 888b d888 d8b 888 d8b 888
yanjiv2-meilisearch-1 | 8888b d8888 Y8P 888 Y8P 888
yanjiv2-meilisearch-1 | 88888b.d88888 888 888
yanjiv2-meilisearch-1 | 888Y88888P888 .d88b. 888 888 888 .d8888b .d88b. 8888b. 888d888 .d8888b 88888b.
yanjiv2-meilisearch-1 | 888 Y888P 888 d8P Y8b 888 888 888 88K d8P Y8b "88b 888P" d88P" 888 "88b
yanjiv2-meilisearch-1 | 888 Y8P 888 88888888 888 888 888 "Y8888b. 88888888 .d888888 888 888 888 888
yanjiv2-meilisearch-1 | 888 " 888 Y8b. 888 888 888 X88 Y8b. 888 888 888 Y88b. 888 888
yanjiv2-meilisearch-1 | 888 888 "Y8888 888 888 888 88888P' "Y8888 "Y888888 888 "Y8888P 888 888
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | Config file path: "none"
yanjiv2-meilisearch-1 | Database path: "./data.ms"
yanjiv2-meilisearch-1 | Server listening on: "http://0.0.0.0:7700"
yanjiv2-meilisearch-1 | Environment: "development"
yanjiv2-meilisearch-1 | Commit SHA: "4b953d62fbab81278324e71b4037eb06355dd49a"
yanjiv2-meilisearch-1 | Commit date: "2023-04-13T16:24:16+00:00"
yanjiv2-meilisearch-1 | Package version: "1.1.1"
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | Thank you for using Meilisearch!
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | We collect anonymized analytics to improve our product and your experience. To learn more, including how to turn off analytics, visit our dedicated documentation page: https://docs.meilisearch.com/learn/what_is_meilisearch/telemetry.html
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | Anonymous telemetry: "Enabled"
yanjiv2-meilisearch-1 | Instance UID: "073aa49c-374d-4af8-bb0c-5e0b8ef95b38"
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | No master key was found. The server will accept unidentified requests.
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | A master key of at least 16 bytes will be required when switching to a production environment.
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | We generated a new secure master key for you (you can safely use this token):
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | >> --master-key XVIX1t9QZsTQN6nz7uSS8Y71qPUIdohggUxIWeCR8gg <<
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | Restart Meilisearch with the argument above to use this new and secure master key.
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | Documentation: https://docs.meilisearch.com
yanjiv2-meilisearch-1 | Source code: https://github.com/meilisearch/meilisearch
yanjiv2-meilisearch-1 | Contact: https://docs.meilisearch.com/resources/contact.html
yanjiv2-meilisearch-1 |
yanjiv2-meilisearch-1 | [2023-05-05T06:47:15Z INFO actix_server::builder] Starting 10 workers
yanjiv2-meilisearch-1 | [2023-05-05T06:47:15Z INFO actix_server::server] Actix runtime found; starting in Actix runtime
yanjiv2-selenium-1 | 06:47:15.381 INFO [Standalone.execute] - Started Selenium Standalone 4.9.0 (revision d7057100a6): http://172.18.0.2:4444
yanjiv2-laravel.test-1 | Starting laravel development server: http://0.0.0.0:80
yanjiv2-laravel.test-1 | [Fri May 5 06:47:15 2023] PHP 8.2.5 Development Server (http://0.0.0.0:80) started
yanjiv2-mysql-1 | 2023-05-05T06:47:16.298633Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
yanjiv2-laravel.test-1 | 2023-05-05 06:47:17,205 INFO success: php entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
yanjiv2-mysql-1 | [Entrypoint] Database initialized
yanjiv2-mysql-1 | 2023-05-05T06:47:21.867567Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
yanjiv2-mysql-1 | 2023-05-05T06:47:21.869445Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) starting as process 60
yanjiv2-mysql-1 | 2023-05-05T06:47:21.903451Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
yanjiv2-mysql-1 | 2023-05-05T06:47:22.074419Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
yanjiv2-mysql-1 | 2023-05-05T06:47:22.447280Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
yanjiv2-mysql-1 | 2023-05-05T06:47:22.447388Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
yanjiv2-mysql-1 | 2023-05-05T06:47:22.494594Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
yanjiv2-mysql-1 | 2023-05-05T06:47:22.494863Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.32' socket: '/var/lib/mysql/mysql.sock' port: 0 MySQL Community Server - GPL.
yanjiv2-mysql-1 | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
yanjiv2-mysql-1 | Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
yanjiv2-mysql-1 | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
yanjiv2-mysql-1 | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
yanjiv2-mysql-1 | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
yanjiv2-mysql-1 |
yanjiv2-mysql-1 | [Entrypoint] running /docker-entrypoint-initdb.d/10-create-testing-database.sh
yanjiv2-mysql-1 | mysql: [Warning] Using a password on the command line interface can be insecure.
yanjiv2-mysql-1 |
yanjiv2-mysql-1 | 2023-05-05T06:47:25.076164Z 15 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.32).
yanjiv2-mysql-1 | 2023-05-05T06:47:26.390283Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.32) MySQL Community Server - GPL.
yanjiv2-mysql-1 | [Entrypoint] Server shut down
yanjiv2-mysql-1 |
yanjiv2-mysql-1 | [Entrypoint] MySQL init process done. Ready for start up.
yanjiv2-mysql-1 |
yanjiv2-mysql-1 | [Entrypoint] Starting MySQL 8.0.32-1.2.11-server
yanjiv2-mysql-1 | 2023-05-05T06:47:27.300132Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
yanjiv2-mysql-1 | 2023-05-05T06:47:27.301672Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.32) starting as process 1
yanjiv2-mysql-1 | 2023-05-05T06:47:27.316952Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
yanjiv2-mysql-1 | 2023-05-05T06:47:27.452872Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
yanjiv2-mysql-1 | 2023-05-05T06:47:27.736549Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
yanjiv2-mysql-1 | 2023-05-05T06:47:27.736619Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
yanjiv2-mysql-1 | 2023-05-05T06:47:27.770036Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
yanjiv2-mysql-1 | 2023-05-05T06:47:27.770200Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.32' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server - GPL.
yanjiv2-laravel.test-1 | [Fri May 5 06:47:35 2023] 172.18.0.1:44704 Accepted
yanjiv2-laravel.test-1 | [Fri May 5 06:47:35 2023] 172.18.0.1:44704 Closing
yanjiv2-laravel.test-1 | [Fri May 5 06:47:35 2023] 172.18.0.1:44708 Accepted
yanjiv2-laravel.test-1 | [Fri May 5 06:47:35 2023] 172.18.0.1:44708 Closing
yanjiv2-laravel.test-1 | [Fri May 5 06:47:35 2023] 172.18.0.1:44716 Accepted
yanjiv2-laravel.test-1 | [Fri May 5 06:47:35 2023] 172.18.0.1:44716 Closing
yanjiv2-laravel.test-1 | [Fri May 5 06:47:35 2023] 172.18.0.1:44730 Accepted
yanjiv2-laravel.test-1 | [Fri May 5 06:47:35 2023] 172.18.0.1:44730 Closing
yanjiv2-laravel.test-1 | [Fri May 5 06:47:36 2023] 172.18.0.1:44744 Accepted
yanjiv2-laravel.test-1 | [Fri May 5 06:47:36 2023] 172.18.0.1:44744 Closing
yanjiv2-laravel.test-1 | [Fri May 5 06:47:36 2023] 172.18.0.1:44750 Accepted
yanjiv2-laravel.test-1 | [Fri May 5 06:47:36 2023] 172.18.0.1:44750 [200]: GET /favicon.ico
yanjiv2-laravel.test-1 | [Fri May 5 06:47:36 2023] 172.18.0.1:44750 Closing
yanjiv2-meilisearch-1 | [2023-05-05T06:47:43Z INFO actix_web::middleware::logger] 127.0.0.1 "GET /health HTTP/1.1" 200 22 "-" "Wget" 0.000301

如果没出意外的话,现在服务应该起来了,访问一下 localhost 就可以看到熟悉的 laravel 欢迎界面了。

若提示“No application encryption key has been specified.”,点击报错页面的“Generate app key”,然后刷新一下就好了

之后就和正常继续部署项目一样,但是记得把每行指令的 php artisan 改为 sail artisan,比如:

1
2
3
4
5
6
7
php artisan migrate:refresh --seed
// to
sail artisan migrate:refresh --seed

composer i
// to
sail composer i

7. 部署已有项目(未适配 docker)并启动

更多情况下,我们的工程项目一般不是在 docker 环境跑的,基本都是在 lnmp 的环境下开发和部署(win 环境?异端!烧了!),比如我们经常使用的“本地 homestead 开发 + 线上 lnmp 部署”思路。下面我们就切换我们的开发方式,修改为“本地 docker 开发+线上 docker or lnmp 环境部署”的思路。

首先我们先在 /home/wangkai 目录下新建一个名为 code 的目录,我们之后项目代码都放在这。这里我多说一些为啥这样操作,我们把代码放在 linux 中,而非放在 windows 中,目的就是解决了这两个系统数据传输的缓慢问题,这一点在我们使用 homestead 环境的时候尤其明显,用过的都知道,homestead 是吧 virtualbox 的 linux 虚机的一个目录 与 windows 的一个文件夹做了同步,你在任意一个系统中操作的代码都会同步到另一端,这期间就牵扯到跨系统,固然牵扯到跨系统带来的读写缓慢,而这里我们的一切都在 linux 中操作,你的代码、数据库、redis、nginx 都在 Linux
中,不与 windows 做交互,速度自然高。

1
2
cd /home/wangkai
mkdir code

然后我们再从远端 git 仓库 clone 下一份全新的代码

1
2
cd ~/code
git clone git@github.com:NightingaleWK/yanjiv2.git yanjiv2

注意,这里我使用的是 ssh-key 的方式拉的代码到本地,你也可以使用 https 的方式,只不过需要每次都输入账号密码,用 ssh-key 的方法是实现了免密,比较方便。关于如何实现 ssh-key 的方式操作 git,请自行百度或者等我佛系更新一下。

之后我们复制一份 .env 出来,并根据实际修改其中的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
cd yanjiv2
cp .env.example .env
vim .env

然后开始你的修改就行了,修改完毕保存退出 .env,下面是修改的例子

APP_NAME=yanjiv2
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yanjiv2
DB_USERNAME=yanjiv2
DB_PASSWORD=yanjiv2

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

# 环境是HTTPS环境,则为 true,否则请填写 false
ADMIN_HTTPS=false

# 环境若为windows,则为 true,否则请填写 false。这里影响维修工单导出时图片的显示
MAINTENANCE_EXPORT_DEV_MODE=false

本小节未完待续…
[1]: https://www.docker.com/