如何启动Rails服务器?

35

我正在开发一个使用rails 2.3.2的应用程序。

当我输入命令"rails script/server"时,我得到了以下输出,而不是启动服务器,为什么?

rails script/server
Usage:
  rails new APP_PATH [options]

Options:
  -J, [--skip-javascript]        # Skip JavaScript files
      [--dev]                    # Setup the application with Gemfile pointing to your Rails checkout
      [--edge]                   # Setup the application with Gemfile pointing to Rails repository
  -G, [--skip-git]               # Skip Git ignores and keeps
  -m, [--template=TEMPLATE]      # Path to an application template (can be a filesystem path or URL)
  -b, [--builder=BUILDER]        # Path to a application builder (can be a filesystem path or URL)
      [--old-style-hash]         # Force using old style hash (:foo => 'bar') on Ruby >= 1.9
      [--skip-gemfile]           # Don't create a Gemfile
  -d, [--database=DATABASE]      # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
                                 # Default: sqlite3
  -O, [--skip-active-record]     # Skip Active Record files
      [--skip-bundle]            # Don't run bundle install
  -T, [--skip-test-unit]         # Skip Test::Unit files
  -S, [--skip-sprockets]         # Skip Sprockets files
  -r, [--ruby=PATH]              # Path to the Ruby binary of your choice
                                 # Default: /home/xichen/.rvm/rubies/ruby-1.8.7-p352/bin/ruby
  -j, [--javascript=JAVASCRIPT]  # Preconfigure for selected JavaScript library
                                 # Default: jquery

Runtime options:
  -q, [--quiet]    # Supress status output
  -s, [--skip]     # Skip files that already exist
  -f, [--force]    # Overwrite files that already exist
  -p, [--pretend]  # Run but do not make any changes

Rails options:
  -h, [--help]     # Show this help message and quit
  -v, [--version]  # Show Rails version number and quit

Description:
    The 'rails new' command creates a new Rails application with a default
    directory structure and configuration at the path you specify.

Example:
    rails new ~/Code/Ruby/weblog

    This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
    See the README in the newly created application to get going.

当我输入 Linux 命令 "ls" 后,我会看到以下目录和文件显示:
app  Capfile  config  criptq  db  doc  features  Gemfile  Gemfile.lock  generate  lib  log  nbproject  public  Rakefile  README  script  spec  test  tmp  vendor

我的Gemfile如下:

source "http://rubygems.org"

gem "rails", "2.3.2"
gem "mysql", "2.8.1"
gem "fastercsv"
gem "will_paginate", "2.3.16"
gem "chronic", "0.6.4"
gem "whenever", "0.4.1"
gem "searchlogic", "2.4.28"

group :development do
  gem "mongrel", "1.1.5"
end

group :test do
  gem "rspec", "1.3.2"
  gem "rspec-rails", "1.3.4"
  gem "factory_girl", "1.3.3"
end

4
我猜你在输入这个时不在你的应用程序文件夹里。 - apneadiving
你的意思是我应该进入“app”文件夹吗?我尝试了,但结果还是一样的。 - Leem
不,在你的 Rails 应用程序根目录下。 - apneadiving
@apneadiving,正如您在上面看到的那样,“ls”命令的输出显示我正在我的Rails应用程序的根目录中。 - Leem
@Nicklasos,是的,“ruby script/server”和“./script/server start”都可以工作 :) - Leem
显示剩余2条评论
17个回答

29
在Rails 2.3应用中,只需运行./script/server start即可。

1
"ruby script/server" 和 "./script/server start" 两者都可以正常运行 :) - Leem
开始运行WEBrick在端口3000或其他端口,您可以通过添加“-p 8088”等来更改。 - Volker Pacher
1
在Ubuntu/Debian中,只需运行./script/server - 在BeagleBone Black上进行了测试。 - TekuConcept
$ ./script/server start sh: vendor/plugins/blackbox_services/script/start_thin.rb: 没有那个文件或目录 - A H K

24

对于Rails 3.2.3和最新版本的Rails,您可以通过以下方式启动服务器:
首先使用命令安装所有宝石: bundle installbundle.
然后将您的数据库配置到database.yml文件中。
创建新的数据库: rake db:create
然后启动Rails服务器。
rails serverrails s


在安装所有缺失的 gem(Linux)之前,这是行不通的:bundle install - TekuConcept

6

对于 Rails 2.3.2 版本,您可以通过以下方式启动服务器:

ruby script/server

6

在启动服务器时,请确保您位于正确的目录中

sites>yoursite> rails s


6
在 Rails 2.3.x 应用程序中,您可以通过以下命令启动服务器: ruby script/server 在 Rails 3.x 中,您需要使用以下命令: rails s

3
对于Rails 4.1.4,您可以启动服务器: $ bin/rails server

3
在Rails 3中,最简单的方法是使用rails s。在Rails 2中,您可以使用./script/server start
您还可以使用其他服务器,如thinunicorn,它们也提供更高的性能。
我使用unicorn,在终端中可以轻松启动它,只需输入unicorn_rails
顺便说一句,如果您使用其他工具,如worker(sidekiq、resque等),我强烈建议您使用foreman,这样您就可以在一个终端窗口中用一个命令启动所有作业,并获得统一的日志。

3
前往Rails项目的根目录
  • 在Rails 2.x中运行 > ruby script/server
  • 在Rails 3.x中使用 > rails s

2

对于最新版本的Rails(Rails 5.1.4,发布于2017年9月7日),您需要按以下方式启动Rails服务器:

hello_world_rails_project$ ./bin/rails server

=> Booting Puma
=> Rails 5.1.4 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.10.0 (ruby 2.4.2-p198), codename: Russell's Teapot
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000

更多帮助信息:

hello_world_rails_project$ ./bin/rails --help

The most common rails commands are:
generate     Generate new code (short-cut alias: "g")
console      Start the Rails console (short-cut alias: "c")
server       Start the Rails server (short-cut alias: "s")
test         Run tests except system tests (short-cut alias: "t")
test:system  Run system tests
dbconsole    Start a console for the database specified in 
config/database.yml
             (short-cut alias: "db")

new          Create a new Rails application. "rails new my_app" creates a
             new application called MyApp in "./my_app"

2

最新版 Rails 的情况

如果你在使用 rails s 命令时遇到了问题,有时候终端会出错。

这时你应该尝试使用以下命令:

./bin/rails

访问命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接