6
9

More than 5 years have passed since last update.

WindowsにVagrantでCentOS7のセットアップ

Last updated at Posted at 2019-04-20

Vagrantのセットアップ

ここを参考に作業
Windows上にVirtualBox+VagrantでCentOS環境構築

VirtualBoxとVagrantのインストール

https://www.vagrantup.com/downloads.html
https://www.virtualbox.org/wiki/Downloads

VirtualBoxにCentOS boxファイルを登録する

http://www.vagrantbox.es/
※↑からインストールしたいOSを選択

コマンド

vagrant box add VM名 boxファイルURL
vagrant box add centos7-2 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box

※ダウンロードにすごく時間かかる

確認

vagrant box list

centos7-2と表示されればOK

VagrantFileの作成・編集

コマンド 「vagrant init box名」

vagrant init centos7-2

できたvagrantFileを開き、以下をコメントから有効にする

// Windowsから見ることの出来るIPを指定
config.vm.network "private_network", ip: "192.168.33.10"

// GUIモードの設定をONにする
config.vm.provider "virtualbox" do |vb|

  # Display the VirtualBox GUI when booting the machine

  vb.gui = true

end

(必要に応じて)ホストOSとファイルを共有する

vagrantFileを編集する

# argument is a set of non-required options.
config.vm.synced_folder "./share", "/var/share"

./shareがホストOS側、/var/shareがゲストOS側

(必要に応じて)HDDの容量を拡張する

初期設定だと4GBしかない

参考:Vagratfileに一行書くだけでVMのディスク容量を増やす方法

上記手順だけだとまだハード的に接続されただけで、実際のボリュームの拡張作業が必要
以下

(必要に応じて)同じLANネットワーク内から参照できるようにする

vagrantfileを編集

以下のコメントを外し、有効化。IPを固定にしたい場合は

# dhcpでIPとるなら
config.vm.network "public_network""
# 固定IPにしたいなら
config.vm.network "public_network", ip: "192.168.11.28"

VM立ち上げ

vagrantfileを作りたいディレクトリで以下コマンド(この時はD:\vagrant\centos7-2)

vagrant up

起動したらlogin/passwordともにvagrantと入力すればログイン出来る
sshクライアントから入りたい場合はvagrant@192.168.33.10

OSパッケージの最新化

Windows Update的なもの、必ずやること
(FireWalldの有効化とかに必要)

sudo yum -y update

firewallの有効化

起動している確認(ここまでの手順だと多分してない)

[vagrant@localhost opt]$ systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead)

起動する

sudo systemctl start firewalld

httpdとsshを許可(sshはデフォルトで入ってると思う)

[root@localhost ~]# firewall-cmd --add-service=http --permanent #httpを許可
success
[root@localhost ~]# firewall-cmd --add-service=ssh --permanent #sshを許可
success
[root@localhost ~]# firewall-cmd --reload #再読み込み

時刻合わせ

タイムゾーンをJSTにする

[vagrant@localhost ~]$ date
Wed 11 Apr 01:10:07 BST 2018

BSTになってるのでJSTにする

[vagrant@localhost ~]$ sudo timedatectl set-timezone Asia/Tokyo
[vagrant@localhost ~]$ date
Wed 11 Apr 09:10:56 JST 2018

ntpを使って他サーバーに問い合わせて時刻を自動で同期するようにする

 sudo yum -y install ntp

nict(日本の標準時サーバー的なもの)から正確な時刻を取得

[vagrant@localhost ~]$ sudo ntpdate ntp.nict.jp
11 Apr 01:05:14 ntpdate[7161]: step time server 133.243.238.244 offset 2.686926 sec

ntpdデーモンを起動して、自動的に時刻同期させる

sudo vi /etc/ntpd.conf
server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org

↑を↓に修正

server -4 ntp.nict.jp
server -4 ntp1.jst.mfeed.ad.jp
server -4 ntp2.jst.mfeed.ad.jp
server -4 ntp3.jst.mfeed.ad.jp

サービス起動

[vagrant@localhost ~]$ sudo systemctl start ntpd

起動していれば以下でステータスを確認できる

[vagrant@localhost ~]$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ntp-b3.nict.go. .NICT.           1 u   29   64  177    7.396  191.571 125.662
+ntp1.jst.mfeed. 133.243.236.17   2 u   15   64  177    5.654  198.674 129.149
+ntp2.jst.mfeed. 133.243.236.17   2 u   29   64  177    5.188  192.696 125.004
+ntp3.jst.mfeed. 133.243.236.17   2 u   27   64  177    5.423  191.855 124.789

自動起動設定

[vagrant@localhost ~]$ sudo systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

HaProxyインストール(いらんかも)

sudo yum -y install haproxy

HaProxy サービス自動起動設定(いらんかも)

sudo chkconfig haproxy on

サービス自動起動設定の確認

systemctl list-unit-files --type=service | grep haproxy

実行結果

[vagrant@localhost ~]$ systemctl list-unit-files --type=service | grep haproxy
haproxy.service                             enabled

Mysqlをインストールする

こちらがとてもわかりやすかったです。

CentOS 7 に MySQL 5.7 を yum インストールして初期設定までやってみた
https://enomotodev.hatenablog.com/entry/2016/09/01/225200

開発用にパスワードなしでログインできるようにするには/etc/my.cnfに設定を加えます

my.cnf
[mysqld]
skip-grant-tables

再起動

sudo systemctl restart mysqld.service

VM停止

vagrant halt ※変更した内容が保持して終了

vagrant destroy ※変更した内容を破棄して終了

6
9
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
6
9