LoginSignup
4
5

More than 5 years have passed since last update.

ChefでCentOS 6.7 + nodejs + npmを設置する。

Last updated at Posted at 2016-03-08

関連記事

rubyからjavascriptコードを実行

  • railsではassets precompileなどのjavascriptの実行のためにtherubyracerというgemを提供している。
  • しかし、therubyracerはメモリ使用量が大きくてHerokuでは非推奨となっている。
  • therubyracerの代わりにnodejsを設置することを推奨する。
  • どうせphantomjsを設置するためにはnodejsは必要だ。

クックブックの作成

  • Chef社のSupermarketであるnodejsをみたが、簡単に設置することだけなのに余計に複雑だったので自作する。
% bin/knife cookbook create nodejs -o site-cookbooks

レシピの作成

  • nodeはyumで設置しても構わないが、npmはsourceで設置することを推奨する。
  • yumで提供するnpmは余計なpackageがたくさん設置されるし古い。
  • site-cookbooks/nodejs/recipes/default.rb
package "nodejs" do
  action :install
end

execute "install npm" do
  command "curl -L https://www.npmjs.com/install.sh | sh"
  not_if "which npm"
end

%w(phantomjs).each do |pkg|
  execute "npm install #{pkg}" do
    command "npm -g install #{pkg}"
    not_if "npm -g ls --depth=0 | grep #{pkg}"
  end
end

実行結果

% bin/knife node run_list add dev 'recipe[nodejs]' -z
% bin/knife solo cook dev -E development

... snip ...

Recipe: nodejs::default
  * yum_package[nodejs] action install
    - install version 0.10.42-4.el6 of package nodejs
  * execute[install npm] action run
    - execute curl -L https://www.npmjs.com/install.sh | sh
  * execute[npm install phantomjs] action run
    - execute npm -g install phantomjs

ノード上で確認

% ssh dev
$ node -v
v0.10.42
$ npm -v
3.7.5
$ npm -g ls --depth=0
/usr/lib
├── npm@3.7.5
└── phantomjs@2.1.3

$ which node
/usr/bin/node
$ which npm
/usr/bin/npm

参考

  • Chef活用ガイド
  • Chef実践入門
4
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5