王筝的博客
ruby学习

 

$ unicorn_rails -h
Usage: unicorn_rails [ruby options] [unicorn_rails options] [rackup config file]
Ruby options:
  -e, --eval LINE          evaluate a LINE of code
  -d, --debug              set debugging flags (set $DEBUG to true)
  -w, --warn               turn warnings on for your script
  -I, --include PATH       specify $LOAD_PATH (may be used more than once)
  -r, --require LIBRARY    require the library, before executing your script
unicorn_rails options:
  -o, --host HOST          listen on HOST (default: 0.0.0.0)
  -p, --port PORT          use PORT (default: 8080)
  -E, --env RAILS_ENV      use RAILS_ENV for defaults (default: development)
  -D, --daemonize          run daemonized in the background
  -l {HOST:PORT|PATH},     listen on HOST:PORT or PATH
      --listen             this may be specified multiple times
                           (default: 0.0.0.0:8080)
  -c, --config-file FILE   Unicorn-specific config file

      --path PATH          Runs Rails app mounted at a specific path.
                           (default: /)
Common options:
  -h, --help               Show this message
  -v, --version            Show version

 

$ unicorn -h
Usage: unicorn [ruby options] [unicorn options] [rackup config file]
Ruby options:
  -e, --eval LINE          evaluate a LINE of code
  -d, --debug              set debugging flags (set $DEBUG to true)
  -w, --warn               turn warnings on for your script
  -I, --include PATH       specify $LOAD_PATH (may be used more than once)
  -r, --require LIBRARY    require the library, before executing your script
unicorn options:
  -o, --host HOST          listen on HOST (default: 0.0.0.0)
  -p, --port PORT          use PORT (default: 8080)
  -E, --env RACK_ENV       use RACK_ENV for defaults (default: development)
  -N                       do not load middleware implied by RACK_ENV
      --no-default-middleware
  -D, --daemonize          run daemonized in the background

  -s, --server SERVER      this flag only exists for compatibility
  -l {HOST:PORT|PATH},     listen on HOST:PORT or PATH
      --listen             this may be specified multiple times
                           (default: 0.0.0.0:8080)
  -c, --config-file FILE   Unicorn-specific config file
Common options:
  -h, --help               Show this message
  -v, --version            Show version

 

config/unicorn.conf.rb

# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
 # documentation.
 
-rails_env = ENV['RAILS_ENV'] || 'staging'
+rails_env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'staging'
Add a comment to this line
 # Use at least one worker per core if you're on a dedicated server,
 # more will usually help for _short_ waits on databases/caches.
-worker_processes rails_env == "production" ? 4 : 1
+worker_processes (rails_env == "production" ? 4 : 1)
 
 # Since Unicorn is never exposed to outside clients, it does not need to
 # run on the standard HTTP port (80), there is no reason to start Unicorn

 

部署的时候发现需要输入服务器密码,不能直接登录

DEBUG [ab5be9b4] Running /usr/bin/env [ -d $HOME/.rbenv/versions/2.3.0 ] on 10.103.xx.xx
DEBUG [05871a47] Running /usr/bin/env [ -d $HOME/.rbenv/versions/2.3.0 ] on 10.103.xx.xx
DEBUG [dd74890c] Running /usr/bin/env [ -d $HOME/.rbenv/versions/2.3.0 ] on 10.103.xx.xx
DEBUG [ab5be9b4] Command: [ -d $HOME/.rbenv/versions/2.3.0 ]
DEBUG [05871a47] Command: [ -d $HOME/.rbenv/versions/2.3.0 ]
DEBUG [dd74890c] Command: [ -d $HOME/.rbenv/versions/2.3.0 ]
Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text.
root@10.103.xx.xx's password:Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text.
root@10.103.xx.xx's password:Text will be echoed in the clear. Please install the HighLine or Termios libraries to suppress echoed text.
root@10.103.xx.xx's password:

 

 

3.0+版本config/deploy.rb 取消了user的写法

set :user, 'webuser'

需要用这样的写法

set :ssh_options, { user: 'webuser' }

 

