Can't jekyll build as www-data

In my php program I do exe("jekyll build"); as www-data but the command seems to be not working. I can manually issue jekyll build command.

I presume you meant exec(). Since PHP is running as www-data, there are likely issues with file/dir permissions and environment setups (PATH, GEM_HOME, etc). The error output/result-code from the exec call should provide more info.

It returns jekyll: not found. How to allow www-data to use jekyll?

I need to add GEM_HOME and $HOME/gems to Apache envvar file.

Possibly the simplest thing to do is to create a shell script to run Jekyll with the correct environment. Something like build-site.sh:

#!/bin/bash

PATH=<path-for-ruby-jekyll>:$PATH
export GEM_HOME=<path-for-gem-home>

jekyll build

Then you would execute the script from PHP:

exec('/home/user/build-site.sh');
# or:
exec('bash /home/user/build-site.sh');