Installing Redmine in 15 minutes (RVM + RoR + Unicorn + Nginx)

This article is the result of work on the installation and autonomy of a Linux-based server with Redmine from various sources  (including the official instructions) , some of the commands and the sequence of actions were taken from other sources. All sources used are indicated at the end of the article.





Introduction

In general, the task sounded like thisinstall Redmine on a server with a web server on nginx .





Since  Redmine is  written in  RoR , it is necessary to have a RoR environment, but the problem is that  different RoR applications may require different versions of the environment . In my case, it was necessary to provide for the possibility of installing RoR applications with different environments, which means that a version manager is needed  that will deploy the required environment in the right place.





RVM  is a version manager of ruby โ€‹โ€‹environments, ranging from interpreter versions to jams. It is needed in order to  run different RoR applications on the same server , which may require different runtime environments.





I also wanted a classic version of a web server on nginx. However, nginx does not know how to execute the application, and in this case acts as a proxy to the executing RoR web server of the application.





Unicorn  is a web server for  Rack  (and including RoR) applications. For some reason, I didn't want to use  passenger , probably because  nginx must be compiled with passenger , that is, not pure nginx. And also because  Unicorn can be different for each RoR application , and not one global one.





Now the  task  becomes clearer:  install Redmine on the server, deploying the RoR + Unicorn + Nginx stack, with automatic launch .





Training

Disable ipv6

: ,  :)





VPS Ubuntu 16.04  apt-get install



   ipv6



:





0% [Connecting to archive.ubuntu.com (2001:67c:1562::15)]
      
      



: ipv6   /etc/sysctl.conf:





echo '' >> /etc/sysctl.conf
echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf
echo 'net.ipv6.conf.default.disable_ipv6 = 1' >> /etc/sysctl.conf
echo 'net.ipv6.conf.lo.disable_ipv6 = 1' >> /etc/sysctl.conf
sysctl -p
      
      



curl   RVM (Ruby Version Manager):





apt-get install curl
      
      



:  :)





Midnight Commander ( ):





apt-get install mc
      
      



Nano ( ):





apt-get install nano
      
      



Redmine

  Redmine 4.1.1.  /opt/



    redmine   :





wget --no-check-certificate https://www.redmine.org/releases/redmine-4.1.1.tar.gz
tar xvzf redmine-4.1.1.tar.gz
mv redmine-4.1.1 redmine
rm redmine-4.1.1.tar.gz
      
      



Redmine: /opt/redmine/







 1000



,  www-data



 (  www-data



):





chown -R www-data /opt/redmine/
      
      



PostgreSQL

PostgreSQL!





, redmine -  PostgreSQL >9.2.





:





apt policy postgresql
      
      



:





postgresql:
  Installed: (none)
  Candidate: 9.5+173ubuntu0.3
  Version table:
     9.5+173ubuntu0.3 500
        500 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages
     9.5+173 500
        500 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages
      
      



, .





apt install postgresql
      
      



PostgreSQL ( , postgres psql), :





sudo -i -u postgres
# su postgres
psql
      
      



CREATE ROLE redmine LOGIN ENCRYPTED PASSWORD 'password' NOINHERIT VALID UNTIL 'infinity';
CREATE DATABASE redmine WITH ENCODING='UTF8' OWNER=redmine;
ALTER DATABASE "redmine" SET datestyle="ISO,MDY";
      
      



 \q







PostgreSQL, GitLab ( PostgreSQL), .





My SQL

MySQL!





MySQL:





apt-get install mysql-server
      
      



mysql:





mysql -u root -p
      
      



MySQL < 8.0   redmine, , :





CREATE DATABASE redmine CHARACTER SET utf8mb4;
CREATE USER 'redmine'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON redmine.* TO 'redmine'@'localhost';
      
      



, MySQL >= 8.0  mysqlnativepassword (  ):





ALTER USER 'redmine'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
      
      



MySQL 8, MySQL.





RVM

 gnupg2  RVM:





apt install gnupg2
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
      
      



 /opt/



  rvm :





curl -sSL https://get.rvm.io -o rvm.sh
      
      



:





wget --output-document=rvm.sh https://get.rvm.io
      
      



RVM (  /opt/



):





cat rvm.sh | bash -s stable --rails
      
      



RVM  rvm



,  www-data



 ( RVM):





usermod -a -G rvm www-data
      
      



Ruby On Rails  (   ):





source /usr/local/rvm/scripts/rvm
      
      



Redmine 4.1.1, Ruby [2.3, 2.6]



. RVM:





rvm list known
      
      



, :





[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.9]
[ruby-]2.5[.7]
[ruby-]2.6[.5]
[ruby-]2.7[.0]
      
      



 Ruby 2.6.5



, :





rvm install 2.6.5
rvm use 2.6.5
      
      



  Redmine:





rvm gemset create redmine
echo "rvm use ruby-2.6.5@redmine" > /opt/redmine/.rvmrc
      
      



 /opt/redmine/



  ,  ruby-2.6.5@redmine



:





Using /usr/local/rvm/gems/ruby-2.6.5 with gemset redmine
      
      



Redmine

 /opt/redmine/config/database.yml.sample



    . :





touch /opt/redmine/config/database.yml
      
      



:





production:
  adapter: postgresql
  database: redmine
  host: localhost
  username: redmine
  password: password
      
      



RoR :