https://github.com/capistrano/capistrano/blob/621985511529893d972e79452ad63eb54430fb4c/lib/capistrano/templates/stage.rb.erb

或者在deploy/production.rb里设置ssh用户名

# server 'example.com', user: 'deploy', roles: %w{app db web}, my_property: :my_value
# server 'example.com', user: 'deploy', roles: %w{app web}, other_property: :other_value
# server 'db.example.com', user: 'deploy', roles: %w{db}

 

15:13 [webuser@a02.test.stat]$ bundle exec cap production deploy:check --trace
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
cap aborted!
ArgumentError: wrong number of arguments (given 4, expected 2..3)

https://github.com/capistrano/capistrano/blob/v3.4.0/lib/capistrano/templates/stage.rb.erb

# role :app, %w{deploy@example.com}, my_property: :my_value
# role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value
# role :db,  %w{deploy@example.com}

把/config/deploy/production.rb中的服务器列表改为数组的形式

 

-role :web, "10.103.xx.xx", "10.103.xx.xx", "10.103.xx.xx"                          # Your HTTP server, Apache/etc
-role :app, "10.103.xx.xx", "10.103.xx.xx", "10.103.xx.xx"                         # This may be the same as your `Web` server
+role :web, ["10.103.xx.xx", "10.103.xx.xx", "10.103.xx.xx"]                          # Your HTTP server, Apache/etc
+role :app, ["10.103.xx.xx", "10.103.xx.xx", "10.103.xx.xx"]                         # This may be the same as your `Web` server

 

https://github.com/capistrano/capistrano

https://github.com/capistrano/capistrano/wiki

https://github.com/capistrano/capistrano/wiki/2.x-from-the-beginning

(1)使用capistrano2,要在Gemfile里指定版本

  gem 'capistrano', git: 'https://github.com/capistrano/capistrano', branch: :'legacy-v2'
  gem 'capistrano-rbenv', git: 'https://github.com/yyuu/capistrano-rbenv.git', branch: :develop

如果使用最新版本的就不需要指定版本了~

(2)capistrano2生成Capfile用的命令是

$ bundle exec capify .

capistrano3生成Capfile用的命令是

$ bundle exec cap install

 

今天部署了新服务器发现cache目录没有权限,

App 26772 stderr: Started GET "/investors" for 103.238.226.130 at 2015-08-25 11:25:47 +0800
App 26772 stderr: Processing by InvestorsController#index as HTML
App 26772 stderr: Read fragment views/investors/1-20150825032428475102000/zh-CN/ba9ec9f1a927c00438fab3e53de969e8 (0.2ms)
App 26772 stderr: Write fragment views/investors/1-20150825032428475102000/zh-CN/ba9ec9f1a927c00438fab3e53de969e8 (0.8ms)
App 26772 stderr:   Rendered investors/_investor.html.slim (3.8ms)
App 26772 stderr:   Rendered investors/index.html.slim within layouts/application (10.1ms)
App 26772 stderr: Completed 500 Internal Server Error in 13ms
App 26772 stderr: 
App 26772 stderr: ActionView::Template::Error (Permission denied - /home/ubuntu/campo/releases/20150824101227/tmp/cache/84B):
App 26772 stderr:     1: - cache [investor, locale] do
App 26772 stderr:     2:   .investor-frame
App 26772 stderr:     3:     .investor  id="investor-#{investor.id}"
App 26772 stderr:     4:       .investor-content
App 26772 stderr:   app/views/investors/_investor.html.slim:1:in `_app_views_investors__investor_html_slim___4107010476464905044_55536860'
App 26772 stderr:   app/views/investors/index.html.slim:40:in `_app_views_investors_index_html_slim___256757312322326570_55623180'
App 26772 stderr:

查一下文件夹目录的权限

