Rails 3.1应用程序部署教程

发布于 2024-12-15 17:09:17 字数 2492 浏览 1 评论 0原文

我正在寻找一个关于服务器上 Rails 3.1.1 应用程序的良好部署教程。我所说的“好”实际上是指“完整”。我发布这个问题的原因是,尽管网络上有大量教程(谷歌、博客、书籍、其他 stackoverflow 问题等...),但它们似乎都集中在部署过程的问题上或者对部署环境做出一些与我需要不匹配的假设。

我意识到在服务器上部署 Rails 应用程序需要各种不同的程序和工具,这些程序和工具需要配置,并且不知怎的,我总是陷入明显的“小”事情,这让我非常沮丧。

那么,让我们从头开始吧。我现在拥有一台可以通过 SSH 和域名访问的服务器,我们将其命名为 www.example.com。服务器运行全新的 Ubuntu 10.04 x64,并且只安装了一个用户,即 root(我通过 root 使用 SSH 访问服务器)。 笔记!没有安装 Apache、Ruby、PHP、MySQL、cPanel 或任何其他面板,只是全新安装的最低限度的面板。

此外,Web 服务器将托管单个应用程序,数据库将在同一台计算机上运行。

据我所知,部署 Rails 应用程序的过程遵循以下场景:

  • 安装 Ruby

我已经通过使用多用户安装过程使用 RVM 完成了此操作(仅仅因为我只有 root 用户,而且它确实如此)自动)。之后这似乎工作得很好,但我确实有一些问题:

直接安装 Ruby 而不是通过 RVM 更有意义(我想也许是在效率方面 - RVM 在修改一些场景的背后也做了一些魔法) bash_profile 文件以我不理解的方式,这在某种程度上似乎是侵入性的......)?

通过 RVM 作为单独用户安装是否更有意义(会出现任何安全问题)吗?

  • 必要的 gems

既然 Ruby 已经安装完毕,那么与它一起安装的最佳初始 gems 集是什么?

我只安装了捆绑程序,这样一旦我将应用程序上传到服务器上,我就可以运行捆绑安装命令,该命令将依次安装所需的应用程序 gem。

问题 - 我应该预先安装 Rails gem 吗?还有其他需要安装的gem吗?

  • 网络服务器

这对我来说是棘手的地方。我正在尝试使用 apache 和 mod-passenger 进行部署(它们似乎是最受欢迎的)。所以我安装了 apache Web 服务器和乘客 gem 以及所有相关的依赖项(乘客提到的库)。

现在,问题出现了。首先是与用户相关的。 Apache是​​如何运行的?我的意思是在每个用户下?如果我使用 root 用户安装并(重新)启动它,它会永久在 root 用户下运行吗?这是一件坏事吗?如果是,我应该创建另一个用户吗?如果是,怎么办?我应该将新用户放入哪些组中,他应该拥有什么权限(即他应该有权访问哪些文件夹)。在这种情况下如何启动apache(在root下,在该用户下,在配置文件中的某处添加一行等)

  • 网站配置

将网站配置内容放在哪里?是否可以将其放入 apache.conf,或者我应该在sites-available 中创建一个新文件?或者我应该简单地重用那里已经存在的默认文件?如果在上一步中创建了一个新用户,他应该拥有与配置文件相关的哪些权限?

另外...最终将 Rails 应用程序放在哪里?最好放在哪个文件夹中?家下面某个地方?也许在/var/www下?我想知道这会如何影响为应用程序提供服务的 apache 进程的权限? (通常我在提供应用程序时遇到问题,它抱怨它没有特定文件夹的权限。此外,使用新的资产管道 - 我不完全理解 - 正在创建资产文件,但它们似乎没有继承父文件夹权限...)

  • 数据库

作为数据库,我使用 MySQL 并且安装它非常简单。我在这里提出的问题又与用户相关。我应该为数据库使用特殊用户吗? MySQL 实际上如何管理用户(他们是内部用户,还是 Linux 用户还是……?)。数据库用户应该与上面的“Web 用户”相同吗?或者应该换一种方式?

  • 资产

在这里我真的迷失了。我真的很难让资产管道发挥作用。我已经检查了 Railscasts 剧集以及 Rails 网站教程,我似乎在理论上理解了这一点,但在实践中我遇到了问题。

