出于为xiong节省成本的考虑,近日在尝试将soopie移到Dreamhost的虚拟主机当中。任务艰巨主要体现在以下两点:
- 转移的数据量大,总共为9G,而且有无数的小文件(图片,视频,文本。。。)。在soopie的服务器和Dreamhost服务器以及Dreamhost不同帐号之间倒腾数据是一件挺麻烦的事情。先是打包压缩,然后ftp传送,再然后解压,这些步骤都非常的费时。通过这个过程,了解了两件事情:
- 需要在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 /myrailsappRewriteRule ^$ 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.htmlErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
ENV['GEM_PATH'] = '/home/你的用户名/.gems:/usr/lib/ruby/gems/1.8'
OK,做完这些步骤之后,在访问这个应用之前,唯一可以做的是祈祷。可能遇到的情况有很多,最常见的包括以下几种: