Laravel Homestead下的环境配置和安装

WechatIMG3.jpeg

环境

  • VirtualBox 6.1.10

VitrualBox 是一个非常强大的免费虚拟机软件。

  • Vagrant 2.2.9

Vagrant 是一个用于创建和部署虚拟化开发环境的工具,依赖于 VirtualBox 虚拟机。

  • Homestead 0.6.0

HomesteadBox 是一个 Laravel 官方预装的适合 Laravel 开发的 Vagrant box。其中内置了 Laravel 开发时所需要用到的各种软件,例如 Ubuntu 14.04、Git、PHP 5.6 / 7.0、Xdebug、HHVM、Nginx、MySQL、Sqlite3、Postgres、Composer……

安装VirtualBox

  • 下载

www.virtualbox.org_wiki_Downloads.png

访问 VirtualBox 官网,选择当前操作系统相对应的安装包进行下载。当前的操作系统是 macOS,这里选择的是 OS X hosts。

  • 安装

Screen Shot 2020-07-14 at 2.48.22 PM.png

下载完成后,双击打开按照提示完成安装即可。

安装Vagrant

  • 下载

www.vagrantup.com_downloads.html.png

访问 Vagrant 官网,选择当前操作系统相对应的安装包进行下载。这里选择的是 Mac OS X 64-bit。

  • 安装

Screen Shot 2020-07-14 at 2.56.45 PM.png

下载完成后,双击打开按照提示完成安装即可。

安装HomesteadBox

  • 下载

pan.baidu.com_s_1hrN55w4.png

这里手动安装 HomesteadBox ,访问 大佬的网盘,选择最新的版本进行下载。这里选择的是 homestead-virtualbox-0.6.0.box。

  • 创建 metadata.json,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"name": "laravel/homestead",
"versions":
[
{
"version": "0.6.0",
"providers": [
{
"name": "virtualbox",
"url": "homestead-virtualbox-0.6.0.box"
}
]
}
]
}

这里创建的 metadata.json 要与下载的 homestead-virtualbox-0.6.0.box 在同一级目录。

  • 添加

进入 metadata.json 所在目录,执行vagrant box add metadata.json把 homestead-virtualbox-0.6.0.box 添加到 vagrant box。

  • 查看
1
2
$ vagrant box list
laravel/homestead (virtualbox, 0.6.0)

安装Homestead

  • 克隆
1
$ git clone https://github.com/laravel/homestead.git Homestead

克隆完后,会生成一个 Homestead 文件夹,进入该目录执行bash init.sh会在根目录生成 Homestead.yaml 配置文件。

  • 修改 Homestead.yaml 内容如下:
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
---
ip: "192.168.10.10"
# 虚拟机的 ip

version: "0.6.0"
# 虚拟机对应 HomesteadBox 版本

memory: 2048
# 虚拟机的内存

cpus: 2
# 虚拟机的 CPU

provider: virtualbox
# 虚拟机的默认提供者

authorize: ~/.ssh/id_rsa.pub
# 公钥

keys:
- ~/.ssh/id_rsa
# 私钥

folders:
- map: /private/var/HaI
to: /home/vagrant/Code
type: "nfs"
# 共享本地的目录和虚拟机的目录
# map 本地的目录
# to 虚拟机的目录
# nfs 开启解决站点响应缓慢

sites:
- map: homestead.test
to: /home/vagrant/Code/Laravel/public
# 域名映射到虚拟机中项目目录
# map 域名
# to 虚拟机中项目目录

databases:
- homestead
# 数据库

features:
- mariadb: false
- ohmyzsh: false
- webdriver: false

# ports:
# - send: 50000
# to: 5000
# - send: 7777
# to: 777
# protocol: udp

这里新增了 version 配置,修改了 folders 和 sites 配置。

创建项目

  • 启动虚拟机

在 Homestead 根目录下执行vagrant up启动虚拟机。

  • 加载配置

每次更新 Homestead.yaml 文件夹后,需要执行vagrant provision让新的配置生效。

  • 登录虚拟机

虚拟机启动后,再执行 vagrant ssh 登录虚拟机。

  • 修改 php.ini
1
memory_limit = -1

因为 composer 下载的依赖包超过了 php 默认内存限制,所以这里改为 -1(不做限制)后面记得改回来.

  • 重启 php
1
sudo service php7.0-fpm restart
  • 修改 homestead.test