所以这里的问题是 - 我应该运行哪些命令来预编译资产? (尝试运行 rake asset:precompile)。可以吗?我应该在 Production.rb 文件中更改哪些内容?生成的资产如何与上面创建的网络用户或数据库用户相关?我个人在这一步中遇到了一个问题,即日志文件说某些 css 文件无法访问(例如,我在供应商/资产文件夹下安装了 jqplot 库,并且无法访问其文件 - 我应该以某种方式在此处添加清单文件吗? ?)。

如果有人能给我指出一篇好文章或解释所有这些内容的资源,那就太好了。我遇到问题的主要领域是 Apache、Passenger、Ruby、Rails 和 MySQL 如何与 Linux 用户交互。如何正确设置Rails应用程序文件夹的权限?资产管道如何影响用户权限?

谢谢

I'm looking for a good deployment tutorial for a Rails 3.1.1 application on a server. And by good I actually mean complete. The reason I'm posting this question is that although there are tons of tutorials out there on the web (google, blogs, books, other stackoverflow questions etc...) all of them seem to focus either on a problem with the deployment process or make some assumptions about the deployment environment that do not match with what I need.

I realize that deploying a Rails app on a server requieres a variety of different programs and tools that need to be configured and somehow I always get stuck on apparently "little" things that make me very frustrated.

So, let's begin from the start. What I have now is a server that I can access through SSH and a domain name, let's call it www.example.com. The server runs a fresh Ubuntu 10.04 x64 and has just a user installed, namely root (through root I access the server with SSH).
Note! There is no Apache, Ruby, PHP, MySQL, cPanel or any other panel installed, just the bare minimum that comes with a fresh installation.

Also the web server will host a single application and the database will run on the same machine.

From my knowledge the process of deploying a Rails application follows the following scenarios:

  • Installing Ruby

I already did this by using RVM using the Multi-User install process (simply because I have just the root user and it does it automatically). This seems to work fine aftewards but I do have some questions:

Would it make more sense to install Ruby directly and not through RVM (I'm thinking maybe in terms of efficiency - also RVM does a little bit of magic behind the scences modifying some bash_profile files in ways that I don't understand and this somehow seems invasive...)?

Would it make more sense to install as a separate user through RVM (can there any safety problems arise)?

  • Necessary gems

Now that Ruby is installed what would be the best initial set of gems to install along side it?

I installed just bundler, so that once I upload my application on the server I can run a bundle install command which will install in turn the required app gems.

Question - Should I install the rails gem before hand? Are there any other gems that need to be installed?

  • Web server

This is where it gets tricky for me. I'm trying to use apache and mod-passenger for deployment (they seem to be the most popular). So I installed the apache web server and the passenger gem and all the related dependencies (libraries mentioned by passenger).

Now, the problems arise. First thing is user related. How does Apache run? I mean under each user? If I installed it and (re)started it using the root user, will it permanently run under the root user? Is this a bad thing? If yes, should I create another user? If yes, how? In what groups should I put the new user in, what rights should he have (namely what folders should he have access to). How to start the apache in this case (under root, under that user, add a line somewhere in the configuration file, etc.)

  • Website configuration

Where to put the website configuration stuff? Is it OK to put it into apache.conf, or should I create a new file in sites-available? Or should I simply reuse the default file that is already present there? If a new user has been created at the previous step, what rights should he have in relation to the config files?

Also... where to eventually put the rails application? In what folder would it be best? Somewhere under home? Maybe under /var/www? I want to know how would this affect the rights of the apache process serving the app? (Generally I have problems while serving an app, it complains that it doesn't have rights on a specific folder. Also, using the new asset pipelines - which I don't fully understand - asset files are being created and they seem not to inherit the parent folder rights...)

  • Database

As database I'm using MySQL and installing it is pretty straightforward. The question that I have here is again user related. Should I use a special user for the database? How does MySQL actually manage users (are they internal, or are they the Linux users or ...?). Should the database user be the same as the "web user" from above? Or should it be a different one?

  • Assets

Here I get really lost. I really have troubles making the assets pipeline works. I've checked the railscasts episode and also the rails website tutorial and I do seem to theoretically understand the thing, yet I have problems in practice.

So the questions here would be - what commands should I run to precompile the assets? (Trying to run rake assets:precompile). Is it ok? What should I change in the production.rb file? How do the assets generated work in relation with the webuser or database users created above? I am personally getting a problem in this step namely the log files say that some css files are not accessible (for example I have jqplot library installed under the vendor/assets folder and its files cannot be accessed - should I add a manifest file here somehow?).

