PHP: Cannot exec() wkhtmltopdf: “sh: /usr/bin/wkhtmltopdf: Permission denied”

假装没事ソ 提交于 2019-12-11 03:04:54

问题


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.


回答1:


You can use:

exec('/usr/local/bin/wkhtmltopdf --print-media-type index.html termo590.pdf 2>&1');



回答2:


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.




回答3:


If you don't want to disable SELinux another method is to use a cron to run this command./usr/bin/wkhtmltopdf "test.de" "/var/www/html/test/test.pdf" 2>&1

1)write argument name to a file from your php program.

 $Handle = fopen("myfile", 'w');
 fwrite($Handle, "test.de");
 fclose($Handle);

2) Create a bash script to read that argument name and convert it to pdf.

#!/bin/sh
arg=$(head -n 1 myfile)   
/usr/bin/wkhtmltopdf arg "/var/www/html/test/test.pdf" 2>&1

save it as pdf_converter.sh

chmod +x pdf_converter.sh

3) Configure the script in crontab

crontab -e and paste this

* * * * * pdf_converter.sh



回答4:


Having struggled with the Permission Denied problem I finally cracked it so for anyone else having this issue:

Apache's shell user (www-data) as default can only access files within var/www/

The Shell user simply doesn't have permission to run wkhtmltopdf in it's default directory (usr/local/bin in my case) using exec().

My workaround was simply to copy and paste the wkhtmltopdf from /usr/local/bin to /var/www/bin (I created the bin folder) and targeted that file in my exec

exec('/var/www/bin/wkhtmltopdf file.htm testfile.pdf');

Boom! PDF's.

Now this may not be safest practise (I don't know if there are security implications to having a bin folder in the var/www directory) but I my case it doesn't matter as this is a test server and for anyone just wanted to verify their other code is working it should at least generate you a test.pdf



来源:https://stackoverflow.com/questions/22560792/php-cannot-exec-wkhtmltopdf-sh-usr-bin-wkhtmltopdf-permission-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!