CentOS 5.6でPHPでexec(),shell_exec()関数が実行できない現象について紹介します。
CentOS 5.6でPHPでexec()
,shell_exec()
関数が実行できないことがあります。exec()関数を呼び出しても何も結果が戻らず/var/log/httpd/error_log
に
が記録されます。
sudo コマンドをexec()関数から実行した場合は
や
が記録されます。
原因はいくつかありますが、SELinuxが有効になっていることでこの現象が発生することがあります。SELinuxの設定を無効にすることでexec(),shell_exec()関数を実行できるようになる場合があります。SELinuxの無効化の方法はこちらの記事を参照してください。
以下のPHPファイルを実行します。
<?php
$results = shell_exec('/bin/ls /usr -lsa');
echo "<pre>$results</pre>";
?>
正常に実行されると /usr のファイル一覧が表示されます。
total 260
8 drwxr-xr-x 15 root root 4096 May 20 00:18 .
8 drwxr-xr-x 24 root root 4096 May 23 01:25 ..
8 drwxr-xr-x 3 root root 4096 Apr 28 02:59 X11R6
40 drwxr-xr-x 2 root root 36864 May 20 04:02 bin
8 drwxr-xr-x 2 root root 4096 May 11 20:58 etc
8 drwxr-xr-x 2 root root 4096 May 11 20:58 games
16 drwxr-xr-x 106 root root 12288 May 11 20:58 include
8 drwxr-xr-x 6 root root 4096 Mar 31 04:25 kerberos
40 drwxr-xr-x 57 root root 36864 May 11 20:58 lib
60 drwxr-xr-x 91 root root 53248 May 20 04:02 lib64
8 drwxr-xr-x 13 root root 4096 May 11 20:58 libexec
8 drwxr-xr-x 12 root root 4096 May 11 20:58 local
20 drwxr-xr-x 2 root root 16384 May 20 04:02 sbin
8 drwxr-xr-x 201 root root 4096 May 11 20:58 share
8 drwxr-xr-x 5 root root 4096 May 11 20:58 src
4 lrwxrwxrwx 1 root root 10 May 20 00:18 tmp -> ../var/tmp