Capistrano是一种在多台服务器上运行脚本的开源工具,它主要用于部署web应用。它自动完成多台服务器上新版本的同步更新,包括数据库的改变。Capistrano最初由Jamis Buck用Ruby开发,并用RubyGems部署渠道部署。而Webistrano是Capistrano的web UI 增强,可以通过Webistrano 提供的web界面配置和操作完成 Capistrano在命令行的配置和操作。不过这里需要声明一点,Webistrano不是单独存在的,其底层还是需要调用Capistrano的。

一、环境安装

1以ubuntu下的环境安装 
2$ sudo apt-get install ruby1.9.3  git  mysql-client mysql-server libmysqld-dev
3
4centos下可以使用
5# yum -y install ruby  mysql mysql-server mysql-devel  git

注:在安装完ruby后还需要安装gem工具,如果想要通过源码编译的方式安装ruby和gem ,可以参看我的另一篇日志内容 中的ruby环境和gem安装及ruby源更改部分 。

二、Webistrano的依赖包的安装

1、下载Webistrano的安装包

这里以git方式获取为例,一些老的linux发行版本的源里可能没有git ,也可以去github下载zip包

1# sudo git clone https://github.com/peritor/webistrano.git

2、安装bundler

bundler是一个管理维护项目的软件包的工具,其和gem类似,不过gem更侧重于管理每一个软件包。便于理解,可以这样理解,gem管理的整机的环境包,而bundler管理的项目上下的所有的软件包。如在php、python中,很多第三方类库,我们可以全局安装,也可以放到调用该类库的目录下,通过import或include进行调用。而gem就像相当于全局进行安装,bundler就相当于将其放到调用该类库的目录下。

1# sudo gem install bundler
2Fetching: bundler-1.6.3.gem (100%)
3Successfully installed bundler-1.6.3
41 gem installed
5Installing ri documentation for bundler-1.6.3...
6Installing RDoc documentation for bundler-1.6.3...
7ubuntu@vn-0-39:/opt/webistrano$ gem list
8*** LOCAL GEMS ***

3、修改Gemfile文件

Gemfile文件列出了Webistrano安装过程中所需的包文件及其版本号。而bundler是通过读取文件文件进行依赖软件安装的。

从github上看到webistrano自2011年就已不再更新,所以这里很多软件都很老。

 1# cd /opt/webistrano
 2# cat Gemfile
 3source "http://rubygems.org"
 4gem 'bundler', "~>1.0.10"
 5gem "rails", "2.3.11"
 6gem "mysql"
 7gem "erubis"
 8gem "rake"
 9gem "syntax", "1.0.0"
10gem "capistrano", "2.6.0"
11gem "open4", "0.9.3"
12gem "exception_notification", "2.3.3.0"
13group :test do
14  gem "mocha", "0.9.8"
15end

这里主要改两项,bundler的版本和rake的版本,修改后的内容如下:

 1# cat Gemfile
 2#这里将源改成了taobao,加快访问速度
 3source 'http://ruby.taobao.org/'
 4#这里bundler改成了我们上面gem安装的目前的版本
 5gem 'bundler', "1.6.3"
 6gem "rails", "2.3.11"
 7gem "mysql"
 8gem "erubis"
 9gem "rake", "0.8.7"
10gem "syntax", "1.0.0"
11gem "net-ssh", "2.7.0" //该行为新加,否者有报错,这个在后面会提到
12gem "capistrano", "2.6.0"
13gem "open4", "0.9.3"
14gem "exception_notification", "2.3.3.0"
15group :test do
16  gem "mocha", "0.9.8"
17end

rake版本需要特别注意下,一定要更改成0.8.7 。不然会报错,报错内容后面会单独给出。

4、bundler依赖软件包安装

 1# sudo bundle install
 2Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this
 3application for all non-root users on this machine.
 4Fetching gem metadata from https://ruby.taobao.org/..........
 5Fetching additional metadata from https://ruby.taobao.org/..
 6Resolving dependencies...
 7Installing rake 0.8.7
 8Installing activesupport 2.3.11
 9Installing rack 1.1.6
