我能够从shell和exec()
fine运行PhantomJS。我使用的服务器在~/.fonts/
目录中查找其他字体。当这些额外的字体出现时,仅从shell中我就可以用PhantomJS截图,并且预期的字体渲染得很好。
> strace ~/public_html/api/libraries/phantomjs-2.1.1-linux-x86_64/bin/phantomjs ~/public_html/api/libraries/phantomjs-2.1.1-linux-x86_64/examples/rasterize.js http://v1.jontangerine.com/silo/typography/web-fonts/ ~/tmp/test.jpg | grep font
open("/home/user1/.fonts/TTF/verdana.ttf", O_RDONLY) = 11
open("/home/user1/.fonts/TTF/andalemoo .TTF", O_RDONLY) = 11open("/home/user1/.fonts/TTF/arial.ttf", O_RDONLY) = 11
open("/home/user1/.fonts/TTF/court . TTF ", O_RDONLY) = 11
open("/home/user1/.fonts/TTF/georgia.ttf", O_RDONLY) = 11
open("/home/user1/.fonts/TTF/impact.ttf", O_RDONLY) = 11
…
当我从exec()
中尝试相同的命令时,不会搜索用户字体目录。
<?php
exec('strace ~/public_html/api/libraries/phantomjs-2.1.1-linux-x86_64/bin/phantomjs ~/public_html/api/libraries/phantomjs-2.1.1-linux-x86_64/examples/rasterize.js http://v1.jontangerine.com/silo/typography/web-fonts/ ~/tmp/test.jpg | grep font');
不搜索~/.fonts
目录,但将屏幕截图写入磁盘,而没有呈现适当的字体。
我理解exec()
作为Apache用户运行,所以用户字体不会被使用。然而,
> whoami
user1
和
<?php
echo exec('whoami');
user1
显示为相同的用户,所以我怀疑这是误导,因为这在shell中工作得很好(字体和所有):
php -r "exec('~/public_html/api/libraries/phantomjs-2.1.1-linux-x86_64/bin/phantomjs ~/public_html/api/libraries/phantomjs-2.1.1-linux-x86_64/examples/rasterize.js http://v1.jontangerine.com/silo/typography/web-fonts/ ~/tmp/test.jpg');"
我知道setuid
可以允许用户以其所有者(user1)的权限执行程序,但这没有帮助。这个特定的服务器是一个共享服务器,并且su
和sudo
被禁用,因此不允许作为不同的用户运行。
Linux用户配置不是我的专业领域,但是使用exec()
如何运行PhantomJS命令使用户字体包括在内?
研究:
- 使用exec()从PHP运行PhantomJS -问题是$PATH
- PHP + PhantomJS栅格化-这是HostGator的一个问题
- exec()和phantomjs的绝对路径问题-一个答案建议将命令作为cron运行,这将不起作用。也不能修改
/etc/sudoers
。
问题在分别运行printenv
和exec('printenv')
时显示出来
HOME=/home/user1
和
HOME=/home/user1/tmp
这意味着即使whoami
和exec('whoami')
都返回user1
,共享主机配置中的某些内容将主目录设置为/tmp
,这对沙箱脚本是明智的。
解决方案是在exec命令前添加export HOME=~;
(而不是 HOME=~;
)。例如
exec('export HOME=~; strace ~/public_html/api/libraries/phantomjs-2.1.1-linux-x86_64/bin/phantomjs ~/public_html/api/libraries/phantomjs-2.1.1-linux-x86_64/examples/rasterize.js http://v1.jontangerine.com/silo/typography/web-fonts/ ~/tmp/test.jpg | grep font');
这将导致HOME
目录被设置,并且现在可以搜索用户字体。