phpunit安裝出錯的緣由及解決辦法

官方指引

很遺憾, phpunit尚未在ArchLinux的倉庫裏。php

因此使用下載安裝的方式。按照官方的指引html

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version

結果獲得下面的錯誤:安全

PHP Warning:  realpath(): open_basedir restriction in effect. File(/usr/local/bin/phpunit) is not within the allowed path(s): (/srv/http/:/home/:/tmp/:/usr/share/pear/) in /usr/local/bin/phpunit on line 3
PHP Fatal error:  Class 'Phar' not found in /usr/local/bin/phpunit on line 714

啓用phar擴展

先解決Fatal error: Class 'Phar' not foundless

ls /usr/lib/php/modules

發現有 phar.so,說明Phar的擴展已經安裝,那麼是否是該擴展沒有Enable呢?
打開 /etc/php/php.ini搜索 phar,果真發現 extension=phar.so被註釋掉了。去掉該行前面的 ;,保存php.ini,再次運行 phpunit --versionide

PHP Warning:  realpath(): open_basedir restriction in effect. File(/usr/local/bin/phpunit) is not within the allowed path(s): (/srv/http/:/home/:/tmp/:/usr/share/pear/) in /usr/local/bin/phpunit on line 3
PHP Warning:  Phar::mapPhar(): open_basedir restriction in effect. File(/usr/local/bin/phpunit) is not within the allowed path(s): (/srv/http/:/home/:/tmp/:/usr/share/pear/) in /usr/local/bin/phpunit on line 714

Fatal error解決了,但警告還在,並且phpunit沒有正常運行。google

php對文件訪問的保護機制

google之,發現這裏有解釋: http://www.templatemonster.com/help/open_basedir-restriction-in-effect-filex-is-not-within-the-allowed-paths-y.htmlrest

PHP open_basedir protection tweak is a Safe Mode security measure that prevents users from opening files or scripts located outside of their home directory with PHP, unless the folder has specifically excluded. PHP open_basedir setting if enabled, will ensure that all file operations to be limited to files under certain directory, and thus prevent php scripts for a particular user from accessing files in unauthorized user’s account. When a script tries to open a file with, for example, fopen() or gzopen(), the location of the file is checked. When the file is outside the specified or permissible directory-tree, PHP will refuse to open it and the following errors may occur: ...code

意思是說:php.ini中的open_basedir是php爲保證安全進行文件訪問的設置。若是該選項被賦值,全部的文件操做將限定在特定的目錄裏,這樣能夠防止某個用戶使用php腳本讀取未受權的內容。當你想經過fopengzopen打開一個文件時,若是該文件的位置再也不被容許的目錄下面,就會出現上述的警告信息。htm

從警告信息發現能夠訪問的目錄包括 /srv/http/:/home/:/tmp/:/usr/share/pear/,恰好 ~/bin即在PATH變量中,也屬於能夠被php腳本讀取的目錄,因而ip

mv /usr/local/bin/phpunit ~/bin

再運行phpunit --version,獲得正確結果:

PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

phpunit安裝成功!

相關文章
相關標籤/搜索