Crmgo Blog

Redmine 3.3 + Unicorn + Nginx on CentOS 7

Redmine 3.3 + Unicorn + Nginx on CentOS 7

Heads-up on the pitfalls: the bundle doesn't install with MariaDB 10; I had to reinstall version 5.5. Before installing MariaDB 5.5, you need to remove everything from /var/lib/mysql. After that, download Redmine 3.3.0 and extract it to a convenient folder. Let's move the files to one of the directories designated for web applications…

Redmine 3.3 + Unicorn + Nginx on CentOS 7

Heads up: the bundle does not install with MariaDB 10; I reinstalled version 5.5.

Before installing MariaDB 5.5, you need to remove everything from /var/lib/mysql.

# yum install epel-release
# yum install unzip patch ruby freetds-devel ImageMagick-c++-devel mariadb-devel gcc ruby-devel rubygem-json rubygem-psych libyaml

After that, download Redmine 3.3.0 and extract it to a convenient folder.

# wget http://www.redmine.org/releases/redmine-3.3.0.zip
# unzip redmine-3.3.0.zip

Move the files to one of the directories designated for web applications and create a configuration file for connecting to the database:

# mv redmine-3.3.0 /var/www/redmine
# cd /var/www/redmine/config 
# cp database.yml.example database.yml 
# vim database.yml
production:
  adapter: mysql2
  database: redmine
  host: localhost
  username: redmine
  password: "xxxxx"
  encoding: utf8

Create the database:

# mysql -p
MariaDB [(none)]> CREATE DATABASE redmine CHARACTER SET utf8 COLLATE utf8_general_ci;
MariaDB [(none)]> grant all privileges on redmine.* to redmine@localhost identified by 'xxxxx';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit

Return to the application directory and install bundler and the remaining gems.

# cd /var/www/redmine/

# gem install bundler

# vim Gemfile 
gem 'unicorn'

# export PATH='/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/user/.local/bin:/home/user/bin'
# bundle install --without development test
...
Bundle complete! 27 Gemfile dependencies, 46 gems now installed.
...

Next, according to the instructions, you need to generate a secret key, run migrations, and populate the database with demo data. During installation, I recommend selecting the Russian language right away.

# rake generate_secret_token
# RAILS_ENV=production rake db:migrate
# RAILS_ENV=production rake redmine:load_default_data
Select language: ar, az, bg, bs, ca, cs, da, de, el, en, en-GB, es, es-PA, et, eu, fa, fi, fr, gl, he, hr, hu, id, it, ja, ko, lt, lv, mk, mn, nl, no, pl, pt, pt-BR, ro, ru, sk, sl, sq, sr, sr-YU, sv, th, tr, uk, vi, zh, zh-TW [en] ru
====================================
Default configuration data loaded.

Let's check if all components are installed and if the application can run. For this, you can use the Webrick web server, which comes with the Rails gem.

# rails server webrick -e production -b 192.168.0.10
=> Booting WEBrick
=> Rails 4.2.1 application starting in production on http://192.168.0.10:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2015-06-13 15:24:41] INFO WEBrick 1.3.1 [2015-06-13 15:24:41] INFO ruby 2.2.1 (2015-02-26) [x86_64-linux] [2015-06-13 15:24:41] INFO WEBrick::HTTPServer#start: pid=27426 port=3000

You can visit the specified address and see the Redmine main page.

Next, create the configuration for the new gem.

# vim config/unicorn.rb

worker_processes 1   # Количество процессов: master + 1 дочерний
working_directory "/var/www/redmine/" # Домашний каталог redmine
 
preload_app true
 
timeout 30
 
listen "/var/www/redmine/tmp/sockets/unicorn.sock", :backlog => 64 #путь к unix сокету с которым будет работать nginx
 
pid "/var/www/redmine/tmp/pids/unicorn.pid" # Место хранения PID-файла
 
stderr_path "/war/www/redmine/log/unicorn.stderr.log" # файл логов для ошибок
stdout_path "/war/www/redmine/log/unicorn.stdout.log" # файл логов для вывода
 
before_fork do |server, worker|
    defined?(ActiveRecord::Base) and
        ActiveRecord::Base.connection.disconnect!
end
 
after_fork do |server, worker|
    defined?(ActiveRecord::Base) and
        ActiveRecord::Base.establish_connection
end

And try to start it.

# bundle exec unicorn_rails -E production -c config/unicorn.rb -D
# ps aux | grep unicorn

Now, we need to configure Unicorn to work with Nginx:

# vim /etc/nginx/conf.d/redmine.conf

upstream redmine {
    server unix:/var/www/redmine/tmp/sockets/unicorn.sock;
}

server {
        listen 80;
        server_name redmine.domain.com.ua;
        client_max_body_size 200m;
        root /var/www/redmine/;

        location / {
                try_files $uri @ruby;
        }

        location ~* \.(jpg|gif|png|js|css|ico)$ {
                root /var/www/redmine/public;
                expires 7d;
                error_page 404 502 504 = @ruby;
        }

        location @ruby {
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header  Host $http_host;
                proxy_redirect off;
                proxy_read_timeout 300;
                proxy_pass http://redmine; #upstream redmine
        }
}

Check the configuration and apply the rules.

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx -s reload

Go to http://redmine.domain.com.ua

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.