10Installing actionpack 2.3.11
11Installing actionmailer 2.3.11
12Installing activerecord 2.3.11
13Installing activeresource 2.3.11
14Using bundler 1.6.3
15Installing highline 1.6.21
16Installing net-ssh 2.9.1
17Installing net-scp 1.2.1
18Installing net-sftp 2.1.2
19Installing net-ssh-gateway 1.2.0
20Installing capistrano 2.6.0
21Installing erubis 2.7.0
22Installing exception_notification 2.3.3.0
23Installing mocha 0.9.8
24Installing mysql 2.9.1
25WARNING: open4-0.9.3 has an invalid nil value for @cert_chain
26Installing open4 0.9.3
27Installing rails 2.3.11
28Installing syntax 1.0.0
29Your bundle is complete!
30It was installed into ./vendor/bundler

这里,可以看出通过bundler进行项目软件安装时非常方便,比我们使用gem insatll rake –version 0.8.7这种方式一个个的去安装方便多了,而且使用gem进行安装会在全局环境进行安装,打乱很多依赖关系。而bundler只会在本项目目录下进行安装,具体可以通过下面的方式进行查看验证:

 1# ll
 2total 88
 3drwxr-xr-x 22 ubuntu ubuntu 4096 Jun 24 18:07 ./
 4drwxr-xr-x  7 ubuntu ubuntu 4096 Jun 24 18:05 ../
 5drwxr-xr-x  4 ubuntu ubuntu 4096 Jun 24 18:06 actionmailer-2.3.11/
 6drwxr-xr-x  4 ubuntu ubuntu 4096 Jun 24 18:06 actionpack-2.3.11/
 7drwxr-xr-x  5 ubuntu ubuntu 4096 Jun 24 18:06 activerecord-2.3.11/
 8drwxr-xr-x  4 ubuntu ubuntu 4096 Jun 24 18:06 activeresource-2.3.11/
 9drwxr-xr-x  3 ubuntu ubuntu 4096 Jun 24 18:05 activesupport-2.3.11/
10drwxr-xr-x  5 ubuntu ubuntu 4096 Jun 24 18:07 capistrano-2.6.0/
11drwxr-xr-x 10 ubuntu ubuntu 4096 Jun 24 18:07 erubis-2.7.0/
12drwxr-xr-x  4 ubuntu ubuntu 4096 Jun 24 18:07 exception_notification-2.3.3.0/
13drwxr-xr-x  7 ubuntu ubuntu 4096 Jun 24 18:06 highline-1.6.21/
14drwxr-xr-x  5 ubuntu ubuntu 4096 Jun 24 18:07 mocha-0.9.8/
15drwxr-xr-x  7 ubuntu ubuntu 4096 Jun 24 18:07 mysql-2.9.1/
16drwxr-xr-x  4 ubuntu ubuntu 4096 Jun 24 18:06 net-scp-1.2.1/
17drwxr-xr-x  4 ubuntu ubuntu 4096 Jun 24 18:06 net-sftp-2.1.2/
18drwxr-xr-x  5 ubuntu ubuntu 4096 Jun 24 18:06 net-ssh-2.9.1/
19drwxr-xr-x  4 ubuntu ubuntu 4096 Jun 24 18:06 net-ssh-gateway-1.2.0/
20drwxr-xr-x  5 ubuntu ubuntu 4096 Jun 24 18:07 open4-0.9.3/
21drwxr-xr-x  7 ubuntu ubuntu 4096 Jun 24 18:05 rack-1.1.6/
22drwxr-xr-x 11 ubuntu ubuntu 4096 Jun 24 18:07 rails-2.3.11/
23drwxr-xr-x  6 ubuntu ubuntu 4096 Jun 24 18:05 rake-0.8.7/
24drwxr-xr-x  5 ubuntu ubuntu 4096 Jun 24 18:07 syntax-1.0.0/
25# pwd
26/opt/webistrano/vendor/bundler/ruby/1.9.1/gems

三、Webistrano的启动

1、复制配置文件

1# cd /opt/webistrano/config
2# sudo cp database.yml.sample database.yml
3# sudo cp webistrano_config.rb.sample webistrano_config.rb

2、修改database.yml

编辑该文件,修改为当前mysql的具体信息,如:

1production:
2  adapter: mysql
3  database: webistrano_production
4  username: root
5  password: asdf
6  socket: /var/run/mysqld/mysqld.sock

3、数据库的创建

这里有两种方法创建,可以通过rails的db:create进行创建

