Запуск нескольких проектов Ruby on Rails на одном сервере.
Создаю второе приложение:
$ cd /home/marley/rails_projects
$ rails new myApp2
$ cd myApp2
$ vi Gemfile
добавил:
gem 'unicorn'
gem 'therubyracer'
gem 'execjs'
$ bundle install
// Делаю возможность различать приложения.
echo "App1" > /home/marley/rails_projects/myApp1/public/index.html
echo "App2" > /home/marley/rails_projects/myApp2/public/index.html
Меняю настройки DNS сервера домена:
Создал в настройках DNS домена A записи:
app1.sysadm.ru - ip адрес моего сервера
app2.sysadm.ru - ip адрес моего сервера
vi /home/marley/rails_projects/myApp1/config/unicorn.rb
worker_processes 2
working_directory "/home/marley/rails_projects/myApp1/"
preload_app true
timeout 30
listen "/home/marley/rails_projects/myApp1/tmp/sockets/unicorn.sock", :backlog => 64
pid "/home/marley/rails_projects/myApp1/tmp/pids/unicorn.pid"
stderr_path "/home/marley/rails_projects/myApp1/log/unicorn.stderr.log"
stdout_path "/home/marley/rails_projects/myApp1/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
vi /home/marley/rails_projects/myApp2/config/unicorn.rb
worker_processes 2
working_directory "/home/marley/rails_projects/myApp2/"
preload_app true
timeout 30
listen "/home/marley/rails_projects/myApp2/tmp/sockets/unicorn.sock", :backlog => 64
pid "/home/marley/rails_projects/myApp2/tmp/pids/unicorn.pid"
stderr_path "/home/marley/rails_projects/myApp2/log/unicorn.stderr.log"
stdout_path "/home/marley/rails_projects/myApp2/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
Теперь нам нужно запустить unicorn как демон, для каждого проекта свой:
$ cd /home/marley/rails_projects/myApp1
$ unicorn_rails -c config/unicorn.rb -E production -D
$ cd /home/marley/rails_projects/myApp2
$ unicorn_rails -c config/unicorn.rb -E production -D
Инсталлирую сервер nginx
# yum install -y nginx
# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bkp
# vi /etc/nginx/nginx.conf
...
http {
...
upstream app1.sysadm.ru {
# путь должен совпадать с тем, что в конфиге unicorn.rb
server unix:/home/marley/rails_projects/myApp1/tmp/sockets/unicorn.sock;
}
upstream app2.sysadm.ru {
server unix:/home/marley/rails_projects/myApp2/tmp/sockets/unicorn.sock;
}
}
# vi /etc/nginx/conf.d/myApp1.conf
server {
listen 8080;
server_name app1.sysadm.ru;
location / {
root /home/marley/rails_projects/myApp1/public;
if (!-f $request_filename) {
#делаем проксипасс на первый поток
proxy_pass http://app1.sysadm.ru;
break;
}
}
}
# vi /etc/nginx/conf.d/myApp2.conf
server {
listen 8080;
server_name app2.sysadm.ru;
location / {
root /home/marley/rails_projects/myApp2/public;
if (!-f $request_filename) {
#делаем проксипасс на второй поток
proxy_pass http://app2.sysadm.ru;
break;
}
}
}
service nginx restart
http://app1.sysadm.ru:8080/index.html
App1
http://app2.sysadm.ru:8080/index.html
App2