Running Ruby on Rails on Windows has historically sucked. Most of the Ruby/Rails folks are Mac and Linux users and haven't focused on getting Rails to be usable for daily development on Windows. There have been some heroic efforts by a number of volunteers to get Rails working with projects like RailsInstaller, but native modules and dependencies almost always cause problems. Even more, when you go to deploy your Rails app you're likely using a Linux host so you may run into differences between operating systems.
从历史上看,在Windows上运行Ruby on Rails很烂。 大多数Ruby / Rails用户都是Mac和Linux用户,他们并没有将Rails用于Windows的日常开发。 为了使Rails与RailsInstaller之类的项目一起工作,许多志愿者做出了一些英勇的努力,但是本机模块和依赖关系几乎总是会引起问题。 更重要的是,当您部署Rails应用程序时,您可能会使用Linux主机,因此可能会遇到操作系统之间的差异。
Fast forward to today and Windows 10 has the Ubuntu-based "Linux Subsystem for Windows" (WSL) and the native bash shell which means you can run real Linux elf binaries on Windows natively without a Virtual Machine...so you should do your Windows-based Rails development in Bash on Windows.
快进到今天,Windows 10具有基于Ubuntu的“ Windows Linux子系统”(WSL)和本地bash shell,这意味着您可以在Windows上直接运行真正Linux elf二进制文件,而无需虚拟机...因此您应该这样做Windows上Bash中基于Windows的Rails开发。
Ruby on Rails development is great on Windows 10 because you've Windows 10 handling the "windows" UI part and bash and Ubuntu handling the shell.
Ruby on Rails开发在Windows 10上很棒,因为Windows 10可以处理“ windows” UI部分,而bash和Ubuntu可以处理外壳。
After I set it up I want to git deploy my app to Azure, easily.
设置好之后,我想轻松地将我的应用程序git部署到Azure。
Rails and Ruby folks can apt-get update and apt-get install ruby, they can install rbenv or rvm as they like. These days rbenv is preferred.
Rails和Ruby人士可以apt-get更新和apt-get安装ruby,他们可以根据需要安装rbenv或rvm。 这些天rbenv是首选。
Once you have Ubuntu on Windows 10 installed you can quickly install "rbenv" like this within Bash. Here I'm getting 2.3.0.
一旦在Windows 10上安装了Ubuntu,就可以在Bash中像这样快速安装“ rbenv ”。 在这里我得到2.3.0。
~$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
~$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
~$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
~$ exec $SHELL
~$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
~$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
~$ exec $SHELL
~$ rbenv install 2.3.0
~$ rbenv global 2.3.0
~$ ruby -v
~$ gem install bundler
~$ rbenv reshash
Here's a screenshot mid-process on my SurfaceBook. This build/install step takes a while and hits the disk a lot, FYI.
这是我的SurfaceBook上的屏幕截图。 此构建/安装步骤需要一些时间,并且会严重击中磁盘,仅供参考。
At this point I've got Ruby, now I need Rails, as well as NodeJs for the Rails Asset Pipeline. You can change the versions as appropriate.
至此,我已经有了Ruby,现在我需要Rails,以及Rails Asset Pipeline的NodeJ。 您可以根据需要更改版本。
@ curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
$ sudo apt-get install -y nodejs
$ gem install rails -v 5.0.1
You will likely also want either PostgresSQL or MySQL or Mongo, or you can use a Cloud DB like Azure DocumentDB.
您可能还需要PostgresSQL或MySQL或Mongo,或者可以使用诸如Azure DocumentDB之类的云数据库。
When you're developing on both Windows and Linux at the same time, you'll likely want to keep your code in one place or the other, not both. I use the automatic mount point that WSL creates at /mnt/c so for this sample I'm at /mnt/c/Users/scott/Desktop/RailsonAzure which maps to a folder on my Windows desktop. You can be anywhere, just be aware of your CR/LF settings and stay in one world.
当您同时在Windows和Linux上进行开发时,您可能希望将代码放在一个或另一个位置,而不是两个都保存。 我使用WSL在/ mnt / c中创建的自动挂载点,因此对于此示例,我在/ mnt / c / Users / scott / Desktop / RailsonAzure中,该映射到Windows桌面上的文件夹。 您可以在任何地方,只要知道您的CR / LF设置并留在一个世界中即可。
I did a "rails new ." and got it running locally. Here you can se Visual Studio Code with Ruby Extensions and my project open next to Bash on Windows.
我做了一个“新手”。 并使其在本地运行。 在这里,您可以使用带有Ruby Extensions的Visual Studio Code和我的项目在Windows上的Bash旁边打开。
After I've got a Rails app running and I'm able to develop cleanly, jumping between Visual Studio Code on Windows and the Bash prompt within Ubuntu, I want to deploy the app to the web.
运行Rails应用程序后,我可以进行整洁的开发,可以在Windows上的Visual Studio Code和Ubuntu中的Bash提示符之间跳转,然后将应用程序部署到Web。
Since this is a simple "Hello World" default rails app I can't deploy it somewhere where the Rails Environment is Production. There's no Route in routes.rb (the Yay! You're on Rails message is development-time only) and there's no SECRET_KEY_BASE environment variable set which is used to verify signed cookies. I'll need to add those two things. I'll change routes.rb quickly to just use the default Welcome page for this demo, like this:
由于这是一个简单的“ Hello World”默认Rails应用程序,因此我无法将其部署在Rails环境为生产环境的地方。 route.rb中没有Route(是的!仅在开发时才出现),并且没有用于验证签名cookie的SECRET_KEY_BASE环境变量集。 我需要添加这两件事。 我将快速更改routes.rb,以仅使用此演示的默认“欢迎”页面,如下所示:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get '/' => "rails/welcome#index"
end
And I'll add the SECRET_KEY_BASE in as an App Setting/ENV var in the Azure portal when I make my backend, below.
当我在下面创建后端时,将在Azure门户中将SECRET_KEY_BASE添加为App Setting / ENV var。
From the New menu in the Azure portal, choose to Web App on Linux (in preview as of the time I wrote this) from the Web + Mobile option. This will make an App Service Plan that has an App within it. There are a bunch of application stacks you can use here including node.js, PHP, .NET Core, and Ruby.
在Azure门户的“新建”菜单中,从“ Web +移动”选项中选择Linux上的Web App (在撰写本文时为预览状态)。 这将形成一个其中包含应用程序的应用程序服务计划。 您可以在此处使用大量应用程序堆栈,包括node.js,PHP,.NET Core和Ruby 。
NOTE: A few glossary and definition points. Azure App Service is the Azure PaaS (Platform as a Service). You run Web Apps on Azure App Service. An Azure App Service Plan is the underlying Virtual Machine (sall, medium, large, etc.) that hosts n number of App Services/Web Sites. I have 20 App Services/Web Sites running under a App Service Plan with a Small VM. By default this is Windows by can run Php, Python, Node, .NET, etc. In this blog post I'm using an App Service Plan that runs Linux and hosts Docker containers. My Rails app will live inside that App Service and you can find the Dockerfiles and other info here https://github.com/Azure-App-Service/ruby or use your own Docker image.
注意:一些词汇表和定义点。 Azure App Service是Azure PaaS(平台即服务)。 您在Azure App Service上运行Web Apps 。 Azure应用程序服务计划是承载n个应用程序服务/网站的基础虚拟机(小型,中型,大型等)。 我有20个应用程序服务/网站,这些应用程序在带有小型VM的应用程序服务计划下运行。 默认情况下,这是Windows,可以运行Php,Python,Node,.NET等。在此博客文章中,我使用的是运行Linux并托管Docker容器的App Service Plan。 我的Rails应用程序将驻留在该应用程序服务中,您可以在https://github.com/Azure-App-Service/ruby处找到Dockerfiles和其他信息,或使用自己的Docker映像。
Here you can see my Azure App Service that I'll now deploy to using Git. I could also FTP.
在这里您可以看到我现在将要使用Git部署的Azure应用服务。 我也可以使用FTP。
I went into Deployment OPtions and setup a local (to Azure) git repro. Now I can see that under Overview.
我进入了“部署选项”并设置了本地(到Azure)git repro。 现在,我可以在“概述”下看到它。
On my local bash I add azure as a remote. This can be set up however your workflow is setup. In this case, Git is FTP for code.
在本地bash上,我将azure添加为遥控器。 可以设置,但是您的工作流程已设置。 在这种情况下,Git是FTP的代码。
$ git add remote azure https://[email protected]:443/RubyOnAzureAppService.git
$ git add .
$ git commit -m "initial"
$ git push azure master
This starts the deployment as the code is pushed to Azure.
随着代码被推送到Azure,这将开始部署。
IMPORTANT: I will also add "RAILS_ENV= production" and a SECRET_KEY_BASE=to my Azure Application Settings. You can make a new secret with "rake secret."
重要说明:我还将在我的Azure应用程序设置中添加“ RAILS_ENV =生产”和SECRET_KEY_BASE =。 您可以使用“ rake secret”(秘密)来创建新的秘密。
If I'm having trouble I can turn on Application Logging, Web Server Logging, and Detailed Error Messages under Diagnostic Logs then FTP into the App Service and look at the logs.
如果遇到问题,可以在“诊断日志”下打开“应用程序日志记录”,“ Web服务器日志记录”和“详细错误消息”,然后通过FTP进入App Service并查看日志。
This is all in Preview so you'll likely run into issues. They are updating the underlying systems very often. Some gotchas I hit:
这一切都在预览中,因此您可能会遇到问题。 他们经常更新底层系统。 我碰到的一些陷阱:
I used the Kudu "sidecar" site at mysite.scm.azurewebsite.net to get shell access to the Kudu container, but I'd like to be able to ssh into or get to access to the actual running container from the Azure Portal one day.
我在mysite上使用Kudu的“ sidecar”网站。 scm .azurewebsite.net可以对Kudu容器进行外壳访问,但是我希望有一天可以从Azure门户SSH或访问实际运行的容器。
That said, if you'd like more internal details on how this works, you can watch a session from Connect() last year with developer Nazim Lala. Thanks to James Christianson for his debugging help!
就是说,如果您想了解有关其工作方式的更多内部细节,可以观看去年与开发人员Nazim Lala进行的Connect()会议。 感谢James Christianson的调试帮助!
翻译自: https://www.hanselman.com/blog/ruby-on-rails-on-azure-app-service-web-sites-with-linux-and-ubuntu-on-windows-10