ubuntu12.04.3 rvm ruby on rails nginx passenger nodejs环境搭建

1.安装rvm
创建部署用户:

root@ubuntu-template-ror:~# adduser deploy

添加到sudo组

root@ubuntu-template-ror:~# usermod -a -G sudo deploy

安装依赖包

sudo apt-get -y update
sudo apt-get -y upgrade     这步慎用,会升级底层依赖包包括内核.
sudo apt-get -y install build-essential wget curl git-core libcurl4-openssl-dev zlib1g-dev libxml2-dev libxslt-dev libssl-dev libpcre3-dev libmysqlclient-dev libmagickwand-dev libreadline-dev imagemagick gawk libyaml-dev libsqlite3-dev sqlite3 libxslt1-dev autoconf libgdbm-dev libncurses5-dev automake bison libffi-dev make gcc python-pip
sudo apt-get install libgmp3-dev

切换到deploy 用户, 单用户方式安装rvm:

root@ubuntu-template-ror:~# su - deploy

Installing the stable release version:
deploy@ubuntu-template-ror:~$ \curl -sSL https://get.rvm.io | bash -s stable

-安装完成后添加deploy到rvm组
sudo usermod -a -G rvm deploy
-
加载初始化脚本

deploy@ubuntu-template-ror:~$ source /home/deploy/.rvm/scripts/rvm
修改rvm源:
deploy@ubuntu-template-ror:~$ sed -i 's!cache.ruby-lang.org/pub/ruby!ruby.taobao.org/mirrors/ruby!' $rvm_path/config/db
更新rvm:
deploy@ubuntu-template-ror:~$  rvm get stable

2.安装ruby

echo gem: --no-rdoc --no-ri >> ~/.gemrc  #生产环境,安装gem的时候就不用文档什么的了
rvm install 2.1.0
首次安装会要求输入deploy的密码进行相关包的安装

3.安装rails
建立一个gemset

deploy@ubuntu-template-ror:~$ rvm use 2.1.0

创建一个名字为rails-4.0.2的gem空间

deploy@ubuntu-template-ror:~$ rvm gemset create rails-4.0.2

指定默认空间

deploy@ubuntu-template-ror:~$ rvm use 2.1.0@rails-4.0.2  --default
deploy@ubuntu-template-ror:~$ rvm gemset list 

安装gem之前,国内服务器一般要先替换成淘宝源:

gem source -l
gem source -r https://rubygems.org/
gem sources -a http://ruby.taobao.org/
deploy@ubuntu-template-ror:~$ gem install rails -v 4.0.2  (installing ri documentation for rails-4.0.2 装到这里的时候卡住了,文档没装应该也没关系,就ctrl+C中断了)
rails -v

4.安装passenger

gem install passenger 
passenger -v

5.安装nginx
先下载最新stable version nginx

deploy@ubuntu-template-ror:~$ wget http://nginx.org/download/nginx-1.4.4.tar.gz
deploy@ubuntu-template-ror:~$ tar xf nginx-1.4.4.tar.gz        解压,待会会用到

先安装依赖包

deploy@ubuntu-template-ror:~$ sudo apt-get install libcurl4-openssl-dev -y

开始正式安装

deploy@ubuntu-template-ror:~$ rvmsudo passenger-install-nginx-module           #  这一步可能要重新登录到deploy的终端

这个安装界面做的挺好,提示很全,照着提示进行就可以

然后在出现nginx安装提示的时候选择2,并进行如下设置:

--------------------------------------------

Where is your Nginx source code located?

Please specify the directory: /home/deploy/nginx-1.4.4/

--------------------------------------------

Where do you want to install Nginx to?

Please specify a prefix directory [/opt/nginx]: /usr/local/nginx

--------------------------------------------

Extra Nginx configure options

If you want to pass extra arguments to the Nginx 'configure' script, then
please specify them. If not, then specify nothing and press Enter.

If you specify nothing then the 'configure' script will be run as follows:

  sh ./configure --prefix='/usr/local/nginx' --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-cc-opt='-Wno-error' --with-pcre='/tmp/passenger.6kplxw/pcre-8.32' --add-module='/home/deploy/.rvm/gems/ruby-2.1.0@rails-4.0.2/gems/passenger-4.0.33/ext/nginx'

Extra arguments to pass to configure script: --user=deploy --group=deploy --with-http_realip_module --with-http_gunzip_module  如果想要nginx支持解压缩请求数据包,就加上: --with-http_gunzip_module

--------------------------------------------

Confirm configure flags

The Nginx configure script will be run as follows:

  sh ./configure --prefix='/usr/local/nginx' --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-cc-opt='-Wno-error' --with-pcre='/tmp/passenger.6kplxw/pcre-8.32' --add-module='/home/deploy/.rvm/gems/ruby-2.1.0@rails-4.0.2/gems/passenger-4.0.33/ext/nginx' --with-http_realip_module

Is this what you want? (yes/no) [default=yes]:

然后就开始编译安装了……

装完后给的nginx配置示例:

Deploying a web application: an example

Suppose you have a web application in /somewhere. Add a server block
to your Nginx configuration file, set its root to /somewhere/public, and set
'passenger_enabled on', like this:

   server {
      listen 80;
      server_name www.yourhost.com;
      root /somewhere/public;   #

为方便以后使用,这里做个软链接:

ln -sv /usr/local/nginx/sbin/nginx /usr/bin/nginx

设置nginx开机启动:
在/etc/rc.local中加入nginx

6.安装nodejs

deploy用户:

mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1 #--strip-components=1 这个参数表示解压在当前目录下,不加这个参数会在当前目录新建一个node-latest文件夹,解压在里面
./configure --prefix=/usr/local/node
sudo make install

做软链接,否则ruby on rails找不到node,会报javascript runtime的错误

root@ubuntu-template-ror:~# sudo ln -s /usr/local/node/bin/node /usr/bin/node

如果需要安装npm:

sudo curl -L https://npmjs.org/install.sh | sh

7.测试安装是否成功
创建测试项目

cd /home/deploy
rails new project1
会在这里卡一段时间 : run  bundle install  
cd project1
gem install minitest -v '4.7.5'
gem install json -v '1.8.1'
bundle install

在nginx.conf里面加入server段:

server {
            listen 80;
            server_name 192.168.0.12;
            root /home/deploy/project1/public;   #

线上服务器最好在http端加入:

    passenger_show_version_in_header off;
    passenger_friendly_error_pages off;
    #passenger_max_pool_size 8;   #这个是调整passenger进程的最大数量,在deploy 用户下可以通过 rvmsudo passenger-status 查看进程情况

8.常用命令
查看rvm占用硬盘空间

deploy@xt-air:~$ rvm disk-usage all

9.卸载rvm

rvm implode
rm -rf  /home/deploy/.rvm

10.升级更新rvm

rvm get stable