将 Rails 应用程序更改为生产

将 Rails 应用程序更改为生产

问题描述:

如何更改我的 Rails 应用程序以在生产模式下运行?是否有配置文件(例如 environment.rb)来执行此操作?

How can I change my Rails application to run in production mode? Is there a config file, environment.rb for example, to do that?

如何使用 Apache 和 Phusion Passenger 在生产模式下(分步)设置和运行 Rails 4 应用程序:

How to setup and run a Rails 4 app in Production mode (step-by-step) using Apache and Phusion Passenger:

通常,您可以进入您的 Rails 项目 rails s,并在 http://something.com:3000.生产模式的配置有点棘手.

Normally you would be able to enter your Rails project, rails s, and get a development version of your app at http://something.com:3000. Production mode is a little trickier to configure.

我一直在搞这个,所以我想我会为新手(比如我自己)写这篇文章.有一些小的调整在整个互联网上传播,并认为这可能更容易.

I've been messing around with this for a while, so I figured I'd write this up for the newbies (such as myself). There are a few little tweaks which are spread throughout the internet and figured this might be easier.

  1. 请参阅本指南以了解服务器的核心设置(CentOS 6,但它应该适用于几乎所有 Linux 版本):https://www.digitalocean.com/community/tutorials/how-to-setup-a-rails-4-app-with-apache-and-passenger-on-centos-6

确保在设置Passenger 之后您已经编辑了/etc/httpd/conf/httpd.conf 文件以反映您的目录结构.您想将 DocumentRoot 指向您的 Rails 项目/public 文件夹 httpd.conf 文件中具有此类目录的任何位置:/var/www/html/your_application/public 需要更新,否则一切都会变得非常令人沮丧.我对此再怎么强调也不为过.

Make absolute certain that after Passenger is set up you've edited the /etc/httpd/conf/httpd.conf file to reflect your directory structure. You want to point DocumentRoot to your Rails project /public folder Anywhere in the httpd.conf file that has this sort of dir: /var/www/html/your_application/public needs to be updated or everything will get very frustrating. I cannot stress this enough.

重启服务器(或至少 Apache - service httpd restart )

Reboot the server (or Apache at the very least - service httpd restart )

进入您的 Rails 项目文件夹 /var/www/html/your_application 并使用 rake db:migrate 开始迁移.确保数据库表存在,即使您计划稍后添加表(这也是步骤 1 的一部分).

Enter your Rails project folder /var/www/html/your_application and start the migration with rake db:migrate. Make certain that a database table exists, even if you plan on adding tables later (this is also part of step 1).

RAILS_ENV=production rake secret - 这将创建一个 secret_key,您可以将其添加到 config/secrets.yml .您可以将其复制/粘贴到 config/secrets.yml 中,以使事情运行起来,尽管我建议您不要这样做.就我个人而言,我执行此步骤是为了确保其他一切正常,然后再将其改回并在稍后获取.

RAILS_ENV=production rake secret - this will create a secret_key that you can add to config/secrets.yml . You can copy/paste this into config/secrets.yml for the sake of getting things running, although I'd recommend you don't do this. Personally, I do this step to make sure everything else is working, then change it back and source it later.

RAILS_ENV=production rake db:migrate

RAILS_ENV=production rake assets:precompile 如果您提供静态资源.这会将 js、css、图像文件推送到 /public 文件夹中.

RAILS_ENV=production rake assets:precompile if you are serving static assets. This will push js, css, image files into the /public folder.

RAILS_ENV=生产导轨 s

此时您的应用应该可以在 http://something.com/whatever 而不是 :3000 上使用.如果没有,passenger-memory-stats 并查看是否有类似 908 469.7 MB 90.9 MB Passenger RackApp:/var/www/html/projectname

At this point your app should be available at http://something.com/whatever instead of :3000. If not, passenger-memory-stats and see if there an entry like 908 469.7 MB 90.9 MB Passenger RackApp: /var/www/html/projectname

我可能错过了一些令人发指的事情,但这在过去对我有用.

I've probably missed something heinous, but this has worked for me in the past.