webistrano的安装配置及nginx整合

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

一、环境安装

以ubuntu下的环境安装

$ sudo apt-get install ruby1.9.3  git  mysql-client mysql-server libmysqld-dev

centos下可以使用

# yum -y install ruby  mysql mysql-server mysql-devel  git

注:在安装完ruby后还需要安装gem工具,如果想要通过源码编译的方式安装ruby和gem

二、Webistrano的依赖包的安装

1、下载Webistrano的安装包

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

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

2、安装bundler

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

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

3、修改Gemfile文件

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

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

# cd /opt/webistrano
# cat Gemfile
source "http://rubygems.org"
gem 'bundler', "~>1.0.10"
gem "rails", "2.3.11"
gem "mysql"
gem "erubis"
gem "rake"
gem "syntax", "1.0.0"
gem "capistrano", "2.6.0"
gem "open4", "0.9.3"
gem "exception_notification", "2.3.3.0"
group :test do
  gem "mocha", "0.9.8"
end

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

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

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

4、bundler依赖软件包安装

# sudo bundle install
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this
application for all non-root users on this machine.
Fetching gem metadata from https://ruby.taobao.org/..........
Fetching additional metadata from https://ruby.taobao.org/..
Resolving dependencies...
Installing rake 0.8.7
Installing activesupport 2.3.11
Installing rack 1.1.6
Installing actionpack 2.3.11
Installing actionmailer 2.3.11
Installing activerecord 2.3.11
Installing activeresource 2.3.11
Using bundler 1.6.3
Installing highline 1.6.21
Installing net-ssh 2.9.1
Installing net-scp 1.2.1
Installing net-sftp 2.1.2
Installing net-ssh-gateway 1.2.0
Installing capistrano 2.6.0
Installing erubis 2.7.0
Installing exception_notification 2.3.3.0
Installing mocha 0.9.8
Installing mysql 2.9.1
WARNING: open4-0.9.3 has an invalid nil value for @cert_chain
Installing open4 0.9.3
Installing rails 2.3.11
Installing syntax 1.0.0
Your bundle is complete!
It was installed into ./vendor/bundler

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

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

三、Webistrano的启动

1、复制配置文件

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

2、修改database.yml

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

production:
  adapter: mysql
  database: webistrano_production
  username: root
  password: asdf
  socket: /var/run/mysqld/mysqld.sock

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

# RAILS_ENV=production rake db:create
/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.
(in /opt/webistrano)

也可以连接mysql进行创建

mysql> create database webistrano_production character set utf8;

4、sql数据文件导入

导入Webistrano的数据库

# RAILS_ENV=production rake db:migrate
/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.
(in /opt/webistrano)
==  CreateProjects: migrating =================================================
-- create_table(:projects)
   -> 0.3179s
==  CreateProjects: migrated (0.3183s) ========================================
部分省略………………………………
==  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包时出错

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

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

# yum install mysql mysql-server mysql-devel

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

# 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版 。方法为:

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

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

错误3:安装mongrel报错

# sudo gem install mongrel
Fetching: gem_plugin-0.2.3.gem (100%)
Fetching: daemons-1.1.9.gem (100%)
Fetching: fastthread-1.0.7.gem (100%)
Building native extensions.  This could take a while...
Fetching: cgi_multipart_eof_fix-2.5.0.gem (100%)
Fetching: mongrel-1.1.5.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mongrel:
        ERROR: Failed to build gem native extension.
…………………………………………………………………………省略部分内容
http11.c: In function ‘header_done’:
http11.c:172:13: error: ‘struct RString’ has no member named ‘ptr’
http11.c:172:13: error: ‘struct RString’ has no member named ‘ptr’
http11.c:172:13: error: ‘struct RString’ has no member named ‘ptr’
http11.c:174:89: error: ‘struct RString’ has no member named ‘ptr’
http11.c:176:52: error: ‘struct RString’ has no member named ‘ptr’
http11.c:177:26: error: ‘struct RString’ has no member named ‘len’
http11.c: In function ‘HttpParser_execute’:
http11.c:298:23: error: ‘struct RString’ has no member named ‘ptr’
http11.c:299:23: error: ‘struct RString’ has no member named ‘len’
http11.c:307:5: warning: format not a string literal and no format arguments [-Wformat-security]
make: *** [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 文件解决,找到如下行,进行修改

if(@deployment.task && !@deployment.new_record?)
修改为:
if(!@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

# mongrel_rails start -e production -d -p 3000

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

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

配置完成后reload生效。

方法2:mod_passenger+nginx

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

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

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

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

按上面的提示,按1即可,后面还有类似的其他提示,可以根据自已的实际需求选择或填写 。

安装完成后,其会在nginx.conf文件中的http块部分增加如下两行内容:

http {
      ...
      passenger_root /var/lib/gems/1.9.1/gems/passenger-4.0.45;
      passenger_ruby /usr/bin/ruby1.9.1;
      ...
  }

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

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

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

参考页面:kasia Gogolek 's blog

还没有评论,快来抢沙发!

发表评论

  • 😉
  • 😐
  • 😡
  • 😈
  • 🙂
  • 😯
  • 🙁
  • 🙄
  • 😛
  • 😳
  • 😮
  • emoji-mrgree
  • 😆
  • 💡
  • 😀
  • 👿
  • 😥
  • 😎
  • ➡
  • 😕
  • ❓
  • ❗
  • 68 queries in 0.443 seconds