LoginSignup
98
111

More than 5 years have passed since last update.

VagrantでCentOS7を立てたが、httpアクセスが繋がらない時にやったこと

Posted at

自分のMac上にVagrantでCentOS7を構築した時にはまったこと。

vagrant環境構築

以下ページを参考にさせていただきました。
詳細は割愛します。

Ansible使ってVagrantのゲストOSの構成管理する【Playbook: jenkins, node.js】

Webサーバ起動

node.jsのプロジェクトを構築したかったので、諸々をインストールし、Webサーバを立ち上げました。

app.js
app = require('express')();
app.get('/', function(req, res){
        res.send('hello world');
    });
app.listen(80);

Macのブラウザからアクセスしたところ、
ページが見つかりません。とのエラーが表示されました。

Vagrantfile

Vagrantfile上の設定は特に問題なさそうでした。

config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "private_network", ip: "192.168.33.10", id:"http"

プライベートIPとポートの設定も問題ないので、
localhost:8080192.168.33.10で繋がるはず。

telnetアクセス&IPアドレスの確認

telnetで80ポートにアクセスしてみたものの、はじかれました。
VM上からcurlで確認したところ、問題なくhello worldが返ってきたので、
VMサーバのネットワークの設定関連を確認しました。

VM上のIPアドレスを確認したところ、問題なくVagrantfileに記載されているIPが振られていました。

SELinux設定変更

訳もわからず、ググっていたところ、SELinuxを無効にするといいんじゃないかみたいな記事があったので、とりあえず確認してみました。

getenforceをたたいたところ、Enforcingと表示されたため、有効になっていました。
configファイルを編集し、無効に設定。その後サーバを再起動させました。

そもそもSELinuxってなんなのかというと、ウィキペディアを参考にしてください。
SELinux

で、再度確認したところ、やっぱりだめでした。
SELinuxは関係ない可能性が高いです。

firewalld設定変更

再度途方にくれて、ググったところ、iptablesが怪しいのではないかという記事を見つけたので、確認してみました。

iptablesを無効にすべく、/etc/init.d配下を確認したところ、起動スクリプトがありませんでした。

そんなことはありえない、とさらにググったところ、CentOS7からは、iptablesではなく、firewalldという新しいものができたらしいことを知りました。

firewalldとは、今までのiptablesと同じ役割を果たす、外部からのアクセスを制御するもので、内部的には、iptablesを使って制御している?らしいです。

基本的にiptablesを無効にしていたたちなので、とりあえず、無効にしました。

systemctl stop firewalld

参考:
CentOS 7からデフォルトのFirewalldでハマった(iptablesは今後使わない?)

で、再度確認したところ、無事hello worldが表示されました。

CentOS7から変わっている部分が多々あるそうなので、勉強しないとまたハマりそうです。

後、SELinuxとかfirewalld(iptables)とか久しくやっていないので、設定方法とか完全に忘れていました。

今度からはちょくちょく触るように心がけたいと思います。

Tips

centOS7上でifconfig

CentOS7を最小構成で構築するとネットワーク系のコマンドが最初から入っていないらしいです。

そんな時は、yumでnet-toolsを入れてやればいいらしいです。

RHEL7/CentOS7でipコマンドをマスター

Failed to mount folders in Linux guest

vagrant upしたときにこんなエラーが出た時は、

Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:
mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant
The error output from the last command was:
/sbin/mount.vboxsf: mounting failed with the error: No such device

vagrantでmountエラーの解決方法

このページを参考にして、

vagrant ssh

[vagrant@localhost ~]$ sudo /etc/init.d/vboxadd setup
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [  OK  ]
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]

[vagrant@localhost ~]$ exit

vagrant halt
vagrant up

ログインして、VirtualBox Guest Additionsなどなどを再構築して、サーバを落として、再度あげると解決します。
ちなみにこのコマンドで何をやっているかというと、VirtualBox Guest Additionsという、Vagrant上のOSとホストマシンとのやりとりをスムーズにするための機能の設定をしているみたいです。

参考:
vagrant upでsynced_folderが失敗した時の対処
Guest Additionsのインストール

98
111
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
98
111