-1

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?

12
  • The problem might be, the missing environment variables.
    – Philipp
    Dec 30, 2017 at 13:52
  • @Philipp, Like what? Can you give me some details?
    – Starx
    Dec 30, 2017 at 13:54
  • Show your PHP code as well. You mentioned "container"; are you running PHP and SASS/Ruby in Docker containers? How are those set up? Dec 30, 2017 at 13:55
  • @AndréDion, I have added the PHP code. Yes in a docker container, the setup explanation might be a lot out of context for the question, what specifically do you want to know? PHP, Ruby, Gem, Sass are all installed in the same container, PHP processes the script after receiving proxy request to php-fpm from another container (the web service)
    – Starx
    Dec 30, 2017 at 14:02
  • @Starx TBH, I don't know SASS but some of the syntax does look similar to Docker, to which someone had a similar problem. If so, you might like to have a look at this Q&A by Jay Blanchard stackoverflow.com/q/47997965/1415724 Dec 30, 2017 at 14:18

2 Answers 2

0

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.

3
  • I am trying to get environment variables similar to yours, but I am getting 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
    – Starx
    Dec 30, 2017 at 14:13
  • I didn't think anything other than PATH would be relevant, so I tried with that, and that didn't bring any change.
    – Starx
    Dec 30, 2017 at 14:14
  • Are you sure, scss is actually working inside your container? And how did you execute printenv? With php or from the console?
    – Philipp
    Dec 30, 2017 at 14:15
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.