Ikeda's Blog

Laravelでブログサイトを作る 05. Laravel開発環境の用意

はじめに

Windows11環境に、VirtualBox+Vagrantを使用して環境を作成します。
上記の環境構築方法については前回を御覧ください。

仮想環境に導入するもの

  • CentOS 7.9
  • PHP 7.4
  • MySQL 8.0
  • composer 2.1
  • git 1.8.3.1
  • nodejs 16.13.1

Vagrantfile

vagrantを利用して仮想サーバを作成します。
今回使用するVagrantfileは以下になります。

Vagrant.configure("2") do |config|
    # Box名
    config.vm.box = "CentOS7"

    # ホストオンリーの設定にIPを指定します。ブラウザからアクセスする時等は以下のIPです。
    config.vm.network "private_network", ip: "192.168.56.10"

    # メモリ(数値は自分の環境と相談)
    config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", "1024"]
    end

    # Windows側のファイルを、仮想サーバに認識させる
    # 作業ディレクトリのファイルを、仮想サーバ内では`/vagrant`として扱うようにする
    config.vm.synced_folder ".", "/vagrant", mount_options:['dmode=777','fmode=777']

    # 初回起動時に実行
      config.vm.provision "shell", inline: <<-SHELL
        # タイムゾーン・言語設定の変更
        timedatectl set-timezone Asia/Tokyo
        localectl set-locale LANG=ja_JP.utf8

        # 更新
        yum -y update
        yum -y update kernel
      SHELL

    # 再起動(vagrant-reloadプラグイン必須)
    config.vm.provision "reload"

    # 初回に実行するコマンド(環境を構築する)
    config.vm.provision "shell", inline: <<-SHELL
        # apacheインストール
        yum -y install httpd

        # 邪魔なapache設定を削除
        rm /etc/httpd/conf.d/welcome.conf

        # PHPインストール
        yum -y install epel-release
        rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
        yum -y install --enablerepo=remi,remi-php74 php php-devel php-bcmath php-mbstring php-pdo php-gd php-xml php-mcrypt php-mysql

        # PHP設定調整
        sed -i -e "s/memory_limit = 128M/memory_limit = 1024M/g" /etc/php.ini
        sed -i -e "s/;date.timezone =/date.timezone = Asia\/Tokyo/g" /etc/php.ini
        sudo chmod 777 /var/lib/php/ssession/

        # MySQLインストール
        yum -y localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
        yum -y install mysql-community-server

        # MySQL認証方式の指定
        echo 'default-authentication-plugin = mysql_native_password' >> /etc/my.cnf

        # apache/MySQL自動起動設定
        systemctl enable httpd.service
        systemctl enable mysqld

        # ファイアウォール停止
        systemctl stop firewalld
        systemctl disable firewalld

        # composerインストール
        php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
        php composer-setup.php
        php -r "unlink('composer-setup.php');"
        mv composer.phar /usr/local/bin/composer

        # gitインストール
        yum -y install git

        # unzipインストール
        yum -y install unzip

        # Node.js(npm)インストール(yumで古いバージョンを取得→nで新しいものを取得→古い方を削除)
        yum -y install nodejs npm
        npm install -g n
        n stable
        yum -y remove nodejs npm
    SHELL

    # 再起動
    config.vm.provision "reload"
end

サーバ内作業

vagrant sshでサーバ内に入って、以下の作業を行えば開発環境の構築は完了です。

$ pwd
/home/vagrant

# [コメント] apache設定の調整
$ sudo sed -i -e "s/User apache/User vagrant/g" /etc/httpd/conf/httpd.conf
$ sudo sed -i -e "s/Group apache/Group vagrant/g" /etc/httpd/conf/httpd.conf
$ sudo sed -i -e "s/DirectoryIndex index.html/DirectoryIndex index.php index.html/g" /etc/httpd/conf/httpd.conf
$ sudo sed -i -e "s/\/var\/www\/html/\/home\/vagrant\/blog\/public/g" /etc/httpd/conf/httpd.conf
$ sudo sed -i -e "s/AllowOverride None/AllowOverride All/g" /etc/httpd/conf/httpd.conf

# [コメント] Laravelインストーラ取得
$ composer global require laravel/installer
$ echo "export PATH=~/.config/composer/vendor/bin:$PATH" >> ~/.bash_profile
$ source ~/.bash_profile

# [コメント] Laravelプロジェクト作成
$ laravel new blog

# [コメント] デフォルトパスワードを表示
$ sudo grep "A temporary password is generated" /var/log/mysqld.log | sed -s 's/.*root@localhost: //'

# [コメント] rootパスワードを設定、データベース作成
# [コメント] パスワードを求められるので、一手前で表示させたデフォルトパスワードを使用する
$ mysql -uroot -p --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEW_pass_123'; flush privileges;CREATE DATABASE blog;"

# [コメント] ENVにMySQLパスワードを設定
$ sed -i -e "s/DB_PASSWORD=/DB_PASSWORD=NEW_pass_123/g" /home/vagrant/blog/.env