It would be really great if someone could point me towards a nice article, or resource that explains all this stuff. The main area which I'm having problems in is how does Apache, Passenger, Ruby, Rails and MySQL interact with a Linux user. How to properly set up the permissions for the Rails app folder? How does the assets pipeline affect this user permission stuff?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事未完 2024-12-22 17:09:17

这是我部署 Rails 的方法:

  • 安装 Ruby

我不会使用 RVM,因为要让它与 cron 作业等内容正确运行是非常棘手的。可行(例如使用包装器),但有点麻烦。如果您在那台机器上不需要其他 Ruby 版本,只需自己从源代码安装即可。

  • 宝石

让 Bundler 发挥它的魔力吧。请记住使用标志 --without testdevelopment --deployment 进行安装。但我不会预先这样做。只要确保您有捆绑器即可。

  • Web 服务器

使用 Passenger(使用 Apache 或 Nginx)是一个不错且简单的选择。当您从 apt 安装 Apache 时,它​​将以特殊用户运行。

Apache 在 Ubuntu 上自动正确配置。它将在重新启动时启动。

  • 网站配置

所有配置都在 /etc/apache2.conf 中。将虚拟主机配置放在 /etc/apache2/sites-available 中并使用 a2ensite 启用它。

将您的应用程序放在 /var/www 中,因为这是 Ubuntu 中的默认位置。我通常创建一个部署用户。

通过将您的应用分配到 www-data 组,确保用户可以访问您的应用。

  • 数据库

MySQL有自己的用户系统。以 root 用户身份登录并使用 SQL 创建一个新用户:GRANT ALL ON 'application_product'.* TO 'deploy' IDENTIFIED BY 'some password';

  • 资产

资产应为作为拥有该应用程序的用户生成。您应该将所有 css 和 javascript 文件添加到 production.rb。例如:

config.assets.precompile += %w(backend.css)
  • 结论

使用Capistrano等部署工具会有所帮助。当您运行 capify 时,取消注释 Capfile 中的相应行以获取资产编译。这是我的:

ENV['RAILS_ENV'] = 'production'
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
load 'deploy/assets'
load 'config/deploy'
require 'capistrano/ext/multistage'
require "bundler/capistrano"
require 'capistrano_colors'

这就是我通常安装 Rails 应用程序的方式。我希望这能让你继续前进。最近,我编写了一个用于将 Chef-solo 与 Capistrano 集成的 gem,名为 capistrano-chef-solo.它还处于 alpha 阶段,如果您刚刚开始部署,可能有点太复杂,但它可能会对您有所帮助。

Here's my way of deploying Rails:

  • Installing Ruby

I would not use RVM because it's very tricky to get it running properly in combination with stuff like cron jobs. Doable (with wrappers for example), but a bit of a hassle. If you don't need other Ruby versions on that machine, just install it from source yourself.

  • Gems

Just let Bundler work its magic. Remember to install with the flags --without test development --deployment. I wouldn't do that up front though. Just make sure you have bundler.

  • Web server

Using Passenger (with either Apache or Nginx) is a fine and easy choice. When you install Apache from apt, it will run in a special user.

Apache is configured correctly automatically on Ubuntu. It will start on reboot.

  • Website configuration

All configuration be in /etc/apache2. Place your virtual host configuration in /etc/apache2/sites-available and use a2ensite to enable it.

Put your app in /var/www, since that is the default location in Ubuntu. I usually make a deploy user.

Make sure that user can access your application by assigning your app to the www-data group.

  • Database

MySQL has its own user system. Log in as root user and create a new user with SQL: GRANT ALL ON 'application_production'.* TO 'deploy' IDENTIFIED BY 'some password';

  • Assets

Assets should be generated as the user owning the application. You should add any css and javascript files to production.rb. For example:

config.assets.precompile += %w(backend.css)
  • Conclusion

It helps to use a deploy tool like Capistrano. When you run capify, uncomment the appropriate line in the Capfile to get assets compilation. Here's mine:

ENV['RAILS_ENV'] = 'production'
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
load 'deploy/assets'
load 'config/deploy'
require 'capistrano/ext/multistage'
require "bundler/capistrano"
require 'capistrano_colors'

This is just how I normally install my rails apps. I hope this will get you going. Recently I've written a gem for integrating Chef-solo with Capistrano, called capistrano-chef-solo. It's very alpha and might be a bit too complicated if you're just starting with deployment, but it might help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文