如何在CentOS 7/6和Fedora 27上使用Passenger和Apache部署Ruby应用

时间:2019-05-19 01:25:53  来源:igfitidea点击:

Phusion Passenger是一个应用服务器,可以集成到web服务器中,如Apache web服务器,并允许通过web服务器服务Ruby/Rails应用程序。
在生产系统上部署Ruby on Rails应用程序是一个不错的选择。
本文将在CentOS、Red Hat和Fedora系统上使用Apache部署Ruby应用程序。
首先,我们假设系统已经安装了Ruby。

步骤1 -安装准备工作

首先,在系统上启用epel yum存储库和更新包。

$ sudo yum install -y epel-release yum-utils
$ sudo yum-config-manager --enable epel
$ sudo yum clean all && sudo yum update -y

步骤2 -安装Apache Passenger模块

现在在你的系统上安装rails和passenger gem。
执行下面的命令来安装两个gem命令。

$ sudo yum install -y pygpgme curl

$ sudo curl --fail -sSLo /etc/yum.repos.d/passenger.repo https://oss-binaries.phusionpassenger.com/yum/definitions/el-passenger.repo

现在,使用以下命令在系统上为Apache安装passenger。

$ sudo yum-config-manager --enable cr && sudo yum install -y mod_passenge

步骤3 -验证Apache Passenger 模块

Apache的默认 Passenger 配置文件是 /etc/httpd/conf.d/passenger.conf。
我们可以编辑此文件并检查如下设置。

$ vim /etc/httpd/conf.d/passenger.conf

默认的PassengerRuby选项设置为/usr/bin/ruby。
我已经将它更改为我们自定义的与rvm一起安装的ruby。

PassengerRuby /usr/local/rvm/rubies/ruby-2.4.2/bin/ruby

现在,使用以下命令验证 Passenger 安装和配置。

$ sudo /usr/bin/passenger-config validate-install

步骤4 -配置Apache虚拟主机

让我们在Apache配置中使用域名配置虚拟主机。
虚拟主机配置示例如下所示。

<VirtualHost *:80>
    ServerName <orange>example.com</orange>

    # Path to Ruby Applications public directory
    DocumentRoot /path-to-your-app/public

    <Directory /path-to-your-app/public>
	  Allow from all
	  Options -MultiViews
	  # Uncomment this if youre on Apache > 2.4:
	  #Require all granted
    </Directory>
    </Directory>
</VirtualHost>

步骤5 -重启Apache服务

在Apache配置中添加上述行之后,使用以下命令重新启动Apache服务。

$ sudo systemctl restart httpd.service