部署 Ruby on Rails - Heroku 有好的替代方案吗? [英] Deploying Ruby on Rails - Is there a good alternative for Heroku?

查看:26
本文介绍了部署 Ruby on Rails - Heroku 有好的替代方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始一个新的小型企业,如果您愿意,可以创建一个 POC,并且我想在某个地方免费部署我的 Rails 应用程序.我发现有Heroku,还有其他选择吗?

解决方案

这是一篇关于这个主题的非常好的帖子

http://blog.sudobits.com/2013/02/15/heroku-alternatives-for-deploying-rails-applications

我一一浏览了那里的选项,以我的拙见,OpenShift 是中小型网站的最佳选择,至少对于开发和创建 POCPrototype 的开始

为什么更好?

  • 它给了你 Heroku 给你的东西
  • 它为您提供本地(持久)存储 - 因此您无需在 Amazon 或同等设备上为 S3 付费 - 我想在某些时候您会想要这样做(使用 S3),但至少对于一开始你不需要花钱
  • 网站似乎运行得更快
  • 我发现它在控制机器上发生的事情方面更加灵活

有什么缺点吗?

  • 我在 Openshift 上发现的唯一缺点是,部署似乎比在 Heroku 上花费的时间要长得多,并且第一次访问网站时有相当大的延迟(但在第一次之后工作得更快 - 不相关如果您认为这是问题,请转到浏览器缓存)

可能是本文档下的 Gemfile 部分 https://www.openshift.com/blogs/converting-an-existing-rails-app-to-run-on-openshift 可以解决这个问题...我稍后再试

为了在 Openshift 上部署我现有的应用程序,我需要执行哪些步骤?

在很多地方我找到了关于如何创建和部署新应用程序的解释,但有点难以理解如何将我现有的应用程序放到 OpenShift 上

这就是我要解释的原因

