安装ruby on rails
第1步:更新
$ sudo apt-get update
第2步:安装必要的三方库
$ sudo apt-get install -y wget vim build-essential openssl libreadline6 libreadline6-dev libmysqlclient-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev libxslt-dev libcurl4-openssl-dev autoconf automake libtool imagemagick libmagickwand-dev libpcre3-dev nodejs libpq-dev
第3步:安装rvm
$ curl -L https://get.rvm.io | bash -s stable
$ echo'[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"' >>~/.bashrc
$ source ~/.bashrc
$ source /etc/profile.d/rvm.sh
$ rvm -v
第4步:安装Ruby
$ rvm pkg install readline openssl
$ rvm install 2.0.0
$ rvm use 2.0.0 --default
$ ruby -v
国内服务器推荐替换rubygems到淘宝源:
$ gem sources --remove https://rubygems.org/
$ gem sources -a http://ruby.taobao.org/
$ gem sources -l
第5步:安装Rails
$ gem install rails
$ rails -v
安装Passenger
用gem安装passenger
$ gem install passenger
安装Nginx
如果之前安装了Nginx需要先卸载掉
$ sudo rm -f -R /usr/local/nginx && rm -f /usr/local/sbin/nginx
用Passenger Nginx module installer安装Nginx
$ passenger-install-nginx-module
验证是否成功安装,例如
$ sudo passenger-config validate-install
* Checking whether this Phusion Passenger install is in PATH... ✓
* Checking whether there are no other Phusion Passenger installations... ✓
使用Nginx
启动Nginx
$ sudo /opt/nginx/sbin/nginx
停止Nginx
$ sudo kill $(cat /opt/nginx/logs/nginx.pid)
查看Nginx和Passenger进程
$ rvmsudo passenger-memory-stats
Version: 5.0.8
Date : 2015-05-28 08:46:20 +0200
---------- Nginx processes ----------
PID PPID VMSize Private Name
-------------------------------------
12443 4814 60.8 MB 0.2 MB nginx: master process /usr/sbin/nginx
12538 12443 64.9 MB 5.0 MB nginx: worker process
Processes: 3
Total private dirty RSS: 5.56 MB
----- Passenger processes ------
PID VMSize Private Name
--------------------------------
12517 83.2 MB 0.6 MB PassengerAgent watchdog
12520 266.0 MB 3.4 MB PassengerAgent server
12531 149.5 MB 1.4 MB PassengerAgent logger
如果Nginx已经启动,应该可以看见这些基本进程,如果没有,那么可能在安装配置过程存在问题。
在Nginx上部署Rails
编辑nginx.conf
$ sudo vim /opt/nginx/conf/nginx.conf
做如下配置
server {
listen 80;
server_name localhost;
root /Users/wanglei/Documents/myrails/blog/public;
passenger_enabled on;
}
listen端口,server_name域名或ip地址, root Rails项目的public目录。
在浏览器中输入localhost即可打开项目。
如无法打开index,可以在rails routes.rb中设置路由
root 'welcome#index'
get 'index' => "welcome#index"
如上两种方法可试。