Vagrantで共有フォルダがマウントできなくなった

ホストがLinux、ゲストがLinuxなVirtualBoxでの話です。

vagrant upすると、以下のように共有フォルダがマウントできなくてエラーになりました。

==> default: Mounting shared folders...
    default: /vagrant => .../vagrant-centos7-php
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

ターミナルの出力を遡ると、Guest Additions moduleのビルドに失敗してました。

Copy iso file /usr/share/virtualbox/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
mount: /dev/loop0 is write-protected, mounting read-only
Installing Virtualbox Guest Additions 4.3.28 - guest version is 5.0.6
Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.28 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.28 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module[FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong)
Doing non-kernel setup of the Guest Additions[  OK  ]
Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.
An error occurred during installation of VirtualBox Guest Additions 4.3.28. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.

仮想マシンでログを見ると、インクルードファイルが見つからないというエラー。

[vagrant@localhost ~]$ cat /var/log/vboxadd-install.log
/tmp/vbox.0/Makefile.include.header:115: *** Error: unable to find the include directory for your current Linux kernel. Specify KERN_INCL=<directory> and run Make again.  Stop.
Creating user for the Guest Additions.
Creating udev rule for the Guest Additions kernel module.

VirtualBoxが古すぎるのがまずいのか?とアップデートしてみましたが、変わりませんでした。

結局、kernelがアップデートされたが、ヘッダファイルが古いままで見つけられなかったということかと思いました。

[vagrant@localhost ~]$ sudo yum -y update kernel
[vagrant@localhost ~]$ sudo yum -y install kernel-devel kernel-headers dkms gcc gcc-c++

そして、仮想マシンを停止してvagrant up。これで後はvbguestプラグインが面倒を見てくれて、正常に動作できるようになりました。

vbguestプラグインがインストールされてない場合は、

$ vagrant plugin install vagrant-vbguest

参考

関連

Tags: linux, vagrant, virtualbox

Mac OS XにPHP 7をインストールする

Mac OS X(El Capitan)にphp7をインストールします。

$ curl -s http://php-osx.liip.ch/install.sh | bash -s 7.0

確認します。

$ /usr/local/php5/bin/php -v
PHP 7.0.4 (cli) (built: Mar  4 2016 18:16:03) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.4.0RC3, Copyright (c) 2002-2015, by Derick Rethans

~/.bash_profilePATH/usr/local/php5/binを追加します。

例えば、以下を追加します。

export PATH="$HOME/bin:$HOME/.composer/vendor/bin:/usr/local/php5/bin:$PATH"

Mac OS XでのPHP開発環境の構築」へ続く。

参考

Tags: php, mac, php7

CentOS 6で秘密情報をrootだけが読めるファイルに設定する

Webアプリで秘密情報などを環境変数から取得するようにすることがあります。

Apacheの設定ファイルで環境変数を定義すると、CLIからPHPスクリプトを実行した場合に再度別に環境変数を定義しないといけません。1か所で定義する方法を考えました。

まず、/etc/sysconfig/httpdに環境変数を追加します。

/etc/sysconfig/httpd

export ENV=production

このファイルはrootだけが読めるようにパーミッションを設定しておきます。

$ sudo chmod go-rwx /etc/sysconfig/httpd

次に、Apacheの設定でPHPに渡したい環境変数をPassEnvに指定します。

PassEnv ENV

これで、Web経由で実行されるPHPスクリプトでは$_SERVER['ENV']に値が渡されます。

CLIから実行する場合は、以下のようなshell script経由で実行します。

php.sh

#!/bin/sh

. /etc/sysconfig/httpd

su -s /bin/bash apache -c "php $*"

これで以下のように実行できます。

$ sudo ./php.sh index.php

Tags: apache, php, cron, centos