PHP:无法exec()wkhtmltopdf:"sh:/usr/bin/wkhtmltopdf:权限被拒绝" [英] PHP: Cannot exec() wkhtmltopdf: "sh: /usr/bin/wkhtmltopdf: Permission denied"

查看:121
本文介绍了PHP:无法exec()wkhtmltopdf:"sh:/usr/bin/wkhtmltopdf:权限被拒绝"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是真的很喜欢unix,这就是为什么这件事对我来说很奇怪.

操作系统是CentOS 6.

我阅读了几乎所有有关wkhtmltopdf的主题,但是我的问题仍然存在.实际上,这不是wkhtmltopdf问题,而是权限问题.
概括起来:wkhtmltopdf在命令行中有效,但不适用于php exec().
但是wget与php exec()可以正常工作:

我可以在命令行中执行wkhtmltopdf,效果很好:
/usr/bin/wkhtmltopdf "test.de" "/var/www/html/test/test.pdf" 2>&1

我可以用php执行wget,效果很好:
exec('/usr/bin/wget -O /var/www/html/test/test123.txt "test.de" 2>&1', $output, $return_var);

但是我不能用php执行wkhtmltopdf:
exec('/usr/bin/wkhtmltopdf "test.de" "/var/www/html/test/test.pdf" 2>&1', $output, $return_var);

这将导致"sh:/usr/bin/wkhtmltopdf:权限被拒绝"

我检查了/usr/bin/wkhtmltopdf并将其与/usr/bin/wget进行了比较.
权限是相同的:rwxr-xr-x或0755
即使我将/usr/bin/wkhtmltopdf的chmod设置为777并将所有者设置为apache,错误仍然相同.

也许是百胜吗?! 因为我不是通过yum和wkhtmltopdf来安装wget的.这是我唯一能想到的区别.

我使用以下内容安装了wkhtmltopdf:

tar -vxjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
mv wkhtmltopdf-amd64 /usr/bin/wkhtmltopdf
chmod +x /usr/bin/wkhtmltopdf

还是SELinux可能有问题?我对此不太了解,但是我通过研究发现,"httpd_ssi_exec"和"httpd_execmem"可能会有所帮助,我都将其设置为是",但是权限被拒绝的问题仍然存在.

所以我想知道wget为什么不能与php一起使用,而wkhtmltopdf却不能呢? 希望有人可以帮助我,我可以不再为此生气;)

致以最诚挚的问候, 亚历克斯

PS:在我的脚本中,test.de是 http://test.de ,但我不允许发布两个以上的链接.

解决方案

即使权限为777,该命令也以www用户身份执行.由于www的特权级别非常低,因此会有很多限制.因此请使用 setuid文件权限

setuid表示在执行时设置用户ID.如果setuid位打开了文件,则执行该可执行文件的用户将获得拥有该文件的个人或组的权限.因此,您可以以root/高特权用户身份执行该命令.

I am not really into unix and that's why this thing is kind of weird for me.

The OS is CentOS 6.

I read nearly all topics concerning wkhtmltopdf, but my problem still exists. Actually it's not a wkhtmltopdf problem, but a permission problem.
To sum it up: wkhtmltopdf works in the command line, but not with php exec().
But wget works fine with php exec():

I can execute wkhtmltopdf in the command line, works fine:
/usr/bin/wkhtmltopdf "test.de" "/var/www/html/test/test.pdf" 2>&1

I can execute wget with php, works fine:
exec('/usr/bin/wget -O /var/www/html/test/test123.txt "test.de" 2>&1', $output, $return_var);

But I can NOT execute wkhtmltopdf with php:
exec('/usr/bin/wkhtmltopdf "test.de" "/var/www/html/test/test.pdf" 2>&1', $output, $return_var);

this leads to "sh: /usr/bin/wkhtmltopdf: Permission denied"

I checked /usr/bin/wkhtmltopdf and compared it to /usr/bin/wget.
The permissions are the same: rwxr-xr-x or 0755
Even if I set chmod of /usr/bin/wkhtmltopdf to 777 and set the owner to apache, the error is still the same.

Perhaps it's a matter of yum?! Becauce I installed wget through yum and wkhtmltopdf not. That is the only difference I can think of.

I installed wkhtmltopdf with the following:

tar -vxjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
mv wkhtmltopdf-amd64 /usr/bin/wkhtmltopdf
chmod +x /usr/bin/wkhtmltopdf

Or could it be a problem with SELinux? I don't know very much about it, but I read through my research that "httpd_ssi_exec" and "httpd_execmem" might help and I put both to "yes", but the permission denied issue still exists.

So I wonder why does wget work with php and wkhtmltopdf does not? Hopefully someone can help me out and I can stop getting mad on this ;)

best regards, Alex

P.S.: test.de is http://test.de in my scripts, but I am not allowed to post more than two links.

解决方案

Even though permission is 777 the command getting executed as www user.There will be lot of limitation since www has very low privilege levels.So use setuid File Permission

setuid means set user ID upon execution. If setuid bit turned on a file, user executing that executable file gets the permissions of the individual or group that owns the file.Hence you can execute that command as root/high privileged user.

这篇关于PHP:无法exec()wkhtmltopdf:"sh:/usr/bin/wkhtmltopdf:权限被拒绝"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