2

I am running a php application on an apache server. From the php application I need to run a ruby script on the command line. I have verified that running the script directly from a terminal as user, works properly. The local ruby environment for user is setup by setting a path variable and sourcing /home/user/.rvm/scripts/rvm which allows the script to load the requirements with require statements.

Now through the php application the script fails at these require statements and I assume this is because there is no ruby environment setup for the www-data user. I have never used ruby before and I am not sure what the correct way would be of setting that environment

1 Answer 1

1

I was able to figure this out. I posted the answer to my question on SO. Hope it helps!

https://stackoverflow.com/a/40224414/1672458

I was able to figure this out. First, I installed rvm as a multi-user installation to ensure the www-data account can access it.

$ curl -sSL https://get.rvm.io | sudo bash -s stable

Install the desired ruby version, in my case 2.3.1, then set rvm to use it:

$ rvm install 2.3.1
$ rvm use 2.3.1

Run gem to install any gems that are needed. Because rvm is a multi-user installation, these gems are stored to the system and not your specific user.

$ gem install packagename

I don't know if this is necessary, but I would close the SSH session and reopen it. rvm messes with environment variables, so better safe than sorry.

Run env to print all environment variables. printenv also works if env doesn't for some reason. You'll get a big list of everything set, you only need the ruby-related ones. Do not copy/paste these values, they are examples I pulled from my system. Yours will be different!

PATH=/usr/local/rvm/gems/ruby-2.3.1/bin:/usr/local/rvm/gems/ruby-2.3.1@global/bin:/usr/local/rvm/rubies/ruby-2.3.1/bin:/usr/local/rvm/bin:/home/steven/bin:/home/steven/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
rvm_bin_path=/usr/local/rvm/bin
GEM_HOME=/usr/local/rvm/gems/ruby-2.3.1
IRBRC=/usr/local/rvm/rubies/ruby-2.3.1/.irbrc
MY_RUBY_HOME=/usr/local/rvm/rubies/ruby-2.3.1
rvm_path=/usr/local/rvm
rvm_prefix=/usr/local
rvm_ruby_string=ruby-2.3.1
GEM_PATH=/usr/local/rvm/gems/ruby-2.3.1:/usr/local/rvm/gems/ruby-2.3.1@global
RUBY_VERSION=ruby-2.3.1

Now we need PHP to recognize these variables. You'll need to find the right file on your system, which can be tricky. I don't have a way of knowing which one is correct, I used trial and error.

The file on my system is /etc/php/5.6/fpm/pool.d/www.conf. Add all of the environment variables you previously grabbed into this file with the below format. Note that you DO need PATH in here as well!

env[rvm_path] = /usr/local/rvm
env[rvm_prefix] = /usr/local

Now restart php-fpm. Your service name may be different from mine; I'm using the 5.6 build from ondrej/php.

Ubuntu 15.04 and newer (systemd):

$ sudo systemctl restart php5.6-fpm

Ubuntu 14.10 and newer:

$ sudo service php5.6-fpm restart

Finally, in the script itself you'll need to cd to the directory you're running the bundle command from. My short script is this:

cd /opt/slate
/usr/bin/git reset --hard
/usr/bin/git pull
bundle exec middleman build --clean
cp -R /opt/slate/build/* /var/www/docs

Works for me!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .