php cli配置文件問題

引言

今天在教別人使用protobuf的時候,無心中發現了一個php cli模式下的詭異問題,費了老半天的找到解決方法了,這裏拿出來分享下。php

問題描述

咱們這邊最早引入了protobuf協議,使用的是allegro/php-protobuf這個擴展安裝的。這幾天其餘同事也要用到這個,因而我將protobuf生成php類庫文件的部分放到了一個公共的位置/data/php_proto/文件夾下,裏面有這些內容,git

drwsrwxr-x 2 yanruitao users 4096 10月 14 17:05 Dmp
-rwxrwxr-x 1 yanruitao users  472 10月 10 17:59 dmp.proto
drwsrwxr-x 2 yanruitao users 4096 10月 10 17:54 ProtobufCompiler
-rwxrwxr-x 1 yanruitao users 2969 3月   9 2015 protoc-php.php

後面兩個是必須的,生成pb協議實現類庫文件時會用到,前面兩個是一個簡單的例子,使用也很簡單,就像下面這樣。github

php protoc-php.php -n --psr dmp.proto //-n是使用命名空間,--psr是使用psr-4標準

等同事用的時候發現會報錯,web

PHP Warning:  file_put_contents(pb_proto_dmp.php): failed to open stream: Permission denied in /data/php_proto/ProtobufCompiler/ProtobufParser.php on line 309

同事問個人時候我直接說,加上sudo就能夠了,結果加上sudo問題更嚴重了,原本裝好的擴展程序加載不成功,通過一番排查發下有下面這麼一個問題:redis

$yanruitao: php -i | grep 'php.ini'
$yanruitao: sudo php -i | grep 'php.ini'

兩種用戶加載的不是同一個配置文件,普通用戶加載的是/usr/local/php/lib/php.ini,而root用戶加載的是/etc/php.ini這個文件,我看了下這兩個文件內容不一致,哈哈,好開心,趕忙把先把/etc/php.ini備份,/usr/local/php/lib/php.ini複製到/etc/php.ini,複製完了以後,我再使用的時候嚇我一跳,報了一大堆錯誤:json

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/web_stub_cntl.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/web_stub_cntl.so: undefined symbol: zend_new_interned_string in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/web_stub_cntl.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/web_stub_cntl.so: undefined symbol: zend_new_interned_string in Unknown on line 0
PHP Warning:  PHP Startup: test: Unable to initialize module
Module compiled with module API=20100525
PHP    compiled with module API=20090626
These options need to match
 in Unknown on line 0

Warning: PHP Startup: test: Unable to initialize module
Module compiled with module API=20100525
PHP    compiled with module API=20090626
These options need to match
 in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/protobuf.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/protobuf.so: undefined symbol: zend_new_interned_string in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/protobuf.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/protobuf.so: undefined symbol: zend_new_interned_string in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/imagick.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/imagick.so: undefined symbol: zend_new_interned_string in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/imagick.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/imagick.so: undefined symbol: zend_new_interned_string in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/data/boss/phpredis-develop/modules/redis.so' - /data/boss/phpredis-develop/modules/redis.so: undefined symbol: zend_new_interned_string in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/data/boss/phpredis-develop/modules/redis.so' - /data/boss/phpredis-develop/modules/redis.so: undefined symbol: zend_new_interned_string in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/curl.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/curl.so: cannot open shared object file: No such file or directory in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/curl.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/curl.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/fileinfo.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/fileinfo.so: cannot open shared object file: No such file or directory in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/fileinfo.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/fileinfo.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/gd.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/gd.so: cannot open shared object file: No such file or directory in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/gd.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/gd.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/json.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/json.so: cannot open shared object file: No such file or directory in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/json.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/json.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/phar.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/phar.so: cannot open shared object file: No such file or directory in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/phar.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/phar.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/data/boss/phpredis-develop/modules/redis.so' - /data/boss/phpredis-develop/modules/redis.so: undefined symbol: zend_new_interned_string in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/data/boss/phpredis-develop/modules/redis.so' - /data/boss/phpredis-develop/modules/redis.so: undefined symbol: zend_new_interned_string in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/zip.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/zip.so: cannot open shared object file: No such file or directory in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/zip.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/zip.so: cannot open shared object file: No such file or directory in Unknown on line 0
protoc-php.php requires protobuf extension installed to run

這讓我摸不着頭腦了。curl

時間過去了很久,沒有發現任何突破,卻發現了一個備用的解決這個問題的方案,修改/data/php_proto/文件夾權限爲0775,其餘普通用戶就能夠直接使用了,不用加sudo(這個是個人錯,忘記修改文件夾權限了,這也讓我發現了這個詭異的問題)ui

chmod -R 0775 /data/php_proto

看了報的這些錯誤,大部分都是擴展的問題,有的是擴展加載不成功,有的是擴展不存在,我就納悶了,爲何普通用戶能夠,root用戶就不能夠。this

看到root用戶php -i的時候,配置文件的地方跟非root用戶有不一致的地方:url

//非root
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)

//root
Scan this dir for additional .ini files => /etc/php.d
Additional .ini files parsed => /etc/php.d/curl.ini,
......

命名加載的是同樣的配置文件怎麼加載以後的內容就不同呢?
搜了半天,知道了報的錯裏面有部分是跟這個有關係的,可是這個在哪裏修改搜了老久也沒有搜到。

突然想到root用戶和非root用戶使用的php可執行文件是不一致的

root => /usr/bin/php
非root => /usr/local/bin/php

嘗試着對這兩個文件使用了php -v,結果下了我一跳,root用戶的版本是5.3.3,非root用戶是5.4.6,呵呵,我就呵呵了,誰部署的環境~,坑人啊。

嘗試着將原有/usr/bin/php備份後,複製/usr/local/bin/php過來覆蓋掉,在使用php -v發現版本一致了,也不報錯了,再次去執行php protoc-php.php -n --psr dmp.proto,發現all is well,至此問題解決。

糾結了半天,結果就是版本的問題。

總結

多思考,多換個角度思考。 作事要完善啊,別給後輩留下一些詭異問題←_←。

相關文章
相關標籤/搜索