Libreoffice permission issue under Nginx web server and PHP

We have a feature need to convert office documents to pdf files,because our servers are all CentOS, so we selected LibreOffice to do the conversion.

The command can run successfully under console, but failed in web server.

// PHP code.

$cmd = "libreoffice --headless ...";
exec($cmd, $output, $code);

Here we got $code with value 77, and $output as an empty array.

It’s a permission issue.But if we change the $cmd to someothers, for example “java xxx”, it’s all OK.

Our nginx server is running as user www, so we su – www and run the convertor command manually. And got these errors:

-bash-4.1$ /usr/bin/libreoffice –headless –convert-to pdf /tmp/fff.docx –outdir /tmp
[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx

We googled the web, and it seems an permission issue.But the file /user/bin/libreoffice has an normal property as other commands, there is no doubt that we have the execute permission on it.

So we begin inspect the user himself.

Ohh, www user’s home is set to /home, certainly he can do nothing in his home directory.We should change it somewhere else.

Create a new directory /home/www and make it the home directory for user www, su – www and then run the libreoffice command in console, this time we got no errors.

But we still get errors when run it form browser.After restart the Nginx and PHP-FPM service, all got worked.

Posted in Tech Tagged with: