Introduction
Today when I was teaching others to use protobuf, I accidentally discovered a strange problem in the php cli mode. It took me a long time to find a solution. I will share it here.
Problem Description
We first introduced the protobuf protocol here and installed it using the allegro/php-protobuf extension. Other colleagues will also use this in the past few days, so I put the part where protobuf generates the PHP class library file in a public location /data/php_proto/ folder, which contains these contents,
drwsrwxr-x 2 yanruitao users 4096 October 14 17:05 Dmp
-rwxrwxr-x 1 yanruitao users 472 October 10 17:59 dmp.proto
drwsrwxr-x 2 yanruitao users 4096 October 10 17:54 ProtobufCompiler
-rwxrwxr-x 1 yanruitao users 2969 March 9 2015 protoc-php.php
The last two are necessary and will be used when generating the pb protocol implementation class library file. The first two are a simple example and are very simple to use, just like below.
php protoc-php.php -n --psr dmp.proto //-n is to use the namespace, --psr is to use the psr-4 standard, etc. When you use it, you will find an error,
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
When my colleague asked me, I directly said that adding sudo would be enough. As a result, adding sudo made the problem more serious. The originally installed extension failed to load successfully. After some troubleshooting, I found the following problem:
$yanruitao: php -i | grep 'php.ini'
$yanruitao: sudo php -i | grep 'php.ini'
The two users do not load the same configuration file. The ordinary user loads /usr/local/php/lib/php.ini, while the root user loads the /etc/php.ini file. I looked at it. The contents of these two files are inconsistent. Haha, I am so happy. I quickly backed up /etc/php.ini and copied /usr/local/php/lib/php.ini to /etc/php.ini. After copying, I was shocked when I tried to use it again, and a lot of errors were reported:
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
这让我摸不着头脑了。
时间过去了好久,没有发现任何突破,却发现了一个备用的解决这个问题的方案,修改/data/php_proto/文件夹权限为0775,其他普通用户就可以直接使用了,不用加sudo(这个是我的错,忘记修改文件夹权限了,这也让我发现了这个诡异的问题)
chmod -R 0775 /data/php_proto
看了报的这些错误,大部分都是扩展的问题,有的是扩展加载不成功,有的是扩展不存在,我就纳闷了,为什么普通用户可以,root用户就不可以。
看到root用户php -i的时候,配置文件的地方跟非root用户有不一致的地方:
//非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
Non-root => /usr/local/bin/php
I tried using php -v on these two files, but I was shocked. The root user version is 5.3.3, and the non-root user version is 5.4.6. Haha, I was confused. Who deployed it? The environment is so deceptive.
Try to back up the original /usr/bin/php and copy /usr/local/bin/php to overwrite it. After using php -v, I found that the version is consistent and no error is reported. Then execute php protoc again. -php.php -n --psr dmp.proto, found that all is well, the problem is solved.
After struggling for a long time, it turned out to be a version problem.