胡思乱想 2.0 (ttthree)
2007-04-21
  将soopie向Dreamhost迁移

出于为xiong节省成本的考虑,近日在尝试将soopie移到Dreamhost的虚拟主机当中。任务艰巨主要体现在以下两点:

- 转移的数据量大,总共为9G,而且有无数的小文件(图片,视频,文本。。。)。在soopie的服务器和Dreamhost服务器以及Dreamhost不同帐号之间倒腾数据是一件挺麻烦的事情。先是打包压缩,然后ftp传送,再然后解压,这些步骤都非常的费时。通过这个过程,了解了两件事情:

  1. lftp后台传送很方便,调用put命令以后,任务就在后台运行了,这时候退出lftp,任务进程会一直继续直到传送完成;Dreamhost上只有最基本的ftp命令,后台运行就比较困难:先运行nohup ftp;然后所有的输出就被定向到nohup.out了;输入open <主机名>,回车;输入用户名;输入密码;输入put或者get命令;Ctrl+Z把进程挂起;bg命令把ftp进程放到后台。整个操作过程中没有回显,一不小心输错了就只好从头再来-_-
  2. soopie到目前为止9G的用户数据中,有7.3G是用户通过手机存在我们的网络硬盘中的文件,而不是分享的照片/视频,由此可见免费网络存储仍然是中国网民的最爱,大概为智能手机用户提供好用的网络硬盘客户端还是一件值得做的事情。

- 需要在Dreamhost上安装自己定制的Ruby解释器和各种扩展(soopie是基于Ruby on Rails的)。这是因为国内有不少地方的移动网关有“问题”,会偷偷修改上传数据中的boundary,导致soopie收到手机上传的HTTP包无法被Ruby CGI正确处理,需要修改Ruby解释器中一个cgi.rb来应付这种情况。这个过程其实弄明白了以后并不算太复杂,但是问题在于网络上可以检索到的任何一篇相关文章都不是完全可以拿来直接照搬成功的,需要到处借鉴,不停尝试。。。摘记于此:

1.  Freeze soopie的Rails版本到$RAILS_ROOT/vendor/rails目录,并且把soopie用到的各种gems也都freeze到$RAILS_ROOT/lib/下。这是为了保证soopie用到的Rails不会受到Dreamhost安装的新版本Rails 1.2影响。具体的做法是在$RAILS_ROOT运行rake freeze_gems,然后下载这个gems.rake文件放到$RAILS_ROOT/lib/tasks下面,并修改其中的第四行,填入soopie用到的各个gems名字,运行rake freeze_other_gems. (之后就可以把整个Rails应用程序打包了) 

参考: 这里

2.  在Dreamhost上安装Ruby, FastCGI, RubyGems, 若干必要的gems ,步骤如下:(参考1参考2)

a. 下载Ruby源代码 ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.4.tar.gz 解压,下载rubygc.patch放到解压后的目录

./configure --prefix=$HOME/local
patch gc.c rubygc.patch
make
make install

安装成功后,在$HOME/local/bin下面会有ruby的可执行程序,

export PATH=$HOME/local/bin:$PATH

which ruby 会输出 $HOME/local/bin/ruby, ok,这样就拥有自己的Ruby解释器了

b. 下载FastCGI源代码 http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz 解压

./configure --prefix=$HOME/local
make
make install

c. 下载RubyGems源代码 http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz 解压

ruby setup

安装成功后运行which gem会输出$HOME/local/bin/gem,ok,这个Ruby扩展管理工具也属于我们自己了

d. 安装几个必须的gems

mkdir $HOME/.gems
export GEM_HOME=$HOME/.gems
export GEM_PATH=$GEM_HOME:/usr/lib/ruby/gems/1.8
gem install mysql
gem install fcgi
gem install rails --include-dependencies

3.  正确设置环境变量

把用户根目录下的.bash_profile修改为:

export PATH=$HOME/local/bin:$HOME/.gems/bin:$PATH
export GEM_HOME=$HOME/.gems
export GEM_PATH=$GEM_HOME:/usr/lib/ruby/gems/1.8

4.  数据上传到Dreamhost服务器并解压之后,在Dreamhost的控制面板中加一个新的子域名,并打开FastCGI支持,默认目录设为/home/用户名/域名/public/

5.  修改一系列文件内容、文件目录权限 (参考,尤其当你遇到这样的提示时: Application Error - Rails application failed to start properly)

  • 进入Dreamhost主机目录$HOME/<第4步添加的域名>/

chmod 755 public
chmod 755 public/dispatch.*
chmod -R 766 log
chmod -R 766 tmp

  • 添加一个.htaccess文件到public目录下,内容为:

# General Apache options
AddHandler fastcgi-script .fcgi
AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI

# If you don't want Rails to look in certain directories,
# use the following rewrite rules so that Apache won't rewrite certain requests
#
# Example:
# RewriteCond %{REQUEST_URI} ^/notrails.*
# RewriteRule .* - [L]

# Redirect all requests not available on the filesystem to Rails
# By default the cgi dispatcher is used which is very slow
#
# For better performance replace the dispatcher with the fastcgi one
#
# Example:
# RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
RewriteEngine On

# If your Rails application is accessed via an Alias directive,
# then you MUST also set the RewriteBase in this htaccess file.
#
# Example:
# Alias /myrailsapp /path/to/myrailsapp/public
# RewriteBase /myrailsapp

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

# In case Rails experiences terminal errors
# Instead of displaying this message you can supply a file here which will be rendered instead
#
# Example:
# ErrorDocument 500 /500.html

ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"

ENV['GEM_PATH'] = '/home/你的用户名/.gems:/usr/lib/ruby/gems/1.8'

OK,做完这些步骤之后,在访问这个应用之前,唯一可以做的是祈祷。可能遇到的情况有很多,最常见的包括以下几种:

 
Comments: 发表评论

Links to this post:

创建链接



<< Home

姓名: Jie TONG
位置: 北京, 北京, CN
Links
My MSN Space
soopie
uupan

Archives
2005-11 / 2006-03 / 2006-04 / 2006-05 / 2006-06 / 2006-07 / 2006-08 / 2006-09 / 2006-10 / 2006-11 / 2007-01 / 2007-02 / 2007-03 / 2007-04 /


Powered by Blogger