1# RAILS_ENV=production rake db:create
2/var/lib/gems/1.9.1/gems/rake-0.8.7/lib/rake/alt_system.rb:32: Use RbConfig instead of obsolete and deprecated Config.
3(in /opt/webistrano)

也可以连接mysql进行创建

1mysql> create database webistrano_production character set utf8;

4、sql数据文件导入

导入Webistrano的数据库

1# RAILS_ENV=production rake db:migrate
2/var/lib/gems/1.9.1/gems/rake-0.8.7/lib/rake/alt_system.rb:32: Use RbConfig instead of obsolete and deprecated Config.
3(in /opt/webistrano)
4==  CreateProjects: migrating =================================================
5-- create_table(:projects)
6   -> 0.3179s
7==  CreateProjects: migrated (0.3183s) ========================================
8部分省略………………………………
9==  AddIndices: migrated (1.3324s) ============================================

5、安装mongrel

安装方法:# sudo gem install mongrel --pre

注意此处后面的–pre一定要加,不然会报错,这个在后面也会提及。

6、启动webistrano

使用mongrel_rails启动production环境下的webistrano, 命令是:# mongrel_rails start -e production -d -p 3000

注意出于安全考虑这里启动的用户尽量不要用root ,而只用普通用启启动。因为使用root启动的程序,一旦出同漏洞上传webshell时,其具有的就是root权限。

启动后,通过http://IP:3000访问,默认用户密码都是admin 。我创建了一个361way的projects ,展示下成果,如下:

webistrano

四、安装过程中可能遇到的错误

安装过程中遇到的错误大部分是使用gem方式安装过程中遇到的,所以这里再强调下推荐使用bundler安装Webistrano依赖的软件包。

错误1:安装mysql包时出错

 1[root@361way webistrano]# gem install mysql erubis rake
 2Fetching: mysql-2.9.1.gem (100%)
 3Building native extensions.  This could take a while...
 4ERROR:  Error installing mysql:
 5        ERROR: Failed to build gem native extension.
 6    /usr/local/bin/ruby extconf.rb
 7checking for mysql_ssl_set()... *** extconf.rb failed ***
 8Could not create Makefile due to some reason, probably lack of
 9necessary libraries and/or headers.  Check the mkmf.log file for more
10details.  You may need configuration options.

解决方法:安装mysql环境包再进行gem安装

1# yum install mysql mysql-server mysql-devel

如果mysql使用的源码包编译安装到其他路径,可以通过下面的方法进进行指向:

1# gem install mysql -- --with-mysql-config=/usr/local/mysql-5.1.56-osx10.6-x86/bin/mysql_config

错误2: rake版本报错

错误内容:ERROR: 'rake/rdoctask' is obsolete and no longer supported. Use 'rdoc/task' (available in RDoc 2.4.2+) instead.

解决方法:降低rake的版本,使用0.8.7版 。方法为:

1# gem list   查看当前gem安装的所有软件包
2# gem uninstall  rake -v 10.0.1
3//不加后面的版本号时,是删除所有的版本
4# gem install  rake -v 0.8.7

这里如果使用最上面修改Gemfile并使用bundler进行的安装的方法,可以避免该问题。

错误3:安装mongrel报错

 1# sudo gem install mongrel
 2Fetching: gem_plugin-0.2.3.gem (100%)
 3Fetching: daemons-1.1.9.gem (100%)
 4Fetching: fastthread-1.0.7.gem (100%)
 5Building native extensions.  This could take a while...
 6Fetching: cgi_multipart_eof_fix-2.5.0.gem (100%)
 7Fetching: mongrel-1.1.5.gem (100%)
 8Building native extensions.  This could take a while...
 9ERROR:  Error installing mongrel:
10        ERROR: Failed to build gem native extension.
11…………………………………………………………………………省略部分内容
12http11.c: In function ‘header_done’:
13http11.c:172:13: error: ‘struct RString’ has no member named ‘ptr’
14http11.c:172:13: error: ‘struct RString’ has no member named ‘ptr’
15http11.c:172:13: error: ‘struct RString’ has no member named ‘ptr’
16http11.c:174:89: error: ‘struct RString’ has no member named ‘ptr’
17http11.c:176:52: error: ‘struct RString’ has no member named ‘ptr’
18http11.c:177:26: error: ‘struct RString’ has no member named ‘len’
19http11.c: In function ‘HttpParser_execute’:
20http11.c:298:23: error: ‘struct RString’ has no member named ‘ptr’
21http11.c:299:23: error: ‘struct RString’ has no member named ‘len’
22http11.c:307:5: warning: format not a string literal and no format arguments [-Wformat-security]
23make: *** [http11.o] Error 1

