我一直在研究Ruby on Rails一个多月。我已经为生产VPS部署了一个应用程序,并且我对Ruby / Rails感到满意。我现在正在尝试在最近的一个Rails版本中学习和构建应用程序。由于Ruby / Rails的多个版本是Rails开发人员中常见的概念,我想我应该在不同版本中编写代码并在生产环境中维护这些应用程序。
似乎从我的谷歌搜索和stackoverflow搜索我想要做的事情并不常见。但是,它构成了我想要在我的服务器上学习/学习Rails和Passenger / Apache的基础(基于上述目标)。也就是说,在同一域名下托管多个Rails应用程序,一些是相同的ruby / rails版本,另一些是不同的版本。所以:
mywebsite.com< --- Ruby 2.3.4 / Rails 4.2.5
mywebsite.com/profile< ---一个单独的应用程序:Ruby 2.3.4 / Rails 4.2.5
mywebsite.com/cool-app< ---一个使用最新和最强大功能的独立应用程序:Ruby 2.5.0 / Rails 5.1.4
当我搜索stackoverflow以及#34; multitenancy rails passenger"时,结果恰好为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>
Haven甚至没有尝试为第三个应用程序执行VirtualHost文件。我假设PassengerRuby字段会告诉Passenger使用正确/不同的Ruby解释器。但同样,我无法找到任何人这样做,而且我特别无法找到正在做的事情的任何解释。当我发现甚至远程关闭的东西时,它是从6年前开始的,代码已经过时了,因为Passenger现在可以很容易地处理这个问题了(但是这些例子在哪里?!)。
似乎当我访问mywebsite.com/profile时,主站点应用程序仍在处理路由,因为它记录到main_site_path / log / production.log(而不是第二个应用程序的日志)。
任何帮助都会受到赞赏,但既然我知道我应该具体,这里有一些具体的事情可以帮助我知道吗?: 当我做乘客记忆统计时,或者只是主要的应用程序,是否应该为每个应用运行一个乘客流程? 如何正确定义我的主域名/ profile应该由不同的Rails应用程序处理(如果适用,可以使用不同的VirtualHost)?
感谢。
答案 0 :(得分:1)
ZOMG它有效!非常感谢tadman建议我使用Nginx作为反向代理服务器。也就是说,一个Nginx为每个应用程序提供单独的Apache进程。我可以去mywebsite.com获取主应用程序。我去mywebsite.com/subapp并获得我的第二个应用程序(与主要版本相同的Ruby / Rails版本)。我可以访问mywebsite.com/mea并获得第三个应用程序(其中包含与前两个不同的Ruby和Rails版本!)。
部分原因是可能的,因为Nginx和Apache都有一个可以安装的Passenger组件,为您提供各种指令,您可以使用这些指令指定每个网址应该提供的应用类型(passenger_ruby让你告诉Nginx使用哪个ruby解释器)。我从未见过像Phusion网站那样全面而美观的文档。
搞清楚这很有趣。这是我的设置,以便您可以看到我做了什么。如果有什么我可以做得更好的话,请告诉我。
Nginx配置: 的/ etc / nginx的/ /阿帕奇启用位点-
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文件侦听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