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,内容如下:
{
    "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。

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

安装Homestead

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

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

  • 修改 Homestead.yaml 内容如下:
---
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
memory_limit = -1

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

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

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

  • 重启 nginx
sudo service nginx restart
  • 下载 Laravel 源码
$ 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 内容如下:
<?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/ ,如上图所示,成功访问。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,475评论 4 372
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,744评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,101评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,732评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,141评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,049评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,188评论 2 320
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,965评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,716评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,867评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,341评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,663评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,376评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,200评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,990评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,179评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,979评论 2 279