I am trying use matplotlib on CentOS through a PHP script. If I run it on the command line it works fine and generates a PNG figure. If I run with another user,

sudo -u apache python filename.py

it gives error: Cannot obtain dvipng version.

Any ideas? I search the net but no answers. It looks like a bug? In the script I am using TeX.

rc('text', usetex=True)

Texlive and dvipng are installed and paths are set correctly.