简体   繁体   English

多租户铁路轨道多个应用程序不同版本同一域

[英]Multitenancy Passenger Rails Multiple Apps Different Versions Same Domain

I have been studying Ruby on Rails for just over a month. 我已经学习Ruby on Rails仅仅一个多月了。 I have deployed an app to a production VPS and am getting comfortable with Ruby/Rails in general. 我已经将一个应用程序部署到了生产VPS,并且通常对Ruby / Rails感到满意。 I am now trying to learn and build apps in one of the more recent Rails versions. 我现在正尝试在一种最新的Rails版本中学习和构建应用程序。 Since multiple versions of Ruby/Rails is a commonplace concept among Rails developers, I figure I should play around with coding in different versions and maintaining those apps in a production environment. 由于Ruby / Rails的多个版本在Rails开发人员中很普遍,我认为我应该尝试使用不同版本的代码并在生产环境中维护这些应用程序。

It seems like from my googling and stackoverflow searches that what I am trying to do is not common. 从我的谷歌搜索和stackoverflow搜索看来,我尝试执行的操作并不常见。 However, it forms the foundation for everything I want to do/learn about Rails and Passenger/Apache on my server (based on above goals). 但是,它构成了我想在服务器上进行的所有关于Rails和Passenger / Apache的学习/学习的基础(基于上述目标)。 That is, hosting multiple Rails applications under the same domain name, some being the same ruby/rails version and others a different version. 也就是说,在同一个域名下托管多个Rails应用程序,其中一些是相同的ruby / rails版本,而另一些是不同的版本。 So: 所以:

mywebsite.com <--- Ruby 2.3.4 / Rails 4.2.5 mywebsite.com <--- Ruby 2.3.4 / Rails 4.2.5

mywebsite.com/profile <--- a separate app: Ruby 2.3.4 / Rails 4.2.5 mywebsite.com/profile <---一个单独的应用程序:Ruby 2.3.4 / Rails 4.2.5

mywebsite.com/cool-app <--- a separate app using the latest and greatest features: Ruby 2.5.0 / Rails 5.1.4 mywebsite.com/cool-app <---使用最新和最强大功能的独立应用程序:Ruby 2.5.0 / Rails 5.1.4

When I search stackoverflow for "multitenancy rails passenger", there are exactly 0 results. 当我在stackoverflow中搜索“多租户导轨乘客”时,恰好有0个结果。 There is also this link: https://www.phusionpassenger.com/library/deploy/apache/ 也有此链接: https : //www.phusionpassenger.com/library/deploy/apache/

Which has an article called "Deploying multiple apps on a single server (multitenancy)", but it doesn't exist yet and says: "to-do"! 其中有一篇名为“在一台服务器上部署多个应用程序(多租户)”的文章,但尚不存在,并说:“待办事项”! I've been trying to stumble my way through it, but it seems like there is too much I don't understand to just copy and paste someone else's code and get it to work. 我一直在尝试迷失方向,但是似乎我不了解太多,仅复制和粘贴别人的代码并使之正常工作。

It seems like part of the trick to getting these things to work is with different VirtualHost setups. 使这些东西正常工作的技巧之一似乎是使用不同的VirtualHost设置。

Here is what I have tried for the above applications: 这是我为上述应用程序所尝试的:

mywebsite.com (main site): 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 (same Ruby/Rails versions as main site) 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>

Haven't even attempted to do a VirtualHost file for the 3rd app. 甚至没有尝试为第3个应用程序创建VirtualHost文件。 I would assume that the PassengerRuby field would tell Passenger to use the correct/different Ruby interpreter. 我假设PassengerRuby字段将告诉Passenger使用正确/不同的Ruby解释器。 But again, I can't find anyone who does this, and I especially can't find any explanations for what is being done. 但同样,我找不到执行此操作的任何人,尤其是找不到正在执行的操作的任何解释。 When I find something even remotely close, it was from 6 years ago and the code is obsolete because Passenger supposedly handles this pretty easily now (but where are the examples?!). 当我发现距离最近很近的东西时,那是6年前的代码了,因为乘客现在应该可以轻松地处理此代码了(但是示例在哪里?!)。

It seems like when I go to mywebsite.com/profile, the main site app is still handling the route, as it logs to main_site_path/log/production.log (not the second app's log). 好像当我转到mywebsite.com/profile时,主站点应用仍在处理路由,因为它记录到main_site_path / log / production.log(而不是第二个应用的日志)中。

Any help would be appreciated, but since I know I should be specific, here are some specific things it might help me to know?: Should there be a Passenger process running for each app when I do passenger-memory-stats, or just the main one? 任何帮助将不胜感激,但是由于我知道我应该具体,所以可能有一些我可以帮助我了解的具体事情?:当我执行乘客内存统计信息时,应该为每个应用程序运行一个“乘客”进程吗?主要的?
How do I correctly define that /profile off my main domain should be handled by a different Rails App (different VirtualHost if applicable)? 如何正确定义主域外的/ profile应该由其他Rails应用程序处理(如果适用,则使用不同的VirtualHost)?

Thanks. 谢谢。

ZOMG it works! ZOMG有效! So a big thanks to tadman for suggesting that I use Nginx as a reverse proxy server. 非常感谢tadman建议我将Nginx用作反向代理服务器。 That is, one Nginx serves individual Apache processes for each app. 也就是说,一个Nginx为每个应用程序提供单独的Apache进程。 I am able to go to mywebsite.com and get the main app. 我可以转到mywebsite.com并获取主应用程序。 I go to mywebsite.com/subapp and get my second app (same Ruby/Rails version as the main one). 我转到mywebsite.com/subapp并获得第二个应用程序(与主要版本相同的Ruby / Rails版本)。 I can go to mywebsite.com/mea and get a third app (that has a different Ruby and Rails version than the first two!). 我可以转到mywebsite.com/mea并获得第三个应用程序(与前两个版本具有不同的Ruby和Rails版本!)。

Part of this is possible because both Nginx and Apache have a Passenger component that you can install, giving you all sorts of Directives you can use to specify what kind of app each url should serve up (passenger_ruby lets you tell Nginx which ruby interpreter to use). 之所以可行,部分原因是因为NginxApache都可以安装一个Passenger组件,从而为您提供了各种指令,可用于指定每个网址应提供哪种应用程序(passenger_ruby可让您告诉Nginx使用哪个ruby解释器)。 I've never seen documentation as comprehensive and beautiful as on Phusion's website. 我从未见过像Phusion网站上的文档那样全面和美观。

Figuring this out was fun. 弄清楚这一点很有趣。 Here is my setup so that you can see what I did. 这是我的设置,以便您可以看到我的工作。 And please let me know if there's anything I could do better. 并且,如果有什么我可以做得更好的,请告诉我。

Nginx config: /etc/nginx/sites-enabled/apache 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;
    }
}

My Ports file listens for the ports served by Nginx: 我的端口文件侦听Nginx服务的端口:

/etc/apache2/ports.conf /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>

My Apache VirtualHost definitions. 我的Apache VirtualHost定义。

/etc/apache2/sites-enabled/login_app.conf /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>

And the processes produced, from the command line: passenger-memory-stats 并从命令行产生了以下过程: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM