I call a Node script from PHP on my Ubuntu web server, as in
// The following PHP executes in the context of the Apache user - 'www-data'
//
$execstring = "node " . $args;
exec($execstring, $output, $return_value);
As noted, the PHP code executes in the context of the Apache user, which is www-data
by default on Ubuntu.
I would like to be able to use nvm
to switch Node versions for the www-data
user.
(Note: I might be able to switch Node versions without using nvm
by specifying the full path to a different Node installation - I have not tried this yet, but it's not my question.)
Following instructions here, I install and use nvm
quite easily for other users:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
and
nvm install 4.1.2
I have found that I must install nvm
separately for every user. This means that nvm
does not install for the www-data
user when I run the command above as the login user (ubuntu
); and, conversely, switching my Node version for the login user (ubuntu
) has no effect on the www-data
user.
One more important detail: The www-data
user is special. This user has severely restricted functionality. To switch to the www-data
user in the shell, this special command is required:
sudo su www-data -s /bin/sh
(...And even the above command won't work without other, previous special steps, not noted here, to allow a shell prompt for the www-data
user.)
When logged in as the www-data
user, however, although I seem to be able to install nvm
using the command noted previously, I cannot run nvm
. (The error is nvm not found
.)
Attempting to overcome this, I made the brutal mistake of following the suggestion here to attempt to switch the Node version globally (i.e., for all users) while logged in as (I think, but forget now) the root
user (oops!):
// Danger! Do not do this!
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
Unfortunately, running the above command caused my sudo
access to break, and now I must apparently rebuild the server from scratch.
My question is: How can I install and use nvm
as the Apache user (www-data
) to allow a web server to run a Node process of a given version, and thereby control which version of Node the web server accesses - with the convenience of using nvm
?