[英]How to run bundle from PHP script
I'm writing a webhook to automatically publish a site when I push to GitHub. 我正在编写一个Webhook,以在推送到GitHub时自动发布网站。 Part of the process requires that I build the site with
该过程的一部分要求我使用
bundle exec middleman build --clean
I'm trying to invoke that with a PHP script, the script called by the GitHub webhook, so the user is www-data
. 我正在尝试使用PHP脚本(由GitHub webhook调用的脚本)调用该脚本,因此用户为
www-data
。 No matter what I try, however, I'm getting an error that bundle
cannot be found. 但是,无论我尝试什么,都会遇到无法找到
bundle
的错误。
How can I run a bundle
command from a PHP script? 如何从PHP脚本运行
bundle
命令?
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. 首先,我将rvm安装为多用户安装,以确保
www-data
帐户可以访问它。
$ 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: 在我的情况下,安装所需的ruby版本2.3.1,然后将rvm设置为使用它:
$ rvm install 2.3.1
$ rvm use 2.3.1
Run gem
to install any gems that are needed. 运行
gem
安装所需的所有gem。 Because rvm is a multi-user installation, these gems are stored to the system and not your specific user. 由于rvm是多用户安装,因此这些gems将存储在系统中,而不是您的特定用户。
$ gem install packagename
I don't know if this is necessary, but I would close the SSH session and reopen it. 我不知道这是否必要,但是我将关闭SSH会话并重新打开它。 rvm messes with environment variables, so better safe than sorry.
rvm弄乱了环境变量,因此安全胜过遗憾。
Run env
to print all environment variables. 运行
env
以打印所有环境变量。 printenv
also works if env
doesn't for some reason. 如果
env
由于某种原因不起作用, printenv
也可以工作。 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. 现在我们需要PHP来识别这些变量。 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
. 我系统上的文件是
/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! 请注意,您也确实需要在其中输入
PATH
!
env[rvm_path] = /usr/local/rvm
env[rvm_prefix] = /usr/local
Now restart php-fpm
. 现在重新启动
php-fpm
。 Your service name may be different from mine; 您的服务名称可能与我的不同。 I'm using the 5.6 build from ondrej/php .
我正在使用ondrej / php的5.6版本。
Ubuntu 15.04 and newer (systemd): Ubuntu 15.04及更高版本(systemd):
$ sudo systemctl restart php5.6-fpm
Ubuntu 14.10 and newer: Ubuntu 14.10及更高版本:
$ 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. 最后,在脚本本身中,您需要
cd
到正在运行bundle
命令的目录。 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! 为我工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.