解决该问题的方法网上有两种,一种是修改mongrel的源文件,再进行ruby编译,该方法比较烦锁,不推荐,有兴趣的,可以参看http://computerplumber.com/2009/08/installing-mongrel-with-ruby19/ ;另一种是直接执行一条命令就行了sudo gem install mongrel --pre

错误4:web界面创建stage时出错

webistrano-wrong

该问题是由webistrano的一个bug引起的,可以通过修改lib/webistrano/deployer.rb 文件解决,找到如下行,进行修改

1if(@deployment.task && !@deployment.new_record?)
2修改为:
3if(!@deployment[:task].nil? && !@deployment.new_record?)

修改完成后,需要重启webistrano 生效。

错误五:Net::SSH::AuthenticationFailed

在web界面上新建好project ,并选择stage执行时deploy:setup等任意操作,都会报错 connection failed for:(Net::SSH::AuthenticationFailed: Authentication failed for user , 出现该错误的原因是由于在安装capistrano时,会自动安装上net-ssh 2.9.1这类版本的包,而这些执行时会报错,只能选择降级为net-ssh 2.7.0,具体可以参看github上的net-ssh项目的issues部分

五、webistrano与nginx的整合

webistrano与nginx的整合方式有两种:一种是通过反向代理的方式整合,一种是通过passenger+nginx的方式整合。

方法1:反向代理

以第三部最后的方式启动webistrano

1# mongrel_rails start -e production -d -p 3000

接着安装nginx,并增加如下配置:

 1server {
 2        listen        80;
 3        server_name   webistrano.361way.com;
 4        root    /opt/webistrano/public;  #注意这里一定要指向Rails应用的public目录
 5        index   index.html index.htm;
 6        location / {
 7            proxy_pass http://127.0.0.1:3000;
 8            proxy_redirect off;
 9            proxy_set_header Host $host;
10            proxy_set_header X-Real-IP $remote_addr;
11            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
12        }
13    }

配置完成后reload生效。

方法2:mod_passenger+nginx

该方法也是官方wiki 问答中提供到的方法,具提操作方式如下:

1# sudo apt-get insatll build-essential libcurl4-openssl-dev or libcurl4-gnutls-dev
2// 安装paaenger
3# sudo gem install  passenger

passenger集成的有passenger-install-apache2-module 和 passenger-install-nginx-module 模块,这里以nginx的为例,直接运行passenger-install-nginx-module ,该工具会自动从网络上下载最新稳定版的nginx进行交互式安装 ,默认nginx会安装在/opt/nginx目录下,在交互过程中会出现进示,也可以更改为其他路径。

 1# /usr/local/bin/passenger-install-nginx-module
 2Welcome to the Phusion Passenger Nginx module installer, v4.0.45.
 3This installer will guide you through the entire installation process. It
 4shouldn't take more than 5 minutes in total.
 5Here's what you can expect from the installation process:
 6 1. This installer will compile and install Nginx with Passenger support.
 7 2. You'll learn how to configure Passenger in Nginx.
 8 3. You'll learn how to deploy a Ruby on Rails application.
 9Don't worry if anything goes wrong. This installer will advise you on how to
10solve any problems.
11Press Enter to continue, or Ctrl-C to abort.

按上面的提示,按1即可,后面还有类似的其他提示,可以根据自已的实际需求选择或填写 。安装完成后,其会在nginx.conf文件中的http块部分增加如下两行内容:

1http {
2      ...
3      passenger_root /var/lib/gems/1.9.1/gems/passenger-4.0.45;
4      passenger_ruby /usr/bin/ruby1.9.1;
5      ...
6  }

如果要配置webistrano,只需要在server中增加如下内容

1server {
2        listen       80;
3       server_name webistrano.361way.com;
4        root /opt/webistrano/public/;   # <--- be sure to point to 'public'!
5        passenger_enabled on;
6   }

配置完后,启动nginx,就可以通过http://domain 访问webistrano了。

参考页面:kasia Gogolek ‘s blog