利用Unicorn和Nginx部署Redmine

【独立博客无力维护,博文搬到此处】

Redmine是非常好用的开源项目管理系统。公司的开发基本使用Python,所以选择了Trac。前一段时间接触Rails之后,发现Rails的部署也十分的简便,所以就抽空试用了一下垂涎已久的Redmine。

本文关注的是Unicorn和Nginx的部署方式,附件是最早的apache+passenger方式的部署。

安装ruby环境

1、下面是直接用Ubuntu提供的安装包来安装Ruby,不建议使用,产品环境下应该看这里

apt-get install ruby rubygems

 2、对比ruby与gem的版本满足Redmine的要求

ruby -v
gem -v

3、设置gem的默认源为taobao提供的RubyGems镜像,剔除国内访问RubyGems官方源不稳定带来的影响:

gem source --list
gem source -r https://rubygems.org/
gem source -a http://ruby.taobao.org

安装bundler
gem install bundler

安装并配置MySQL

创建Redmine的用户和数据库:

CREATE DATABASE redmine DEFAULT CHARACTER SET utf8;
GRANT ALL ON redmine.* TO redmine@localhost IDENTIFIED BY 'redmine' WITH GRANT OPTION;

 

下载并安装Redmine依赖

1、下载Redmine并解压到目标路径下,比如/opt/redmine。

2、将redmine的gem源也改成taobao的:直接更改Gemfile内容即可。

3、安装ruby、rails依赖

apt-get install libruby ruby-dev libmagickcore-dev libmagickwand-dev libmysqlclient-dev libmysql-ruby

4、进入Redmine目录,用bundle安装Redmine依赖:

bundle install --without development test postgresql sqlite rmagick

 如果抛如下错的话,应该安装libmysqlclient-dev和libmysql-ruby:

Gem files will remain installed in /var/lib/gems/1.8/gems/mysql-2.8.1 for inspection.
Results logged to /var/lib/gems/1.8/gems/mysql-2.8.1/ext/mysql_api/gem_make.out
An error occurred while installing mysql (2.8.1), and Bundler cannot continue.
Make sure that `gem install mysql -v '2.8.1'` succeeds before bundling.

配置Redmine

1、数据库配置文件:config/database.yml。可将config/database.yml.example直接拷贝过来,然后直接去编辑。注意:根据配置文件顶部说明来确定adapter选mysql还是mysql2。

2、初始化配置和数据:

rake generate_secret_token
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake redmine:load_default_data

3、创建Redmine配置(这步不是必须,因为Redmine有默认配置):

cp config/configuration.yml.example config/configuration.yml

4、运行一下试试,正常的话你应该可以访问到Redmine了(默认用户和密码都是admin):

ruby script/rails server webrick -e production (redmine3以后rails命令不在这里了)

5、常用的Email配置:

### qq exmail
email_delivery:
    delivery_method: :async_smtp
    async_smtp_settings:
      address: smtp.exmail.qq.com
      port: 25
      domain: exmail.qq.com
      authentication: :login
      user_name: "[email protected]"
      password: "xxx"

###Gmail
email_delivery:
    delivery_method: :async_smtp
    async_smtp_settings:
      address: "smtp.gmail.com"
      port: 587
      domain: "smtp.gmail.com"
      authentication: :login
      user_name: "[email protected]"
      password: "xxx"  

安装Unicorn

1、编辑Gemfile,增加unicorn依赖:

gem "unicorn"

 2、安装unicorn:

gem install unicorn

 3、创建unicorn配置:vi config/unicorn.rb,内容如下:

app_path = "/opt/redmine"

worker_processes 2

working_directory = app_path

listen app_path + "/tmp/sockets/unicorn.sock", :backlog => 64
listen "127.0.0.1:8082", :tcp_nopush => true

# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30

# feel free to point this anywhere accessible on the filesystem
pid app_path + "/tmp/pids/unicorn.pid"

# By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path app_path + "/log/unicorn.stderr.log"
stdout_path app_path + "/log/unicorn.stdout.log"

# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

check_client_connection false

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

  # The following is only recommended for memory/DB-constrained
  # installations.  It is not needed if your system can house
  # twice as many worker_processes as you have configured.
  #
  # # This allows a new master process to incrementally
  # # phase out the old master process with SIGTTOU to avoid a
  # # thundering herd (especially in the "preload_app false" case)
  # # when doing a transparent upgrade.  The last worker spawned
  # # will then kill off the old master process with a SIGQUIT.
  # old_pid = "#{server.config[:pid]}.oldbin"
  # if old_pid != server.pid
  #   begin
  #     sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
  #     Process.kill(sig, File.read(old_pid).to_i)
  #   rescue Errno::ENOENT, Errno::ESRCH
  #   end
  # end
  #
  # Throttle the master from forking too quickly by sleeping.  Due
  # to the implementation of standard Unix signal handlers, this
  # helps (but does not completely) prevent identical, repeated signals
  # from being lost when the receiving process is busy.
  # sleep 1
end

after_fork do |server, worker|
  # per-process listener ports for debugging/admin/migrations
  # addr = "127.0.0.1:#{9293 + worker.nr}"
  # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)

  # the following is *required* for Rails + "preload_app true",
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection

  # if preload_app is true, then you may also want to check and
  # restart any other shared sockets/descriptors such as Memcached,
  # and Redis.  TokyoCabinet file handles are safe to reuse
  # between any number of forked children (assuming your kernel
  # correctly implements pread()/pwrite() system calls)
end

 4、运行unicorn_rails:

RAILS_ENV=production unicorn_rails -c config/unicorn.rb -E prodcution -D
写道
#更新20160401 for Rails4.2.5.2 / Unicorn5.1.0
bundle exec unicorn -E production -c config/unicorn.rb

配置Nginx

1、设置redmine公共目录的权限:files/ log/ tmp/public/

2、配置Nginx:

upstream redmine_backend {
    server unix:/opt/redmine/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
        listen   80; ## listen for ipv4; this line is default and implied

        server_name redmine.h7sc.com red.man1dian.com;
        keepalive_timeout 5;

        root /opt/redmine/public;

        access_log /var/log/nginx/redmine-access.log ;
        error_log /var/log/nginx/redmine-error.log ;

        location ~* ^/(images|javascripts|stylesheets|img)/ {
            access_log    off;
            log_not_found off;
            expires       max;
            break;
        }

        location / {
              proxy_redirect     off;
              proxy_set_header   Host $host;
              proxy_set_header   X-Forwarded-Host $host;
              proxy_set_header   X-Forwarded-Server $host;
              proxy_set_header   X-Real-IP        $remote_addr;
              proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
              proxy_buffering    on;
              if (!-f $request_filename) {
                  proxy_pass http://redmine_backend;
                  break;
              }
         }
}

3、重启Nginx 

开始使用

部署好Redmine之后,管理员应该去做好基本的设置,比如:站点名称、welcome信息、公司logo、匿名用户看不到内容、Email

需要注意的是,Email设置时,改掉配置文件并重启后,需要admin在系统设置中再次设置from的邮件地址,填好之后页面右下角有测试的连接(这个忒不好找)。Redmine的邮件提醒值得赞一下,它提供了用户级别的5中不同选择:接收全部、只收与我相关、只收我创建、只收分配给我、完全不接收。与Trac相比起来就人性化了许多。

Redmine另一个很棒的地方是很容易更改系统的外观,同时有许多开源的、漂亮的theme供选择:

参考1Rails的常用部署方式介绍

参考2:Redmine官方部署WIFI