ApacheとSELinuxでいろいろ詰まった話
背景
Apache + PHP環境を検証環境からステージング環境に移行したら、ページが表示できなくなった。
現象
- index.phpにアクセスしようとすると505が帰ってくる。
- error_logを確認すると以下のエラーが
PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "[ホニャララ]" could not be opened: failed to open stream: Permission denied'
対処
PHPモジュール周りを確認した
検証環境に入っていたPHPモジュールを一通りインストールしてみたがダメだった。
パーミッション周りを確認した
確認したが問題なさそう。念のため、パーミッションを777に変更したり所有者をapacheにしてみたりしたがだめ。
SELinux周りを確認した
一旦SELinuxをPermissiveにしてみると、正常に表示されたのでこれが原因と判明。
ずっとPermissiveにしておくのもまずいので、SELinuxのことをいろいろ調べた。
audit.logを確認したところ、PHPからログを書き込もうとしたところで蹴られていることがわかった。
CGIでファイルを変更できるようにするにはsystem_u:object_r:httpd_sys_script_exec_t:s0
というラベルが必要らしい。
$ sudo chcon -R system_u:object_r:httpd_sys_script_exec_t:s0 [対象ディレクトリ]
SELinuxをEnforcingに戻しても正常に表示できることを確認した。
まとめ
数年前に同じ原因でつまった覚えがかすかにあったので解決することが出来た。検証環境について全く知らなかったのも原因を特定するのに時間がかかってしまった原因かもしれない。
どうしてもCentOS6でTensorflow x Pythonを使いたいとき
背景
現行サービスとかの関係でどうしてもCentOS6を手放せないけど、同じマシンでTensorflow (keras)をPythonから動かしたかった。
解決策
Dockerを使う
注意点
RHEL6はDockerのサポート外なのでトラブっても放置されます。また、Docker 1.8以降は利用できないため、1.7以前のバージョンを動かすことになります。
インストール
$ yum install https://get.docker.com/rpm/1.7.1/centos-6/RPMS/x86_64/docker-engine-1.7.1-1.el6.x86_64.rpm
以上。
使ってみる
Dockerfile
FROM ubuntu:16.04 RUN apt update && apt install python3 python3-pip && pip3 install --upgrade pip && pip3 install tensorflow keras sklearn h5py CMD /bin/bash
大体こんな感じです。用途に合わせてpipで入れるモジュールは編集するといいです。
イメージ作成
docker build -t 'tf/ubuntu:16.04' .
コンテナ作成
docker run --name tf -i -t tf/ubuntu:16.04
以上になります。
Dockerの使い方についてはかなりの量ネットに上がっているので割愛しました。
まとめ
ありがとう仮想化時代
CentOS6上でVagrantを動かそうとしてコケた話 (未解決)
はじめに
CentOSでTensorflowをやるなら、よっぽどの理由がない限りはCentOS7を用意すべき。
背景
よっぽどの理由があってCentOS6上でTensorflow-Kerasのプログラムが動かなかった。
pyenv-virtualenvで雑に環境構築をして実行しようとしたが以下のようなエラーが出た。
ImportError: /lib64/libc.so.6: version `GLIBC_2.14' not found
glibc2.14以上がいるらしいがよくよく調べたところCentOS6に入れるのはなかなか厳しいらしいようだった。 面倒なのでVagrantでUbuntu16.04を入れて動かそうということになった。
本題
VirtualBoxやVagrantをインストールしていく。
下記サイトを参考にさせていただいた。 qiita.com
DKMSのインストール
EPELのリポジトリは既に追加してあったのでyumを叩くだけだった。
$ yum -y install dkms
VirtualBoxのインストール
VirtualBoxのリポジトリを追加する。
$ cd /etc/yum.repos.d/ $ sudo wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo
$ yum -y install VirtualBox-5.1
ユーザーをvboxusersグループに追加する。
$ usermod -a -G vboxusers [username]
Vagrantのインストール
https://www.vagrantup.com/downloads.html
上記のページからURLをコピーしてきてrpmを追加する。
$ sudo rpm -Uvh https://releases.hashicorp.com/vagrant/1.9.8/vagrant_1.9.8_x86_64.rpm?_ga=2.130537803.573443576.1503902023-1829921896.1503902023
インストール終わり。
以下のコマンドでVagrantのバージョンを確認できる。
$ vagrant -v
VMを作成
必ず各ゲストごとのディレクトリを作成して実行すること。
$ mkdir xenial64 $ cd xenial64 $ vagrant init ubuntu/xenial64 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
とりあえず早くたてたかったのでVagrantfileにはノータッチで起動した
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'ubuntu/xenial64' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'ubuntu/xenial64' default: URL: https://vagrantcloud.com/ubuntu/xenial64 ==> default: Adding box 'ubuntu/xenial64' (v20170822.0.0) for provider: virtualbox default: Downloading: https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20170822.0.0/providers/virtualbox.box ==> default: Successfully added box 'ubuntu/xenial64' (v20170822.0.0) for 'virtualbox'! ==> default: Importing base box 'ubuntu/xenial64'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'ubuntu/xenial64' is up to date... ==> default: Setting the name of the VM: ubuntu_default_1503914989908_8759 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Running 'pre-boot' VM customizations... ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: ubuntu default: SSH auth method: password The guest machine entered an invalid state while waiting for it to boot. Valid states are 'starting, running'. The machine is in the 'gurumeditation' state. Please verify everything is configured properly and try again. If the provider you're using has a GUI that comes with it, it is often helpful to open that and watch the machine, since the GUI often has more helpful error messages than Vagrant can retrieve. For example, if you're using VirtualBox, run `vagrant up` while the VirtualBox GUI is open. The primary issue for this error is that the provider you're using is not properly configured. This is very rarely a Vagrant issue.
起動しない、、、
$ vagrant status Current machine states: default gurumeditation (virtualbox) The VM is in the "guru meditation" state. This is a rare case which means that an internal error in VitualBox caused the VM to fail. This is always the sign of a bug in VirtualBox. You can try to bring your VM back online with a `vagrant up`.
statusが"gurumediation"となっており、起動しなかった。
その後
いろいろ調べていろいろやったのでやったことを箇条書きする
- VirtualBoxのバージョン依存という記事がいくらかあったので4.3とか5.0とかをインストールし直して試した
- 同じエラーで失敗 (gurumediationとはなんなのか)
- CentOS7のゲストを作成しようとしてみた
- 同じエラーで失敗
- VirtualBoxのせいだと思ってvagrant-libvirtをインストール、KVMベースでVMを作成しようとした
- qemuのイメージのフォーマット(?)がCentOS6では対応していないらしく失敗
八方塞がりっぽかったので諦めた。
まとめ
CentOS7を用意しよう。