# namei -om /home/ubuntu/campo/releases/20150824101227/tmp/cache
f: /home/ubuntu/campo/releases/20150824101227/tmp/cache
 drwxr-xr-x root   root   /
 drwxr-xr-x root   root   home
 drwxr-xr-x ubuntu ubuntu ubuntu
 drwxr-xr-x root   root   campo
 drwxr-xr-x root   root   releases
 drwxr-xr-x root   root   20150824101227
 drwxr-xr-x root   root   tmp
 lrwxrwxrwx root   root   cache -> /home/ubuntu/campo/shared/tmp/cache
   drwxr-xr-x root   root   /
   drwxr-xr-x root   root   home
   drwxr-xr-x ubuntu ubuntu ubuntu
   drwxr-xr-x root   root   campo
   drwxr-xr-x root   root   shared
   drwxr-xr-x root   root   tmp
   drwxr-xr-x root   root   cache

网上看了一下没有太好的方法,只能先修改下权限了

# chmod 777 /home/ubuntu/campo/shared/tmp/cache/

 

# gem install json -v ‘1.8.3’
Building native extensions. This could take a while…
ERROR: Error installing json:
ERROR: Failed to build gem native extension.

/usr/bin/ruby1.9.1 extconf.rb
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file — mkmf (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
from extconf.rb:1:in `<main>’
Gem files will remain installed in /var/lib/gems/1.9.1/gems/json-1.8.3 for inspection.
Results logged to /var/lib/gems/1.9.1/gems/json-1.8.3/ext/json/ext/generator/gem_make.out
解决问题的方法是

# sudo apt-get install ruby-dev

or

#yum install ruby-devel

== 测试服务器是:  test132 
== 正式服务器是:  133, 134 
== which server do you want to deploy to? (test132/133/134/83)? 134
== password for 10.xxx.xx.134 is: 
    triggering start callbacks for `deploy'
  * executing `rbenv:setup_default_environment'
  * executing "echo \"$HOME/.rbenv\""
    servers: ["10.100.27.134"]
connection failed for: 10.xxx.xx.134 (ArgumentError: invalid byte sequence in US-ASCII)

 

网上写的解决方法主要有两种,我是用第一种方法解决的

(1)修改本机的语言设置

export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"

楼主只用以上两句解决了问题,发现有的网站还补充了一句,我没执行下面这句就已经能部署了

export  LANGUAGE=en_US.UTF-8

 

(2)修改Gemfile, put this into your Gemfile , 楼主没有用这个方法,有足够探索未知世界的好奇宝宝们可以撸袖子试试

if RUBY_VERSION =~ /1.9/
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
end

 

(1)如何自动清理老的release

capistrano 部署后自动清理老 release,默认只保留 5 个 releases

添加下面代码到 config/deploy.rb

# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"

(2)如何人指定保留数量
如果想制定具体保留几个releases文件夹,设置保留数量,在 config/deploy.rb 中添加如下代码
set :keep_releases, 15
以上代码的含义是保留15次部署的releases

Usage: nginx [-?hvVt] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file

/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s stop
ps aux | grep nginx
/usr/local/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ps aux | grep nginx

查看本机ip的方法   ifcong

16:57 [root@a02.cmsapi]$ mysql -u<username>
-p<password> -h10.103.xx.xx
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 571
Server version: 5.0.77 Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input
statement.

(username@10.103.xx.xx) [(none)]> show databases;
+--------------------------+
| Database |
+--------------------------+
| information_schema |
| mvf_for_ykew_development |
| test |
+--------------------------+
3 rows in set (0.00 sec)

(username@10.103.xx.xx) [(none)]> Ctrl-C -- exit!
Aborted
tty:[0] jobs:[0] cwd:[/tmp]
16:57 [root@a02.cmsapi]$ mysql -u<username>
-p<password> -h10.103.28.25  mvf_for_ykew_development <
/tmp/db_bak.20140509.sql
tty:[0] jobs:[0] cwd:[/tmp]
16:58 [root@a02.cmsapi]$ mysql -u<username>
-py<password> -h10.103.XX.XX  mvf_for_ykew_development <
/tmp/db_bak.20140509.sql

so,how to connet the database from the other host,just try this:
mysql -u<username>  -p<password> -h10.103.xx.xx

how to import the data to the database ,try this:
mysql -u<username>
-p<password> -h10.103.xx.xx  databasename <
/tmp/db_bak.20140509.sql(the local source)