📜 ⬆️ ⬇️

We lift several Ruby on Rails projects on the same server under different versions of ruby ​​(Nginx + Unicorn)

Hello, hello!

I want to share with you a way to deploy multiple Ruby on Rails applications on a single server.
RVM, Unicorn and Nginx will help us in this.

Recently, the customer asked to put Redmine to them on the server where the Ruby on Rails project was already spinning and using Ruby version 1.9.2. And redmine works maximum with ruby-1.8.7. Since no other projects were supposed to be placed on the server, Nginx + mod_passenger was originally installed there. As you know, using mod_passenger on the same server will not run two applications with different versions of Ruby.

To begin with, we will set our environment for each project using RVM.
It is assumed that you have rvm installed, if not, then install it .
')
We need two versions of ruby:

$ rvm install 1.9.2 $ rvm install 1.8.7 


For convenience, we will create our own set of gems for each project:

 $ rvm use 1.9.2 $ rvm gemset create project $ rvm use 1.8.7 $ rvm gemset create redmine 


Accordingly, we obtain sets 1.9.2@project and 1.8.7@redmine

In order for rvm to automatically switch to the correct version of ruby ​​and a set of gems, do the following:

 $ echo "rvm use 1.9.2@project" > /home/username/www/project/.rvmrc $ echo "rvm use 1.8.7@redmine" > /home/username/www/redmine/.rvmrc 


Now, in the console, when the project directory is changed, the necessary version of Ruby and a set of gems for the project will be automatically connected.

You need to install unicorn. For a project with a rail version of 3.0, this is done by adding a line.
 gem 'unicorn' 

in the gemfile file at the root of the project and running

 $ bundle install 


In the case of Redmine, you need to install unicorn like this:

 $ cd /home/username/www/redmine $ gem install unicorn 


Create a project config for unicorn (/home/username/www/redmine/config/unicorn.rb):

 worker_processes 2 working_directory "/home/username/www/redmine/" preload_app true timeout 30 listen "/home/username/www/redmine/tmp/sockets/unicorn.sock", :backlog => 64 pid "/home/username/www/redmine/tmp/pids/unicorn.pid" stderr_path "/home/username/www/redmine/log/unicorn.stderr.log" stdout_path "/home/username/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 


Making a config for another project by analogy.

Now we need to run unicorn as a demon, for each project its own:

 $ cd /home/username/www/redmine $ unicorn_rails -c config/unicorn.rb -E production -D $ cd /home/username/www/project $ unicorn_rails -c config/unicorn.rb -E production -D 


It remains to configure nginx. Add two thread handlers to nginx.conf (one for each project):

 ... http {    ...    upstream project {        #     ,    unicorn.rb        server unix:/home/username/www/project/tmp/sockets/unicorn.sock;    }    upstream redmine {        server unix:/home/username/www/redmine/tmp/sockets/unicorn.sock;    } } 


Accordingly, from each “virtualhost” we make a proxy pass to the necessary stream, which uses the corresponding unicorn socket.

 server {    listen 80;    server_name project;    location / {        root /home/username/www/project/public;        if (!-f $request_filename) {            #     "project"            proxy_pass http://project;            break;        }    } } 


and

 server {    listen 80;    server_name redmine;    location / {        root /home/username/www/redmine/public;        if (!-f $request_filename) {            #     "redmine"            proxy_pass http://redmine;            break;        }    } } 


Restart nginx. Is done.

I hope someone will come in handy and save time.

Source: https://habr.com/ru/post/128048/


All Articles