2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vangrantの共有フォルダで静的ファイルが更新されない問題の 解決と原因の考察

2
Posted at

概要

Vagrant&Virtualboxで用意した仮想環境をWebサーバとして使う。
そして、フォルダの共有機能を使ってホスト側で編集しながら動作確認する・・・。ような使い方をしている方も多いとおもいます。

だが、htmlやcss,jsファイルを更新してもその内容が反映されない。だがPHPファイルは更新されるという謎現象が発生。

httpdを再起動させたりしてもダメ。
httpd.confのEnableSendfile Offを有効にしてもダメ。

Vagrantfileの設定変更で解決した。

参考

Vagrantのup時、httpdが自動起動しないとき
http://qiita.com/ooba1192/items/96b7ab25d2bda1676aaa

上記リンク先のVagrantfileの変更だけで解決した。

環境

ホスト側(Webエンジニアの環境にありがち)

  • mac os X (El Capitan)
  • Vagrant (1.8.6)
  • VirtualBox (5.0.14)

ゲスト側(この件に関しては関係少なさそうだけど これも定番か)

  • Cent OS 6.7 (公式で公開されてるbento)
  • Apache httpd (2.2系)
  • PHP (5.6系)

設定例

下記抜粋のように、追記。
私の環境では、その後再起動で、無事更新されるようになりました。

Vagrantfileから抜粋
  # 共有設定

   config.vm.synced_folder "/Users/rojiuratech/Documents/workspace" , "/var/www/html" , owner: "apache", group: "apache"

# ここから追記
    config.vm.provision :shell, run: "always", :inline => <<-EOT

       sudo service httpd restart

    EOT
# ここまで追記
end

原因の考察

先に取り上げた記事 並びにそこからリンクされていた
下記ページを読んでみると・・・。

Vagrant上のCentOSでhttpd.confをVagrant共有ディレクトリのシンボリックリンクにしているとOSブート時にhttpdが自動起動しない #vagrant #apache

これは、/etc/init.dの起動スクリプトが実行されるタイミングで/vagrantディレクトリがマウントされていないことが原因です。

ゲストOS側の起動スクリプトが作動する時点で、マウントがなされていないことが原因・・・のようです。

だったら、マウント状態をチェックしよう。
いやいや、Vagrantfileに プロビジョニング時に強制的に(共有フォルダを扱う)httpdを再起動させよう! というのが最初に取り上げたリンク先の考え方。

再起動時のログを確認

下記ログを読んでみると

  1. ポート設定などを行った上で、ゲスト側OSが起動
  2. 共有ディレクトリをマウントする
  3. インライン記述のスクリプトで、 httpdを再起動

といった流れになってます。

やっぱり、マウントされる前にhttpdが起動されるから・・・
認識されていないんだ。

プロビジョニング(再起動)時のログ
$ vagrant reload

==> default: Attempting graceful shutdown of VM...

==> default: Checking if box 'bento/centos-6.7' is up to date...

==> default: Clearing any previously set forwarded ports...

(中略)

==> default: Machine booted and ready!

(中略)

==> default: Mounting shared folders...

    default: /vagrant => /Users/rojiuratech/Documents/vagrant_folder

    default: /var/www/html => /Users/rojiuratech/Documents/workspace

==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`

==> default: flag to force provisioning. Provisioners marked to run always will still run.

==> default: Running provisioner: shell...

    default: Running: inline script

==> default: Stopping httpd: 

==> default: [  OK  ]

==> default: Starting httpd: 

==> default: [

==> default:   OK  

==> default: ]

理屈はわかったけど、疑問が・・・

起動した後とはいえ、httpdを手動で再起動ではダメなのですか?!

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?