# [コメント] apache再起動
$ sudo systemctl restart httpd

動作確認

http://192.168.56.10/にアクセスし、Laravelのwelcome画面が表示されたら無事、構築完了です。

Laravelでブログサイトを作る 04. 仮想環境の立ち上げ

主目的

Windows11環境で、VirtualBoxとvagrantを使えるようにする。

使用PC

acer Spin 5

事前にインストール済みのもの

Git for Windows
※コマンドを打つためにGit Bashが欲しくてインストールしています。

仮想化支援機構の有効化

BIOS起動

  1. スタートボタンを右クリックし、「設定」をクリックします
  2. 左メニューの「システム」を選択します
  3. 下の方にある「回復」をクリックします
  4. PCの起動をカスタマイズするの項目にある「今すぐ再起動」をクリックします
  5. 再起動するかを問われるので、「今すぐ再起動」をクリックします
    これで、再起動後にBIOSが立ち上がります。

設定変更

Advancedにある、「Intel VD○」をEnabledにします。
変更後、Escキーを押して設定を保存し、再起動します。

VirtualBox と Vagrant のインストール

VirtualBox

ダウンロードページ:https://www.virtualbox.org/wiki/Downloads
バージョンは、導入時の最新だった6.1.30です。
ダウンロードページから、Windows hosts(使用しているOS)を選択。
ダウンロード完了後、インストーラを起動し、案内に従えばインストール完了。

Vagrant

ダウンロードページ:https://www.vagrantup.com/
こちらは、導入時バージョン2.2.19です。
こちらも、ダウンロード後はインストーラからインストールします。

Vagrantを使って仮想環境立ち上げ

Boxの入手

Boxとは?

乱暴に言うと「OSのisoイメージ」みたいなもの。
CentOS7のBoxを使えば、CentOS7の仮想サーバを作れます。
当然ながら、環境構築したBoxを自分で作ることもできます。

Boxの追加

vagrant boxリストから使いたいOSのBoxを探し、取得します。
Searchにキーワードを入力すれば絞り込めるので、ProviderがVirtualBoxの、希望のバージョンのURLをこの後に使います。
一度追加すれば、以降は何度でも作れます。

コマンドはvagrant box add [URL] --name [名前(自由入力)]です。
例えば、CentOS7を使いたい場合は、Windows PowerShellを起動し、

vagrant box add CentOS7 https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box

作業ディレクトリの作成とVagrantfileの起動

今回は、C:\workspace\blogで作業します。適宜読み変えて頂くようお願いします。

個人的な好みから、Git Bash(Git for Windows導入時に)で作業しています。

# [コメント]作業ディレクトリを作成し、そこへ移動
$ mkdir -p /c/workspace/blog
$ cd /c/workspace/blog

# [コメント]Vagrantfileの作成(初期化)
$ vagrant init CentOS7

# [コメント]起動
$ vagrant up
ここでエラーが出ました

SSH auth method: private keyのところで、以下のメッセージを出力して止まってしまいました。

Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

メッセージは、「タイムアウトしちゃったから、原因を特定して対処してね。正常なはず、って時はタイムアウト時間の設定値を伸ばしてね」ということ。
これが発生するのは、Windowsの場合はHyper-Vが有効になっている可能性が考えられます。
コマンドプロンプトを管理者権限で立ち上げます。

# [コメント] 現在の設定状況を確認します。「Auto」が表示される場合、有効な状態です。
> bcdedit /enum | find "hypervisorlaunchtype"
hypervisorlaunchtype    Auto

# [コメント] Hyper-Vを無効化します。
bcdedit /set hypervisorlaunchtype off

# [コメント] Windowsを再起動します。
> shutdown /r /t 0

# [コメント] 再起動後、もう一度設定状況を確認すると、きちんと無効になっています。
> bcdedit /enum | find "hypervisorlaunchtype"
hypervisorlaunchtype    Off

仮想サーバの確認

vagrant sshコマンドを利用して、SSH接続します。正常に接続できることをもって、起動確認とします。
なお、パスワードは「vagrant」です。

$ vagrant ssh
vagrant@127
127.0.0.1's password: 
Last login: XXX XXX XX XX:XX:XX XXXX from XXX.XXX.XXX.XXX
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$

確認環境の破棄

今回立ち上げた環境は、あくまで「仮想サーバを作れるか」を確認するためのものですので、今後は不要になります。
なので、破棄しておきます。

# [コメント] `Ctrl + D`でログアウト
[vagrant@localhost ~]$ Abgemeldet.
Connection to 127.0.0.1 closed.

# [コメント] 停止
$ vagrant halt

# [コメント] 破棄
$ vagrant destroy

プラグインインストール

今後使用することになるvagrantのプラグインをインストールします。

  1. vagrant-reload
    • 環境を自動構築する際に再起動を可能とするためのプラグイン
  2. vagrant-vbguest
    • ホストとゲストの、GuestAdditionsのバージョンを自動で合わせてくれるプラグイン
$ vagrant plugin install vagrant-reload
$ vagrant plugin install vagrant-vbguest