【问题标题】:Multitenancy Passenger Rails Multiple Apps Different Versions Same DomainMultitenancy Passenger Rails 多个应用程序 不同版本 相同域
【发布时间】:2018-02-07 17:30:40
【问题描述】:

我学习 Ruby on Rails 已经一个多月了。我已经将一个应用程序部署到了生产 VPS,并且总体上对 Ruby/Rails 感到满意。我现在正在尝试在最新的 Rails 版本之一中学习和构建应用程序。由于 Ruby/Rails 的多个版本在 Rails 开发人员中是一个司空见惯的概念,我认为我应该尝试在不同版本中进行编码并在生产环境中维护这些应用程序。

从我的谷歌搜索和 stackoverflow 搜索看来,我想做的事情并不常见。然而,它构成了我想要在我的服务器上做/了解 Rails 和Passenger/Apache 的所有事情的基础(基于上述目标)。也就是说,在同一个域名下托管多个 Rails 应用程序,一些是相同的 ruby​​/rails 版本,另一些是不同的版本。所以:

mywebsite.com

mywebsite.com/profile

mywebsite.com/cool-app

当我在 stackoverflow 中搜索“多租户铁路乘客”时,正好有 0 个结果。还有这个链接: https://www.phusionpassenger.com/library/deploy/apache/

其中有一篇文章叫做“在单个服务器上部署多个应用程序(多租户)”,但它还不存在并说:“待办事项”!我一直试图通过它跌跌撞撞地通过它,但似乎有太多我不明白只是复制和粘贴别人的代码并让它工作。

似乎让这些东西正常工作的部分技巧是使用不同的 VirtualHost 设置。

这是我对上述应用程序的尝试:

mywebsite.com(主站点):

<VirtualHost *:80>
  ServerName mywebsite.com

  # Tell Apache and Passenger where your app's 'public' directory is
  DocumentRoot /var/www/login_app/code/public

  PassengerRuby /usr/local/rvm/gems/ruby-2.3.4/wrappers/ruby

  # Relax Apache security settings
  <Directory /var/www/login_app/code/public>
    Allow from all
    Options -MultiViews
    # Uncomment this if you're on Apache >= 2.4:
    Require all granted
  </Directory>

  <Directory /var/www/login_app/code/public/profile>
    Allow from all
    Options -MultiViews
    # Uncomment this if you're on Apache >= 2.4:
    Require all granted
  </Directory>

  PassengerBaseURI /profile

</VirtualHost>

mywebsite.com/profile(与主站点相同的 Ruby/Rails 版本)

<VirtualHost *:80>
  ServerName mywebsite.com

  # Tell Apache and Passenger where your app's 'public' directory is
  DocumentRoot /var/www/test_app/code/public

  PassengerAppRoot /var/www/test_app/code
  PassengerRuby /usr/local/rvm/gems/ruby-2.3.4/wrappers/ruby
  # PassengerBaseURI /profile

  # Relax Apache security settings
  <Directory /var/www/test_app/code/public>
    PassengerEnabled on
    Allow from all
    Options -MultiViews
    # Uncomment this if you're on Apache >= 2.4:
    Require all granted
  </Directory>
</VirtualHost>

甚至没有尝试为第三个应用程序创建一个 VirtualHost 文件。我假设PassengerRuby 字段会告诉Passenger 使用正确/不同的Ruby 解释器。但同样,我找不到任何这样做的人,尤其是我找不到任何关于正在做的事情的解释。当我发现一些更接近的东西时,它是 6 年前的,并且代码已经过时了,因为乘客现在应该很容易处理这个问题(但是例子在哪里?!)。

似乎当我转到 mywebsite.com/profile 时,主站点应用程序仍在处理路由,因为它记录到 main_site_path/log/production.log(不是第二个应用程序的日志)。

任何帮助将不胜感激,但由于我知道我应该具体,这里有一些具体的事情可能会帮助我知道?: 当我执行乘客内存统计时,是否应该为每个应用程序运行乘客进程,还是只运行主要进程?
如何正确定义主域之外的 /profile 应该由不同的 Rails 应用程序(如果适用,则使用不同的 VirtualHost)处理?

谢谢。

【问题讨论】:

  • 如果您有完全不同的运行时环境,您可能希望拥有一个头端服务器,它将不同的路径分派到处理每个应用程序的不同实例(虚拟机)。一台服务器拥有多个版本的 Ruby,多个应用程序,可能会变得非常混乱,尤其是当某些版本的 Ruby 需要较旧的系统依赖项时,其他版本的系统依赖项较新,等等。我发现 Nginx 在顶层,Apache +Passenger 在第二个层工作得很好,因为 Nginx 在调度和委派方面非常精简和高效。
  • 这听起来像你在说什么吗? digitalocean.com/community/tutorials/…
  • 这就是原理,是的。 Apache + Passenger 比 Nginx 更令人愉快,因为 Apache 已经编译了模块支持,而 Nginx 需要为非标准扩展重新编译,这是一个巨大的麻烦。混合环境意味着您可以获得 Apache 模块的简单性和顶级 Nginx 的性能。
  • 太棒了,谢谢!我会到处玩,看看我能做些什么。
  • 我尽我所能添加了一个答案。似乎我需要放慢速度并获得更多使用 Nginx 和 Apache 的经验。我越往下这个兔子洞,我就越糊涂,哈哈。但从高层次上看,这似乎是我想要完成的。