1
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

找到 nginx 目录下的 sites-available,修改 homestead.test。因为 fastcgi 默认运行的是 php7.4-fpm,这里改为 7.0。

  • 重启 nginx
1
sudo service nginx restart
  • 下载 Laravel 源码
1
2
$ composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
$ composer create-project laravel/laravel Laravel --prefer-dist "5.1.*"

在 /home/vagrant/Code 目录下使用 composer 下载。下载完后,会生成一个 Laravel 文件夹。

  • 修改 app.php 内容如下:
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
<?php

return [

/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/

'debug' => env('APP_DEBUG', true),
// 进行本地开发时,你应该配置 APP_DEBUG 环境变量为 true。 在上线环境,这个值应该永远为 false

/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/

'url' => 'http://localhost',

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'UTC',

/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/

'locale' => 'en',

/*
|--------------------------------------------------------------------------
| Application Fallback Locale
|--------------------------------------------------------------------------
|
| The fallback locale determines the locale to use when the current one
| is not available. You may change the value to correspond to any of
| the language folders that are provided through your application.
|
*/

'fallback_locale' => 'en',

/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is used by the Illuminate encrypter service and should be set
| to a random, 32 character string, otherwise these encrypted strings
| will not be safe. Please do this before deploying an application!
|
*/

'key' => env('APP_KEY', 'lgfaRyY25gtDF5FGqLbRc6pKOnJ6rUKz'),
// 应用程序密钥是一个 32 位字符的随机字符串,存储在根目录 .env 文件中的 APP_KEY 密钥。也可以执行 php artisan key:generate 来生成。

'cipher' => 'AES-256-CBC',

/*
|--------------------------------------------------------------------------
| Logging Configuration
|--------------------------------------------------------------------------
|
| Here you may configure the log settings for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Settings: "single", "daily", "syslog", "errorlog"
|
*/

'log' => env('APP_LOG', 'single'),

/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/

'providers' => [

/*
* Laravel Framework Service Providers...
*/
Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Routing\ControllerServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
Illuminate\Hashing\HashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,

/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,

],

/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/

'aliases' => [

'App' => Illuminate\Support\Facades\App::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
'Auth' => Illuminate\Support\Facades\Auth::class,
'Blade' => Illuminate\Support\Facades\Blade::class,
'Bus' => Illuminate\Support\Facades\Bus::class,
'Cache' => Illuminate\Support\Facades\Cache::class,
'Config' => Illuminate\Support\Facades\Config::class,
'Cookie' => Illuminate\Support\Facades\Cookie::class,
'Crypt' => Illuminate\Support\Facades\Crypt::class,
'DB' => Illuminate\Support\Facades\DB::class,
'Eloquent' => Illuminate\Database\Eloquent\Model::class,
'Event' => Illuminate\Support\Facades\Event::class,
'File' => Illuminate\Support\Facades\File::class,
'Gate' => Illuminate\Support\Facades\Gate::class,
'Hash' => Illuminate\Support\Facades\Hash::class,
'Input' => Illuminate\Support\Facades\Input::class,
'Lang' => Illuminate\Support\Facades\Lang::class,
'Log' => Illuminate\Support\Facades\Log::class,
'Mail' => Illuminate\Support\Facades\Mail::class,
'Password' => Illuminate\Support\Facades\Password::class,
'Queue' => Illuminate\Support\Facades\Queue::class,
'Redirect' => Illuminate\Support\Facades\Redirect::class,
'Redis' => Illuminate\Support\Facades\Redis::class,
'Request' => Illuminate\Support\Facades\Request::class,
'Response' => Illuminate\Support\Facades\Response::class,
'Route' => Illuminate\Support\Facades\Route::class,
'Schema' => Illuminate\Support\Facades\Schema::class,
'Session' => Illuminate\Support\Facades\Session::class,
'Storage' => Illuminate\Support\Facades\Storage::class,
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,

],

];

在 /home/vagrant/Code/Laravel/config 目录修改 app.php,这里修改了 APP_DEBUG 和 APP_KEY 配置。

访问项目

  • 配置 hosts

修改本地 hosts 文件,新增192.168.10.10 homestead.test

  • 访问

homestead.test_.png

浏览器打开 http://homestead.test/ ,如上图所示,成功访问。

关联

[[Laravel Homestead下安装phpredis扩展]]

-------------本文结束感谢您的阅读-------------
0%