假设- 您的应用程序已经由 git 控制- 它的路径是/home/dev/MyApp- 你现在在/home/dev

  1. https://www.openshift.com
  2. 上创建 Openshift 帐户
  3. 使用命令行工具,我发现它们提供了更多信息
  4. 转到https://www.openshift.com/get-started
  5. 按照步骤 1..3 安装和设置命令行工具
  6. cd 到您的 rails 应用程序文件夹/home/dev/MyApp
  7. 创建一个新的应用程序域 - 运行:rhc domain-create
  8. 通过运行在 OpenShift 中创建您的应用程序

    rhc app-create -a MyApp -t ruby​​-1.9 --no-git

    -a 设置 OpenShift 下的应用程序名称 - 它可以与您的 Rails 应用程序名称完全不同

    -t 设置应用程序类型 - 我认为目前 ruby​​ 1.9 是他们支持的最高版本

    --no-git 告诉不要创建 git - 因为我们已经有了一个

  9. 设置您的数据库

    • 通过调用

      为您的 OpenShift 应用程序安装适当的数据库盒

      rhc 墨盒添加 -a <应用程序名称>

      例如:

      rhc 墨盒添加mysql-5.1 -a MyApp

      它还支持MongoDB和PostgreSQL

      (参见此处https://www.openshift.com/developers/technologies)

    • 更改您的 database.yml 以与 OpenShift 数据库相关 - 现在,这很容易,因为 OpenShift 获得了许多配置作为环境变量,您可以在任何需要的地方使用它 - 例如:

    <块引用>

    生产:适配器:mysql编码:utf8数据库:<%=ENV['OPENSHIFT_APP_NAME']%>池:5主机:<%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>端口:<%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>用户名:<%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>密码:<%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>套接字:<%=ENV['OPENSHIFT_MYSQL_DB_SOCKET']%>

  10. 确保一切都在本地运行

    • 运行:'捆绑安装'
    • 运行:'rails s' - 查看一切正常
  11. Git - 添加 OpenShift 存储库作为您的远程存储库之一并推送它

    • 确保您的所有工作都已更新、提交并与您的 GitHub 同步 - 这可以在以后省去很多麻烦
    • 运行:rhc app-show <应用程序名称> - 这将显示关于你的应用程序的所有信息 - 复制 Git URL
    • 运行:git remote add openshift
    • 通过合并获取 OpenShift 添加的任何内容

      运行:git merge openshift/master -s recursive -X ours

    • 提交更改:git commit -am '添加 OpenShift 文件

    • 推送到 OpenShift:git push openshift

就是这样,现在您的应用程序应该部署在 OpenShift 上

如何打开我部署的网站?

使用 rhc app-show 命令你可以看到你的网站 url

通常是http://<应用名称>-<域名>.rhcloud.com

很容易将其更改为您自己的域

  • 只需运行 rhc alias add <您的域>
  • 然后在您的 DNS 管理中 - 编辑 CNAME 'www' 定义以指向http://<应用名称>-<域名>.rhcloud.com

如何连接到我的 OpenShift 机器?

再次,使用 rhc app-show 可以看到 SSH 地址只需运行 ssh 即可连接

如何在部署时自动运行迁移和种子?

OpenShift 的一个好处是能够添加在部署的不同阶段触发的自定义操作(操作挂钩)

您可以在此处阅读更多相关信息https://www.openshift.com/developers/deploying-和建筑应用

现在,我只谈部署动作钩子

  1. 在您的应用程序文件夹下,转到 .openshift/action_hooks 并在其下创建一个名为 deploy 的文件
  2. 使这个文件可执行 - 运行:chmod +x deploy
  3. 输入一些代码

    例如:

    <块引用>

    #!/bin/bash

    回显开始部署"

    pushd ${OPENSHIFT_REPO_DIR} >/dev/null

    echo "将目录更改为 ${OPENSHIFT_REPO_DIR}public"

    cd ${OPENSHIFT_REPO_DIR}

    公开光盘

    echo "创建指向 ${OPENSHIFT_DATA_DIR} 上传命名上传的软链接"

    ln -s ${OPENSHIFT_DATA_DIR}上传上传

    echo "Running bundle exec rake db:migrate RAILS_ENV=production"

    bundle exec rake db:migrate RAILS_ENV="production"

    echo "Running bundle exec rake db:seed RAILS_ENV=production"

    bundle exec rake db:seed RAILS_ENV="production"

    popd >/dev/null

    • 软链接后面会解释——路由需要找到Carrierwave上传的文件
  4. 将文件添加到您的 git - git add deploy(当然是从文件夹内部)
  5. 提交更改并推送到 openshift remote

如何集成 Carrierwave 以便上传文件并保存在 OpenShift 上?

有两点

  1. 文件将保存在哪里?- 这很容易
  2. 路由会知道如何获取我上传的文件吗?

设置保存路径:

将 initializerscarrierwave.rb 文件设置为

CarrierWave.configure 做 |config|如果 Rails.env.production?config.storage = :fileconfig.enable_processing = trueconfig.root = ENV['OPENSHIFT_DATA_DIR']config.cache_dir = config.root + '上传'结尾结尾

默认上传器保持原样,意思是storage :file

<块引用>

定义 store_dir

上传/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

结束

使用 $OPENSHIFT_DATA_DIR 文件夹将确保我们能够写入文件并将其保留在那里

确保路由会找到文件:

我花了一段时间才想出这个技巧(不是太多,只是几个小时)

Rails 路由只知道如何关联应用程序文件夹下的文件夹 - 所以,在 OpenShift 机器上,它会在${OPENSHIFT_REPO_DIR}public 文件夹,这是我放在 deploy 文件中的软链接变得方便的地方 - 它只是欺骗系统并让它去从一个文件夹中获取这些文件不在应用程序文件夹下

<小时>

我知道有很多关于这些问题以及如何部署和所有内容的帖子,但我只是想对其进行排序.

当然,我写的可能有错误和不准确之处,因为我没有记录每一步,但仅凭我记得的,如果我在某些细节上有错误,请随时纠正我.

最好的来源之一是 OpenShift 文档

我希望这些东西可以帮助人们并节省您的时间和金钱

享受

I'm starting a new small venture, a POC if you would like, and I want to deploy my Rails application for free somewhere. I found that there is Heroku, are there another options?

解决方案

Yes there are

This is a very good post I found on the subject

http://blog.sudobits.com/2013/02/15/heroku-alternatives-for-deploying-rails-applications

I went over the options there one by one and, to my humble opinion, OpenShift is the best option for a small-medium website, at least for the beginning of developing and creating a POCPrototype

Why is it better?

  • It gives you what Heroku will give you
  • It give you local (persistant) storage - So you don't need to pay for S3 on Amazon or equivalent - I guess that in some point you would like to do that (to use S3), but at least for the begining you don't need to put money on it
  • It seems that the website is running faster
  • I find it more flexible in terms of controlling what is happening on the machine

Are there any disadvantages?

  • The only disadvantage I could find on Openshift is that it seems that the deployment takes much more time then on Heroku and there is considerable delay on the first time you address the website (But after the first time is working faster - Not related to browser caching if you think this is the issue)

It might be that the Gemfile section under this document https://www.openshift.com/blogs/converting-an-existing-rails-app-to-run-on-openshift can solve this issue...I will try it later

What are the steps I need to do in order to deploy my already existing application on Openshift?

In many places I found explanations about how to create and deploy new application, but it was a little bit hard to understand how to put my already existing application on OpenShift

This is the reason I want to explain that

Assumptions - Your application is git controlled already - Its path is /home/dev/MyApp - you are now under /home/dev

  1. Create Openshift account on https://www.openshift.com
  2. Use the command line tools, I found them much more informative
  3. Go to https://www.openshift.com/get-started
  4. Follow steps 1..3 in order to install and setup the command line tools
  5. cd to your rails application folder /home/dev/MyApp
  6. Create a new application domain - Run : rhc domain-create <domain name>
  7. Create your application in OpenShift by running

    rhc app-create -a MyApp -t ruby-1.9 --no-git

    -a sets the application name under OpenShift - It can be something totally different from your rails application name

    -t sets the application type - I think that for now ruby 1.9 is their highest supported version

    --no-git tells not to create git - Because we already got one

  8. Setup your DB

    • Install the appropriate DB cartridge for your OpenShift application by calling

      rhc cartridge add <DB cartridge type> -a <Application Name>

      For example :

      rhc cartridge add mysql-5.1 -a MyApp

      It also supports MongoDB and PostgreSQL

      (see here https://www.openshift.com/developers/technologies)

    • Change your database.yml to relate to the OpenShift database - Now, that is very easy since OpenShift got a lot of its configuration as environment variables and you can simply use it wherever you need - For example :

    production:
      adapter: mysql
      encoding: utf8
      database: <%=ENV['OPENSHIFT_APP_NAME']%>
      pool: 5
      host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>
      port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>
      username: <%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>
      password: <%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>
      socket: <%=ENV['OPENSHIFT_MYSQL_DB_SOCKET']%>
    

  9. Make sure everything is working locally

    • Run : 'bundle install'
    • Run : 'rails s' - See that everything is OK
  10. Git - Add the OpenShift repository as one of your remote repositories and push it

    • Make sure all your work is updated , commited and syncronized with your GitHub - This can save a lot of headach later
    • Run : rhc app-show <application name> - This will show you all the information about your application - Copy the Git URL
    • Run : git remote add openshift <OpenShift repo URL>
    • Take whatever OpenShift is adding by merging

      Run : git merge openshift/master -s recursive -X ours

    • Commit the changes : git commit -am 'adding OpenShift files

    • Push to OpenShift : git push openshift

That is all , now your application should be deployed on OpenShift

How do I open my deployed web site?

Using the rhc app-show <application name> command you can see your website url

It will usually be http://<application name>-<domain name>.rhcloud.com

It is quite easy to change it to your own domain

  • Just run rhc alias add <app name> <your domain>
  • Then in your DNS management - Edit the CNAME 'www' definition to point to http://<application name>-<domain name>.rhcloud.com

How do I connect to my OpenShift machine?

Again , using rhc app-show <application name> you can see the SSH address Simply run ssh <SSH address> in order to connect

How do I run the migrate and seed automatically upon deployment?

One nice thing in OpenShift is the ability to add custom actions (action-hooks) that are being triggered in different stages of the deployment

You can read more about it here https://www.openshift.com/developers/deploying-and-building-applications

For now, I will only talk about the deploy action-hook

  1. Under your application folder go to .openshift/action_hooks and create a file named deploy under it
  2. Make this file executable - Run : chmod +x deploy
  3. Put some code into it

    For example:

    #!/bin/bash

    echo "Starting deploy"

    pushd ${OPENSHIFT_REPO_DIR} > /dev/null

    echo "Change directory to ${OPENSHIFT_REPO_DIR}public"

    cd ${OPENSHIFT_REPO_DIR}

    cd public

    echo "Creating soft link to ${OPENSHIFT_DATA_DIR}uploads named uploads"

    ln -s ${OPENSHIFT_DATA_DIR}uploads uploads

    echo "Running bundle exec rake db:migrate RAILS_ENV=production"

    bundle exec rake db:migrate RAILS_ENV="production"

    echo "Running bundle exec rake db:seed RAILS_ENV=production"

    bundle exec rake db:seed RAILS_ENV="production"

    popd > /dev/null

    • The soft link will be explained later - it is needed for the routing to find the Carrierwave uploaded files
  4. Add the file to your git - git add deploy (from inside the folder of course)
  5. Commit your changes and push to openshift remote

How can I integrate Carrierwave so I could upload files and save on OpenShift?

There are two points

  1. Where will the files be saved ? - This is easy
  2. Will the routing know how to fetch my uploaded files ?

Setting the saving path:

Set the initializerscarrierwave.rb file to be

CarrierWave.configure do |config|
  if Rails.env.production?
    config.storage = :file
    config.enable_processing = true
    config.root = ENV['OPENSHIFT_DATA_DIR']
    config.cache_dir = config.root + 'uploads'
  end    
end

leave the uploaders as they are by default, meaning storage :file

and

def store_dir

"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

end

The use of the $OPENSHIFT_DATA_DIR folder will make sure we will be able to write the files and that it will stay there

Making sure the routing will find the files:

It took me a while to come up with that trick (not too much, just a couple of hours)

Rails routing knows how to relate only the the folders that are under the application folder - soo, on OpenShift machine, it will look for the uploaded files folder (named uploads in our case) under the ${OPENSHIFT_REPO_DIR}public folder , this is where the soft link I put in the deploy file is becoming handy - It simply cheats the system and making it to go and fetch those file from a folder which is not under the application folder


I know there are a lot of posts about those issues and how to deploy and everything, but I just wanted to put some order into it.

Of course there might be mistakes and inaccuracies in what I wrote since I didn't documented every step of the way but just from what I remember, feel free to correct me if I'm wrong in some details.

One of the best sourced is the OpenShift documentation

I hope those things will help people and save you time and money

Enjoy

这篇关于部署 Ruby on Rails - Heroku 有好的替代方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