phpunit安裝出錯的原因及解決辦法
官方指引
很遺憾, phpunit還沒有在ArchLinux的倉庫裡。
所以使用下載安裝的方式。按照官方的指引:
wget https://phar.phpunit.de/phpunit.pharchmod +x phpunit.pharsudo mv phpunit.phar /usr/local/bin/phpunitphpunit --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 3PHP Fatal error: Class 'Phar' not found in /usr/local/bin/phpunit on line 714
啟用phar擴充
先解決Fatal error: Class 'Phar' not found。
ls /usr/lib/php/modules
發現有 phar.so,說明Phar的擴充已經安裝,那麼是不是該擴充沒有Enable呢?
開啟 /etc/php/php.ini搜尋 phar,果然發現 extension=phar.so被注釋掉了。去掉該行前面的 ;,儲存php.ini,再次運行 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 3PHP 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沒有正常運行。
php對檔案訪問的保護機制
google之,發現這裡有解釋: http://www.templatemonster.com/help/open_basedir-restriction-in-effect-filex-is-not-within-the-allowed-paths-y.html
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: ...
意思是說:php.ini中的open_basedir是php為保證安全進行檔案訪問的設定。如果該選項被賦值,所有的檔案操作將限定在特定的目錄裡,這樣可以防止某個使用者使用php指令碼讀取未授權的內容。當你想通過fopen或gzopen開啟一個檔案時,如果該檔案的位置不再被允許的目錄下面,就會出現上述的警告資訊。
從警告資訊發現可以訪問的目錄包括 /srv/http/:/home/:/tmp/:/usr/share/pear/,剛好 ~/bin即在PATH變數中,也屬於可以被php指令碼讀取的目錄,於是
mv /usr/local/bin/phpunit ~/bin
再運行phpunit --version,得到正確結果:
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.
phpunit安裝成功!