Different behavior launching script from console, and from php script. How to fix it?

I’m planning to use blender to convert models online.
I wrote a simple script, that “open obj, convert to dae”.

import bpy, sys

file_loc = '/var/www/html/test.obj'
imported_object = bpy.ops.import_scene.obj(filepath=file_loc)
obj_object = bpy.context.selected_objects[0] ####<--Fix
bpy.ops.wm.collada_export(filepath="/var/www/html/test.dae") 

It works PERFECTLY by 2 ways

  1. Run via console (Ubuntu 16.04 Server)
    /usr/bin/blender --debug-all --background -noaudio --python /var/www/html/myscript.py
  2. Run via php script
    $output2 = exec("/usr/bin/blender --debug-all --background -noaudio --python /var/www/html/myscript.py");

Now i want to use addon exporter (obj -> bin)
In user preferences i enabled this addon and add path to scripts.
My export script has changed a little

import bpy, sys

file_loc = '/var/www/html/test.obj'
imported_object = bpy.ops.import_scene.obj(filepath=file_loc)
obj_object = bpy.context.selected_objects[0] ####<--Fix
bpy.ops.export_scene.b4w_json(do_autosave = False, override_filepath='/var/www/html/test.json')

It works perfectly via console
/usr/bin/blender --debug-all --background -noaudio --python /var/www/html/myscript.py
but NOT WORKING via php scriptBelow is Apache log with error

Traceback (most recent call last):
File “/var/www/html/myscript.py”, line 21, in <module>
bpy.ops.export_scene.b4w_json(do_autosave = False, override_filepath=’/var/www/html/test.json’)
File “/usr/share/blender/2.79/scripts/modules/bpy/ops.py”, line 189, in call
ret = op_call(self.idname_py(), None, kw)
AttributeError: Calling operator “bpy.ops.export_scene.b4w_json” error, could not be found

Very strange. Addon exporter activated (userprefs.blend) but it seems that PHP version cannot see it.
I’ve tried to point “user scripts” directory and enable addon inside python script


from bpy import context
path="/var/www/blender9/b4w"
context.user_preferences.filepaths.script_directory=path
bpy.ops.wm.addon_enable(module="blend4web")

but it was the same result

The question is:
Why script works fine via command line, and gives error via PHP triggering? How to fix it?