phpunit安裝出錯的原因及解決方法

WBOY
發布: 2016-08-08 09:29:50
原創
1276 人瀏覽過

官方指引

很遺憾, phpunit還沒有在ArchLinux的倉庫裡。

所以使用下載安裝的方式。按照官方的指引:

<code>wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version</code>
登入後複製

結果得到下面的錯誤:

<code>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</code>
登入後複製

啟用phar擴充

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

<code>ls /usr/lib/php/modules</code>
登入後複製

發現有 phar.so,表示Phar的擴充已經安裝,那麼是不是該擴充沒有Enable呢?
開啟 /etc/php/php.ini搜尋 phar,果然發現 extension=phar.so被註解掉了。去掉該行前面的 ;,保存php.ini,再運行 phpunit --version

<code>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</code>
登入後複製

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. 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 loopenis 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腳本讀取未授權的內容。當你想要透過fopengzopen開啟一個檔案時,如果該檔案的位置不再被允許的目錄下面,就會出現上述的警告訊息。

從警告訊息發現可以存取的目錄包括/srv/http/:/home/:/tmp/:/usr/share/pear/,剛好~/bin即在PATH變數中,也屬於可以被php腳本讀取的目錄,於是

<code>mv /usr/local/bin/phpunit ~/bin</code>
登入後複製

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

<code>PHPUnit 4.5.0 by Sebastian Bergmann and contributors.</code>
登入後複製

phpunit安裝成功!

以上就介紹了phpunit安裝出錯的原因及解決方法,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!