オブジェクト指向とかデザインパターンとか開発プロセスとかツールとか

satoshi's ソフトウェア開発

js






当サイトはアフィリエイト広告を利用してます。

Tools

VirtualBox+Vagrantでお手軽な開発環境を構築

投稿日:


概要

Webアプリを構築するとき、Webアプリ毎にいろいろと環境が異なるので、それに合わせたサーバーを用意するのは手間がかかる。

それを解決するのがVirtualBox+Vagrant。

ViatualBoxはOracleが提供する仮想環境。ホストOS上で別のOS(ゲストOS)を実行できる。

一般的にサーバーで用いるOSとしては、Linux系のCentOSが使われることが多い。

手元のMacOSXにVirtualBox+Vagrantを構築してみたので、その手順をメモしとく。

VirtualBoxのインストール

こちら→Downloads - Oracle VM Virtulbox からダウンロードしてインストールする。

それだけ。

Vagrantのインストール

Macの場合はbrewを使うのが簡単かも。

$ brew cask install vagrant

ゲストOSの追加

VirtualBoxとVagrantのインストールが終わったら、仮想環境を使える準備ができた。

次はゲストOSを追加する。

ゲストOSはBoxファイルという形式で管理されている。

すでに多くの人達がBoxファイルを用意してくれているので、それを使うのが簡単。元気があるなら自分で作ることも可能だけど。

https://app.vagrantup.com/boxes/search にさまざまなBoxが公開されている。

今回はサーバー上でLaravelを使ったWebアプリを作りたいので、「laravel/homestead」を使う。

$ vagrant box add laravel/homestead
==> box: Loading metadata for box 'laravel/homestead'
box: URL: https://vagrantcloud.com/laravel/homestead
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) parallels
3) virtualbox
4) vmware_desktop

Enter your choice: 3
==> box: Adding box 'laravel/homestead' (v9.5.1) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/9.5.1/providers/virtualbox.box
Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
box: Calculating and comparing box checksum...
==> box: Successfully added box 'laravel/homestead' (v9.5.1) for 'virtualbox'!

Boxイメージのダウンロードに少々時間がかかるけど、コマンド1行でゲストOSのインストールが完了してしまう。
便利!

インストールされたことをコマンドで確認してみる。

$ vagrant box list
laravel/homestead (virtualbox, 9.5.1)

 

Boxが追加されたけど、最初に初期化する必要がある。初期化するとVagrantfileというファイルが作られる。

ネットワークの設定などが書かれているので、フォルダを分けて管理するのがよさそうなので、先にフォルダを作成して、その中で初期化する。

$ mkdir laravel
$ cd laravel
$ vagrant init laravel/homestead
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Vagrantfileが作られたので、これを少し編集する。

$ vi Vagrantfile

ハイライトしている行のコメントアウトを外す。

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

ゲストOSの起動

ゲストOSを起動するのもコマンド一発!

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'laravel/homestead' version '9.5.1' is up to date...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 (guest) => 8080 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 6.0.0 r127566
    default: VirtualBox Version: 6.1
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => (作業ディレクトリ)/laravel

 

sshでログインしてみる。

$ vagrant ssh
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-96-generic x86_64)

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

* Homestead v10.8.0
* Settler v9.5.1 (Virtualbox, Parallels, Hyper-V, VMware)

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


vagrant@vagrant:~$







-Tools
-,

Copyright© satoshi's ソフトウェア開発 , 2024 All Rights Reserved Powered by STINGER.