SASS编译器无法从exec运行

In a CentOS container, I am trying to get the SASS to execute from PHP's exec(). SASS is executable, I can execute the command successfully from inside the container manually, but not from the PHP.

Command I am trying is:

PHP

$command = '/usr/local/bin/sass ' . $webFolderPath . '/styles/style_303.scss' . ' ' . $webFolderPath . '/styles/style_303.css';
$command .= " 2>&1";
echo $command;
exec($command, $output, $return);

Translates to:

/usr/local/bin/sass /var/www/html/portal/web/styles/style_303.scss /var/www/html/portal/web/styles/style_303.css 2>&1

I get the following error.

/usr/share/rubygems/rubygems/path_support.rb:68:in `path=': undefined method `+' for nil:NilClass (NoMethodError)
    from /usr/share/rubygems/rubygems/path_support.rb:30:in `initialize'
    from /usr/share/rubygems/rubygems.rb:357:in `new'
    from /usr/share/rubygems/rubygems.rb:357:in `paths'
    from /usr/share/rubygems/rubygems.rb:379:in `path'
    from /usr/share/rubygems/rubygems/specification.rb:794:in `dirs'
    from /usr/share/rubygems/rubygems/specification.rb:658:in `each_normal'
    from /usr/share/rubygems/rubygems/specification.rb:669:in `_all'
    from /usr/share/rubygems/rubygems/specification.rb:822:in `each'
    from /usr/share/rubygems/rubygems/specification.rb:864:in `find'
    from /usr/share/rubygems/rubygems/specification.rb:864:in `find_inactive_by_path'
    from /usr/share/rubygems/rubygems.rb:175:in `try_activate'
    from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:132:in `rescue in require'
    from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:144:in `require'
    from <internal:abrt_prelude>:2:in `<compiled>'

Versions:

Sass: 3.5.4 (Bleeding Edge)

Ruby: 2.0.0p648 (2015-12-16) [x86_64-linux]

Gem: 2.0.14.1

CentOS: 7

Any idea, if I am missing something?

douxing1969
douxing1969 好吧,如果脚本是可执行的,任何用户都可以执行它。但是感谢你的帮助,我设法找出问题所在。看看我的答案。
2 年多之前 回复
dongxuan1660
dongxuan1660 它看起来像在你的容器中你以root身份运行sass。<?phpechoexec('whoami');将告诉您PHP用户是谁,然后您可以尝试以该用户身份运行sass以查看是否存在任何权限问题。
2 年多之前 回复
duanpu1963
duanpu1963 我已经提到那些:)
2 年多之前 回复
dte8665
dte8665 我明白。你有Google(第一个)错误吗?(未定义的方法`+'代表nil:NilClass(NoMethodError))有一些点击它,也许在Stack上的一些问答中有些东西可能会导致你找到问题。
2 年多之前 回复
drmet46444
drmet46444 是的,我明白了。对于不同的可执行文件,这些是不同的。我的尚未弄明白。
2 年多之前 回复
dongyao2129
dongyao2129 别客气。我认为你可能还必须在Jay必须做的命令中传递另一个变量,这就是为什么我认为它可能有所帮助。
2 年多之前 回复
doumeng06063991
doumeng06063991 感谢您的帮助。但是您的链接问题是尝试从PHP执行docker命令。而我正在尝试运行SASS。
2 年多之前 回复
duandang6352
duandang6352 TBH,我不知道SASS,但有些语法看起来与Docker类似,有人遇到类似的问题。如果是这样,你可能想看看JayBlanchard撰写的这篇问答报告stackoverflow.com/q/47997965/1415724
2 年多之前 回复
duanhuanyou6478
duanhuanyou6478 我添加了PHP代码。在Docker容器中是,设置说明可能与问题的背景有很大差异,具体您想知道什么?PHP,Ruby,Gem,Sass都安装在同一个容器中,PHP从另一个容器(Web服务)接收到php-fpm的代理请求后处理脚本
2 年多之前 回复
dongshenghe1833
dongshenghe1833 同时显示您的PHP代码。你提到过“容器”;你在Docker容器中运行PHP和SASS/Ruby吗?这些是如何建立的?
2 年多之前 回复
dongwolu5275
dongwolu5275 像什么?你能告诉我一些细节吗?
2 年多之前 回复
dougou6114
dougou6114 问题可能是,缺少环境变量。
2 年多之前 回复

2个回答



好的,我设法解决了问题。 sass </ code>需要主目录才能正常运行。 如果你作为系统用户执行PHP,在我的情况下,它没有主目录,这就是它失败的原因。</ p>

在Linux上,你可以使用 mkhomedir_helper [user] </ code>创建主目录。 创建主目录后, sass </ code>将开始工作。</ p>
</ div>

展开原文

原文

Ok, I manage to figure the problem out. The sass requires a home directory to operate correctly. If you are executing PHP as a system user, which in my case it was, it does not have a home directory, that is why it failed.

On Linux, you can use mkhomedir_helper [user] to create a home directory. Once you have created a home directory, sass will start to work.

I had a similar problem earlier, when I tried to run grunt from inside a PHP script. It turned out, the problem was the missing environment variables.

So if you could execute your sass command in the bash, but it doesn't work from inside the php script, the following could help you solve your problem.

To list your current environment vars, you could use the unix command printenv. This should give you a list like the following

LANG=en_US.UTF-8
rvm_bin_path=/usr/local/rvm/bin
rvm_version=1.26.11 (latest)
RUBY_VERSION=ruby-2.2.1
GEM_HOME=/usr/local/rvm/gems/ruby-2.2.1
...

As scss uses ruby, it is important to have all the ruby env vars available. So you could pick everything you need from printenvs output and write it inside an array.

$env = [
    "LANG" => "en_US.UTF-8",
    "rvm_bin_path" => "/usr/local/rvm/bin",
    ...
];
$env_command = '';
foreach ($env as $var => $value) {
    $env_command .= "$var=$value ";
}

$command = "/usr/local/bin/sass ...";

exec "$env_command $command";

I'm not sure, which vars are actual important - so you have to figure this out on your own.

duanlan5320
duanlan5320 控制台,是的,它正在工作。
2 年多之前 回复
dongtan7639
dongtan7639 你确定,scss实际上是在你的容器内工作吗? 你是如何执行printenv的? 使用php或从控制台?
2 年多之前 回复
duanmeng1862
duanmeng1862 我认为除了PATH之外的任何东西都不相关,所以我尝试了这一点,并没有带来任何改变。
2 年多之前 回复
dptk0969
dptk0969 我正在尝试获得与您类似的环境变量,但我得到HOSTNAME = 435f9c1b57ba TERM = xterm OLDPWD = / var / www / html / portal LS_COLORS = rs = 0:di = 01 PATH = / usr / local / php / bin :/ usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin PWD = / var / www / html / portal / web / styles SHLVL = 1 HOME = / root LESSOPEN = _ = / usr / bin / printenv
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问