bundle
      
      



RoR .





PostgreSQL  bundle



   pg



:





Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
...
Gem files will remain installed in /usr/local/rvm/gems/ruby-2.6.5/gems/pg-1.1.4
for inspection.
Results logged to
/usr/local/rvm/gems/ruby-2.6.5/extensions/x86_64-linux/2.6.0/pg-1.1.4/gem_make.out
 
An error occurred while installing pg (1.1.4), and Bundler cannot
continue.
Make sure that gem install pg -v '1.1.4' --source 'https://rubygems.org/'
succeeds before bundling.
      
      



 , :





apt-get install libpq-dev
      
      



MySQL mysql2 ( PostgreSQL).  , :





apt-get install build-essential ruby-dev libmysqlclient-dev
      
      



 nokogiri,  , :





apt-get install build-essential patch ruby-dev zlib1g-dev liblzma-dev
gem install nokogiri
      
      



 bundle







 5-, 6- 7-    :





bundle exec rake generate_secret_token
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production REDMINE_LANG=ru bundle exec rake redmine:load_default_data
      
      







Redmine  webrick:





bundle exec rails server webrick -e production
      
      



, 3000  (localhost:3000



  ip:3000



).





webrick, .





Unicorn

 /opt/redmine/GemFile



  :





gem 'unicorn'
      
      



unicorn:





touch /opt/redmine/config/unicorn.rb
      
      



:





#    
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir
 
#    unicorn
worker_processes 2
preload_app true
timeout 30
 
#     nginx
listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
 
#   
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"
 
#  pid ,      unicorn (    )
pid "#{shared_dir}/pids/unicorn.pid"
 
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
      
      



:





bundle
      
      



:





mkdir -p /opt/redmine/shared/pids /opt/redmine/shared/sockets /opt/redmine/shared/log
chown -R www-data:rvm shared
      
      



 bundle



 ( ), unicorn (  /opt/redmine/



):





unicorn_rails -c config/unicorn.rb -E production -D
      
      



unicorn  pid



:





pkill -QUIT --pidfile /opt/redmine/shared/pids/unicorn.pid
      
      



  Unicorn.





Nginx

nginx:





apt-get install nginx
      
      



nginx (  /etc/nginx/nginx.conf



),  http



 ( ):





upstream redmine {
    server unix:/opt/redmine/shared/sockets/unicorn.sock fail_timeout=0;
}
 
server {
    listen 80;
    root /opt/redmine/public;
    try_files $uri/index.html $uri @redmine;
 
    location @redmine {
        proxy_pass http://redmine;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
}
      
      



( Redmine ip ):





user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
 
events {
  worker_connections 768;
}
 
http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
 
  server_names_hash_bucket_size 64;
 
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
 
  # Logging Settings
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
 
  # Gzip Settings
  gzip on;
 
  upstream redmine {
    server unix:/opt/redmine/shared/sockets/unicorn.sock fail_timeout=0;
  }
 
  server {
    listen 80;
    root /opt/redmine/public;
    try_files $uri/index.html $uri @redmine;
 
    location @redmine {
      proxy_pass http://redmine;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
    }
  }
}
      
      



nginx:





service nginx restart
      
      



nginx ,  ip



  (  localhost



), Redmine .





 ( Unicorn), Unicorn , .





,    .





 /etc/systemd/system/



   redmine.service



:





touch /etc/systemd/system/redmine.service
      
      



:





[Unit]
Description=redminerun
After=syslog.target network.target
 
[Service]
Type=forking
PIDFile=/opt/redmine/shared/pids/unicorn.pid
WorkingDirectory=/opt/redmine/
User=www-data
Group=rvm
Environment=RAILS_ENV=production
ExecStart=/opt/redmine/config/unicorn_start
ExecReload=/opt/redmine/config/unicorn_reload
ExecStop=/opt/redmine/config/unicorn_stop
OOMScoreAdjust=-100
TimeoutSec=30
Restart=always
RestartSec=20s
 
[Install]
WantedBy=multi-user.target
      
      



:





  • PIDFile - pid



    ,





  • User Group - ,





  • ExecStart -





  • ExecReload -





  • ExecStop -





  • RestartSec - N





ExecStart



ExecReload



ExecStop



  bash .  ( / , :))





 /opt/redmine/config/



  , :





cd /opt/redmine/config/
touch unicorn_start unicorn_reload unicorn_stop
chown www-data:rvm unicorn_*
chmod +x unicorn_*
      
      



unicorn_start



  unicorn ( rvm



, redmine - , unicorn):





#!/bin/bash -
 
source /usr/local/rvm/scripts/rvm
cd /opt/redmine/
unicorn_rails -c config/unicorn.rb -E production -D
      
      



unicorn_stop



  :





#!/bin/bash -
 
pkill -QUIT --pidfile /opt/redmine/shared/pids/unicorn.pid
      
      



unicorn_reload



   ( ):





#!/bin/bash -
 
/opt/redmine/config/unicorn_stop
/opt/redmine/config/unicorn_start
      
      



:





systemctl status redmine
 
โ— redmine.service - redminerun
   Loaded: loaded (/etc/systemd/system/redmine.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
      
      



:





systemctl enable redmine
      
      



 start



/reload



/stop



.





:





systemctl start redmine
      
      



, ,   Unicorn - !









Author: Vitaly Buturlin








All Articles