LoginSignup
20
23

More than 5 years have passed since last update.

複数のLaravel環境をHomestead上で動かす

Posted at

はじめに

この記事では複数のLaravel環境をHomestead上で動かそうとした際にどう対応したかを書きます。

複数のLaravel環境を動かす方法

調べたところ以下の方法があるようです

  • virtualbox上に複数のHomesteadを立てる

 参考
 https://qiita.com/you-me/items/b886dd0fc8e5047c4bc6

  • Homesteadは1個でその中に複数のLaravelを立てる

 参考
 https://qiita.com/Yorinton/items/08ec8fefcbec71513399
 http://www.dn-web64.com/archives/web/homestead2/

で、今回は後者の「Homesteadは1個でその中に複数のLaravelを立てる」をやります。
内容的には参考にさせて頂いた記事を自分なりに解釈しアレンジしたものになります。

前提

1つめのLaravelのインストールまでは終わっている状態を前提とします。

Laravel(2つめ)のインストール

composerを使ってインストールしたいディレクトリにインストールします

$ composer create-project laravel/laravel laravel-two --prefer-dist

Homestead.yamlの編集


folders:
    - map: ~/Homestead/laravel
      to: /home/vagrant/code/laravel

    - map: ~/Homestead/laravel-two
      to: /home/vagrant/code/laravel-two

sites:
    - map: homestead.app
      to: /home/vagrant/code/laravel/public

    - map: homestead-two.app
      to: /home/vagrant/code/laravel-two/public

hostsへの追加

192.168.10.10   homestead-two.app

serveスクリプト

ここはprovisionと迷ったのですが一旦serveスクリプトでいきます。
provisionでも全然いいと思います。

$ vagrant ssh
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-92-generic x86_64)

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

3 packages can be updated.
0 updates are security updates.


Last login: Wed Jan 10 08:15:19 2018 from 10.0.2.2
vagrant@homestead:~$ 
vagrant@homestead:~$ serve homestead-two.app /home/vagrant/code/laravel-two/public
dos2unix: converting file /vagrant/scripts/serve-laravel.sh to Unix format ...
vagrant@homestead:~$ sudo service nginx restart
vagrant@homestead:~$ exit

この状態でブラウザからlaravel-two.appにアクセスし、No File Specifiedと出る場合には
一旦vagrantをリロードしてみましょう(私はリロードしました)

$ vagrant reload

migrationなどでエラーが出る場合

No such file or directory

注釈:このエラーはおそらく上述したようにlaravelを新たにインストールした場合ではなく、github等からlaravelベースのソースをcloneして配置した場合に起こるようです。

console
PHP Warning:  require(/home/vagrant/code/laravel-two/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /home/vagrant/code/laravel-two/bootstrap/autoload.php on line 17

Warning: require(/home/vagrant/code/laravel-two/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /home/vagrant/code/laravel-two/bootstrap/autoload.php on line 17
PHP Fatal error:  require(): Failed opening required '/home/vagrant/code/laravel-two/bootstrap/../vendor/autoload.php' (include_path='.:/usr/share/php') in /home/vagrant/code/laravel-two/bootstrap/autoload.php on line 17

Fatal error: require(): Failed opening required '/home/vagrant/code/laravel-two/bootstrap/../vendor/autoload.php' (include_path='.:/usr/share/php') in /home/vagrant/code/laravel-two/bootstrap/autoload.php on line 17

私の場合、composerのインストールで解決しました。

console
vagrant@homestead:~/code/laravel-two$ composer install

テーブルの作成でエラーになる

console
[Illuminate\Database\QueryException]                                                                         
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table `u  
  sers` (`id` int unsigned not null auto_increment primary key, `email` varchar(255) not null, `password` var  
  char(255) not null, `permissions` text null, `last_login` timestamp null, `user_name` varchar(255) not null  
  , `company_name` varchar(255) null, `created_at` timestamp null, `updated_at` timestamp null, `deleted_at`   
  timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci engine = InnoDB)

[PDOException]                                                                         
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists

私の場合、Databaseを分けることにしました。
まずはHomestead.yamlを編集します。


folders:
    - map: ~/Homestead/laravel
      to: /home/vagrant/code/laravel

    - map: ~/Homestead/laravel-two
      to: /home/vagrant/code/laravel-two

sites:
    - map: homestead.app
      to: /home/vagrant/code/laravel/public

    - map: homestead-two.app
      to: /home/vagrant/code/laravel-two/public

databases:
    - homestead
    - homestead-two

あと.envも

.env
DB_DATABASE=homestead-two

編集したら一旦リロード

console
$ vagrant reload

リロード後にvagrant sshで再度入って、Databaseを作ります

console
vagrant@homestead:~/code/laravel-two$ mysql -u homestead -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> CREATE DATABASE homestead-two;
Query OK, 1 row affected (0.01 sec)

無事動きました。
以上です

20
23
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
20
23