标签: ruby-on-rails ruby apache passenger multi-tenant


【解决方案1】:

ZOMG 有效!非常感谢tadman 建议我使用 Nginx 作为反向代理服务器。也就是说,一个 Nginx 为每个应用程序提供单独的 Apache 进程。我可以访问 mywebsite.com 并获取主应用程序。我去 mywebsite.com/subapp 并获得我的第二个应用程序(与主要应用程序相同的 Ruby/Rails 版本)。我可以访问 mywebsite.com/mea 并获得第三个应用程序(它的 Ruby 和 Rails 版本与前两个不同!)。

这部分是可能的,因为NginxApache 都有一个可以安装的Passenger 组件,为您提供各种指令,您可以使用它们来指定每个url 应该提供什么样的应用程序(passenger_ruby 让您告诉 Nginx 使用哪个 ruby​​ 解释器)。我从未在 Phusion 的网站上看到过如此全面和精美的文档。

弄清楚这一点很有趣。这是我的设置,以便您可以看到我做了什么。如果有什么我可以做得更好的,请告诉我。

Nginx 配置: /etc/nginx/sites-enabled/apache

server {
    listen 80;
    server_name mywebsite.com www.mywebsite.com;

    passenger_enabled on;

    location / {
        passenger_ruby /usr/local/rvm/gems/ruby-2.3.4/wrappers/ruby;
        proxy_pass http://my_website_ip:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /subapp {
        passenger_ruby /usr/local/rvm/gems/ruby-2.3.4/wrappers/ruby;
        proxy_pass http://my_website_ip:8081;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /mea {
        passenger_ruby /usr/local/rvm/gems/ruby-2.5.0/wrappers/ruby;
        proxy_pass http://my_website_ip:8082;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

我的 Ports 文件监听 Nginx 服务的端口:

/etc/apache2/ports.conf

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8080
Listen 8081
Listen 8082

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

我的 Apache VirtualHost 定义。

/etc/apache2/sites-enabled/login_app.conf

<VirtualHost *:8080>
  ServerName mywebsite.com

  # Tell Apache and Passenger where your app's 'public' directory is
  DocumentRoot /var/www/login_app/code/public

  PassengerRuby /usr/local/rvm/gems/ruby-2.3.4/wrappers/ruby

  # Relax Apache security settings
  <Directory /var/www/login_app/code/public>
    Allow from all
    Options -MultiViews
    # Uncomment this if you're on Apache >= 2.4:
    Require all granted
  </Directory>

</VirtualHost>

<VirtualHost *:8081>
  ServerName mywebsite.com

  # Tell Apache and Passenger where your app's 'public' directory is
  DocumentRoot /var/www/second_app/code/public

  PassengerRuby /usr/local/rvm/gems/ruby-2.3.4/wrappers/ruby

  # Adding a subapp to the base url
  Alias /subapp /var/www/second_app/code/public
  <Location /subapp>
      PassengerBaseURI /subapp
      PassengerAppRoot /var/www/second_app/code
  </Location>

  # Relax Apache security settings
  <Directory /var/www/second_app/code/public>
    Allow from all
    Options -MultiViews
    # Uncomment this if you're on Apache >= 2.4:
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:8082>
  ServerName mywebsite.com

  # Tell Apache and Passenger where your app's 'public' directory is
  DocumentRoot /var/www/third_app/code/public

  PassengerRuby /usr/local/rvm/gems/ruby-2.5.0/wrappers/ruby

  # Adding a subapp to the base url
  Alias /mea /var/www/third_app/code/public
  <Location /mea>
      PassengerBaseURI /mea
      PassengerAppRoot /var/www/third_app/code
  </Location>

  # Relax Apache security settings
  <Directory /var/www/third_app/code/public>
    Allow from all
    Options -MultiViews
    # Uncomment this if you're on Apache >= 2.4:
    Require all granted
  </Directory>
</VirtualHost>

以及产生的进程,从命令行:passenger-memory-stats

Version: 5.2.0
Date   : 2018-02-09 03:22:39 +0000

--------- Apache processes ----------
PID    PPID  VMSize    Private  Name
-------------------------------------
             148.9 MB  0.4 MB   /usr/sbin/apache2 -k start
             813.3 MB  3.1 MB   /usr/sbin/apache2 -k start
             557.3 MB  3.2 MB   /usr/sbin/apache2 -k start
### Processes: 3
### Total private dirty RSS: 6.74 MB


---------- Nginx processes -----------
PID    PPID   VMSize    Private  Name
--------------------------------------
              174.8 MB  0.7 MB   nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
              174.8 MB  0.8 MB   nginx: worker process
### Processes: 2
### Total private dirty RSS: 1.57 MB


----- Passenger processes -----
PID    VMSize    Private  Name
-------------------------------
       379.5 MB  4.7 MB   Passenger watchdog
       666.2 MB  7.1 MB   Passenger core
       378.9 MB  4.2 MB   Passenger watchdog
       662.5 MB  5.5 MB   Passenger core
       318.0 MB  63.0 MB  Passenger RubyApp: /var/www/login_app/code (production)
       314.5 MB  60.3 MB  Passenger RubyApp: /var/www/third_app/code (production)
       315.7 MB  61.4 MB  Passenger RubyApp: /var/www/second_app/code (production)
### Processes: 7
### Total private dirty RSS: 206.14 MB

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多