LoginSignup
2
2

More than 3 years have passed since last update.

Laravel Homesteadでササッと環境構築して、Hello worldしてみよう!

Last updated at Posted at 2019-10-09

What is Laravel Homestead?

laravel.com より

Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine.

意訳: Laravel公式のVargrant boxだよ。必要なソフトは最初からインストールされてるよ。

含まれているソフトウエア

  • Ubuntu 18.04
  • Git
  • PHP 7.3
  • PHP 7.2
  • PHP 7.1
  • Nginx
  • MySQL
  • lmm for MySQL or MariaDB database snapshots
  • Sqlite3
  • PostgreSQL
  • Composer
  • Node (With Yarn, Bower, Grunt, and Gulp)
  • Redis
  • Memcached
  • Beanstalkd
  • Mailhog
  • avahi
  • ngrok
  • Xdebug
  • XHProf / Tideways / XHGui
  • wp-cli
  • Minio

手順

  1. VirtualBoxをインストール

  2. vagrantをインストール

  3. boxを追加

   #仮想マシンのテンプレート
   $ vagrant box add laravel/homestead
  1. githubからlaravel/homesteadをクローン
   # 環境設定ファイルが含まれている
   $ git clone https://github.com/laravel/homestead.git ~/Homestead
  1. 環境設定ファイルの作成
   $ cd ~/Homestead
   $ git checkout release
   $ bash init.sh
  1. Homestead.ymlの編集
   #必要に応じて適宜変更
   $vim Homestead.yml
   #仮想マシンに割り当てるIPアドレス
   ip: "192.168.10.10" #ローカルマシンと異なるNWセグメントの値を設定する
   memory: 2048 #メモリ
   cpus: 2      #CPU
   provider: virtualbox #vagrantで操作する仮想化ソフト

   authorize: ~/.ssh/id_rsa.pub #vagrant sshで使う公開鍵

   keys:
       - ~/.ssh/id_rsa #vagrant sshで使う秘密鍵

   #ローカルマシンと仮想マシンのファイル共有設定
   folders:
       - map: ~/code #ローカルのファイルパス
         to: /home/vagrant/code #仮想マシンのパス

   sites:
       - map: homestead.test #ドメイン
         to: /home/vagrant/code/public #ドキュメントルート

   databases:
       - homestead #データベース名
  1. 仮想マシンの起動
   #しばらく時間がかかる
   $ vagrant up
  1. 仮想マシンに接続
   $ vagrant ssh
   Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-47-generic x86_64)

   Thanks for using
    _                               _                 _
   | |                             | |               | |
   | |__   ___  _ __ ___   ___  ___| |_ ___  __ _  __| |
   | '_ \ / _ \| '_ ` _ \ / _ \/ __| __/ _ \/ _` |/ _` |
   | | | | (_) | | | | | |  __/\__ \ ||  __/ (_| | (_| |
   |_| |_|\___/|_| |_| |_|\___||___/\__\___|\__,_|\__,_|

   * Homestead 8.4.0 released!
   * Settler v7.2.1 released! Make sure you update

   0 packages can be updated.
   0 updates are security updates.
   vagrant@homestead:~$
  1. Laravelプロジェクトの作成
   $ vagrant@homestead:~$ cd ~/code
   $ vagrant@homestead:~/code$ laravel new
  1. Homestead.ymlのIPに設定したアドレスにブラウザでアクセスする

  2. Laravelと表示されれば成功

  3. ディレクトリ構成見てみる

    #Homestead.ymlのfoldersで設定したローカルディレクトリ
    $ cd ~/code
    $ ls -la
    drwxr-xr-x   7 tsubokoh  staff   238B  6  9 17:22 app
    -rw-r--r--   1 tsubokoh  staff   1.6K  6  9 17:22 artisan
    drwxr-xr-x   4 tsubokoh  staff   136B  6  9 17:22 bootstrap
    -rw-r--r--   1 tsubokoh  staff   1.5K  6  9 17:22 composer.json
    -rw-r--r--   1 tsubokoh  staff   163K  6  9 17:22 composer.lock
    drwxr-xr-x  15 tsubokoh  staff   510B  6  9 17:22 config
    drwxr-xr-x   6 tsubokoh  staff   204B  6  9 17:22 database
    -rw-r--r--   1 tsubokoh  staff   1.1K  6  9 17:22 package.json
    -rw-r--r--   1 tsubokoh  staff   1.1K  6  9 17:22 phpunit.xml
    drwxr-xr-x   8 tsubokoh  staff   272B  6  9 17:22 public
    drwxr-xr-x   6 tsubokoh  staff   204B  6  9 17:22 resources
    drwxr-xr-x   6 tsubokoh  staff   204B  6  9 17:40 routes
    -rw-r--r--   1 tsubokoh  staff   563B  6  9 17:22 server.php
    drwxr-xr-x   5 tsubokoh  staff   170B  6  9 17:22 storage
    drwxr-xr-x   6 tsubokoh  staff   204B  6  9 17:22 tests
    drwxr-xr-x  41 tsubokoh  staff   1.4K  6  9 17:25 vendor
    -rw-r--r--   1 tsubokoh  staff   538B  6  9 17:22 webpack.mix.js
    -rw-r--r--   1 tsubokoh  staff   200K  6  9 17:22 yarn.lock
    
  4. ルーティング見てみる

    $ cat routes/web.php
    
    <?php
    
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */
    
    Route::get('/', function () { #ルートパスにgetリクエストされたとき
        return view('welcome'); #welcomeテンプレートを返す
    });
    
  5. テンプレートを編集する

    #BladeはLaravelに標準搭載されているテンプレートエンジン
    #独自の記法がある
    $vi resources/views/welcome.blade.php
    
            --中略--
                    <div class="title m-b-md">
                        {{-- Laravel コメントアウト --}}
    
                        @php
                         #phpの実行
                         #viewでロジックを持つことは非推奨
                         $title = 'Hello world!';
                        @endphp
    
                        {{-- echoはいらない --}}
                        {{ $title }}
                    </div>
            --中略--
    
  6. Homestead.ymlのIPに設定したアドレスにブラウザでアクセスする

  7. Hello world!と表示されれば成功

以上

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2