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?

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.