LoginSignup
17
15

More than 5 years have passed since last update.

【俺メモ】VagrantでNode.jsサーバ(CentOS7)をWindows上に10分でチョチョイのチョイっと構築

Posted at

ほんま、便利な世の中になったで。
10分ってのはだいたいの感覚やから細ぇことは気にせんといてや。
(※ダウンロード時間は省くで)

1.VirtualBoxインストール

とりあえず、公式ページからダウンロードしてインストールしてや。
VMWareでもええにゃけど、Vagrant自体VirtualBox用やさかいな。

2.Vagrantインストール

次はこっちや。公式ページからダウンロードしてさくっとインストールしてや。

3.サーバ起動

Box取得

ここのページの検索ボックスに「CentOS 7」て入れてから、VirtualBox用のBoxのアドレスをコピーしてや。
次にコマンドプロンプト起動してや。

cmd.exe
> mkdir hogehoge
> cd hogehoge
> vagrant box add {box名} {boxのurl}
> vagrant init {box名}

Vagrantfile編集(ネットワーク設定)

initした後に出来たVagrantfileをエディタで開いて編集や。
コメントになってるさかい、#消して外してや。
あとIPアドレスも適当に設定してな。
(idは必要な場合だけでえぇで。)

Vagrantfile
  config.vm.network "private_network", ip: "192.168.xxx.xxx", id:"{hoge_network}"

サーバ起動

んで、サーバ起動やな。ついでにログインもするで。

cmd.exe
> vagrant up
> vagrant ssh

共有ディレクトリのマウントに失敗する場合

/vagrantディレクトリの共有に失敗する場合は、ログインした後にこのコマンド叩くんやで。

bash
$ sudo /etc/init.d/vboxadd setup

4.Node.js環境構築や

nodebrewインストール

vagrantからサーバにログインした状態からスタートな。

bash
$ curl -L git.io/nodebrew | perl - setup
$ echo "export PATH=$HOME/.nodebrew/current/bin:$PATH" >> ~/.bash_profile
$ source ~/.bash_profile

Node.jsインストール

とりあえず、最新安定版を入れるで。

bash
$ nodebrew install-binary stable
$ nodebrew use stable

その他インストール

npmやらで環境構築してや。

5.まとめ(プロビジョニング)

いちいち起動してからやらんでもvagrantにはprovisioningがあるやん。
というわけで、Vagrantfileを修正するで。
プロビジョニングはrootユーザーで実行されるからprivileged: falseオプションを付けて、vagrantユーザーで実行や。
(※もちろんroot権限がいるならsudoつけとけよ)
ここはテストに出るさかい要チェックや。

Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "{box名}"
  config.vm.network "private_network", ip: "192.168.xxx.xxx", id:"{hoge_network}"
  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    sudo /etc/init.d/vboxadd setup
    curl -L git.io/nodebrew | perl - setup
    echo "export PATH=$HOME/.nodebrew/current/bin:$PATH" >> $HOME/.bash_profile
    source $HOME/.bash_profile
    nodebrew install-binary stable
    nodebrew use stable
  SHELL
end

これで、vagrant upするだけやから、まぁ、コマンド叩いて待つだけになってもうたな。

あー、あと、Vagrantのバージョンは1.3.0以降な。
詳しくはここを参考にしてや。(※動作確認Vagrant 1.8.2)

17
15
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
17
15