0
0

More than 1 year has passed since last update.

VagrantによるLAMP環境構築 1【PHP 5.4】

Last updated at Posted at 2022-03-14

はじめに

VagrantとVirtualBoxを使用して
LAMP(CentOS7/Apache/MariaDB/PHP)環境を構築していきます
(※開発環境はWindows10を想定しています)

開発環境

  • Windows10
  • VirtualBox
  • Vagrant
  • LAMP(CentOS7/Apache/MariaDB/PHP)

基本用語

VMとは

  • 物理的なハードウェア上に構築された仮想的なハードウェア

VirtualBoxとは

  • VMを構築するためのソフトウェア(仮想化ソフトウェア)の一つ

Vagrantとは

  • 仮想環境構築ツール

  • 仮想化ソフトウェアのみ使用してVMを構築する場合、GUI画面を操作して構築するVMの設定を行う必要があるが、Vagrantを用いた場合、Vagrantfileを編集することで、VM起動時に自動的に行われるVMの設定を定義することが出来る

  • また使用する仮想化ソフトウェアに違いがあっても、共通のVagrantfileを用いることで同一のVMを構築することが可能となる

LAMPとは

webアプリケーションを開発するためのオープンソースソフトウェアの組み合わせの一つ

必要なものをインストールする

VagrantBoxをインストールする

Vagrantを用いてVMを構築するにはVagrantBoxというOSのイメージファイルをインストールする必要がある
インストールはcmd(コマンドプロンプト)から行う
vagrant box add centos/7でインストールを開始する
プロバイダーを聞かれるのでvirtualbox の3を選択する
vagrant box listでVagrantBoxがインストールされたことを確認する

cmd
C:\> vagrant --version
Vagrant 2.2.19

C:\> vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
    box: URL: https://vagrantcloud.com/centos/7
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) libvirt
3) virtualbox
4) vmware_desktop

Enter your choice: 3
==> box: Adding box 'centos/7' (v2004.01) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box
Download redirected to host: cloud.centos.org
    box:
    box: Calculating and comparing box checksum...
==> box: Successfully added box 'centos/7' (v2004.01) for 'virtualbox'!

C:\> vagrant box list
centos/7 (virtualbox, 2004.01)

Vagrantfileを編集する

C:ドライブ直下に空フォルダを作成して名前を"test_VM"にする
メモ帳を開き、以下のように編集する
ファイル名を"Vagrantfile"にして"test_VM"に保存する

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.network :private_network, ip: "192.168.33.10"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
  end
  config.ssh.insert_key = false
end
1行目
Vagrant.configure("2") do |config|

Vagrantのバージョン指定
最新バージョンは2

2行目
config.vm.box = "centos/7"

VagrantBoxの指定
先ほどインストールしたcentos/7を使う

3行目
config.vm.network :private_network, ip: "192.168.33.10"

VMのIPアドレスを明示的に振っている

4行目
config.vm.network "forwarded_port", guest: 80, host: 8080

Webサーバ用のポートフォワード設定
これをしないとVM上のWebサーバにアクセス出来ない

5行目
config.vm.provider "virtualbox" do |vb|

仮想化ソフトウェアの指定
今回はVirtualBox

6行目
vb.memory = "2048"

VMのメモリを2GBにする

8行目
config.ssh.insert_key = false

VMの初回構築時に生成される秘密鍵がデフォルトの安全ではない秘密鍵で
ある場合、VM上に異なる秘密鍵が挿入されるのを防ぐ
これをしないとホスト側に生成されるVMの秘密鍵がVM上に生成された秘密鍵と一致せず
VMにSSHログインできなくなる

※vagrant initコマンドとは?

vagrant initはコマンドを実行したフォルダ上に
Vagrantfileのテンプレートファイルが生成されます
vagrant initが実行されたフォルダ自体は特に変化しないようです???
今回はvagrant initしないでVMを構築したいと思います

VMを起動する

先ほど作成した"test_VM"フォルダ上にVMを構築する
cmdを立ち上げ、cdコマンドで"test_VM"に移動する
vagrant upでVMを起動する

cmd
C:\test_VM> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
・・・
・・・
・・・
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /cygdrive/c/test_VM/ => /vagrant

VMの基本操作

コマンド 説明
vagrant up VMを起動する
vagrant halt VMをシャットダウンする
vagrant reload VMを再起動する
vagrant destroy VMを削除する
vagrant status VMの状態を確認する

VMにSSH接続する

vagrant statusでVMの状態がrunningであることを確認する
※runningでない場合はVMをもう一度起動する

cmd
C:\test_VM> vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

vagrant ssh-configでVMの秘密鍵のパスを調べる
IdentityFile がVMの秘密鍵のパスなので
C:/Users/【user_name】/.vagrant.d/insecure_private_keyの部分をコピーする

cmd
C:\test_VM> vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/Users/【user_name】/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

vagrant portでVMのポートを確認する
192.168.33.10:22
127:0:0:1:2222
のどちらかでVMにSSH接続出来ることが分かる

cmd
C:\test_VM> vagrant port
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.

    22 (guest) => 2222 (host)
    80 (guest) => 8080 (host)

今回は127:0:0:1:2222のSSH通信を行う
Tera Termを起動してipアドレスとport番号を入れる
tera1.png
ユーザー名:vagrant
パスワード:vagrant
秘密鍵に先ほどコピーしたVMの秘密鍵のパスを張り付ける
tera2.png

VMのOSを確認する

CentOS7のVMを構築することが出来た

127.0.0.1-vagrant@localhost:~ VT
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)

続くVagrantによるLAMP環境構築 2【PHP 5.4】

0
0
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
0
0