LinuxCommandLibrary

rails-server

Start the Rails application web server

TLDR

Run the web server

$ rails server
copy

Run the web server on a specified port
$ rails server [[-p|--port]] [port_number]
copy

Run the web server on a specified IP address
$ rails server [[-b|--binding]] [ip_address]
copy

Run the web server on a specified environment
$ rails server [[-e|--environment]] [environment]
copy

Display help
$ rails server [[-h|--help]]
copy

SYNOPSIS

rails server [options]
rails s [options]
Starts the Rails application server.

PARAMETERS

-p, --port=PORT
    Specifies the port the server will listen on. Default is 3000.

-b, --binding=IP
    Specifies the IP address the server will bind to. Default is 0.0.0.0 (all interfaces).

-e, --environment=ENVIRONMENT
    Defines the Rails environment (e.g., development, test, production). Default is 'development'.

-d, --daemon
    Runs the server as a daemon in the background.

-P, --pid=PIDFILE
    Specifies the PID file path. Useful with --daemon. Default is tmp/pids/server.pid.

-C, --config=FILE
    Specifies the server configuration file (e.g., for Puma, config/puma.rb).

--help
    Shows a list of available options and their descriptions.

DESCRIPTION

The rails server command is a fundamental tool for Ruby on Rails developers, used to launch a local web server that serves the Rails application over HTTP. It allows developers to view and interact with their application in a web browser during development. By default, it typically uses the Puma web server (though older Rails versions used WEBrick or Thin). When executed within a Rails project directory, it reads the application's configuration and code, making it accessible usually at http://localhost:3000. This command monitors changes to application files (in development mode), often reloading parts of the application automatically, streamlining the development feedback loop. While essential for development, rails server is generally not recommended for production environments, which typically require more robust, performance-oriented, and scalable server setups like Nginx/Puma or Passenger deployments.

CAVEATS

Not suitable for production deployments directly; dedicated web servers like Nginx/Apache combined with application servers (Puma, Passenger, Unicorn) are preferred for performance and stability.
Requires being executed within the root directory of a Ruby on Rails application.
In development mode, server restarts or reloads may be necessary for some code changes (e.g., initializer changes), though many changes are picked up automatically by tools like spring or zeitwerk.

STOPPING THE SERVER

To stop a server running in the foreground, press Ctrl+C in the terminal. If the server is running as a daemon (using -d), you'll need to kill the process using the PID found in the specified PID file (e.g., kill $(cat tmp/pids/server.pid)).

DEFAULT ACCESS

By default, the application will be accessible via a web browser at http://localhost:3000. You can change the port and binding address using the respective command-line options.

LOG OUTPUT

The server outputs logs directly to the terminal, including HTTP requests, database queries, and application-specific messages. These logs are also written to files within the log/ directory (e.g., log/development.log).

HISTORY

The rails server command has evolved from its origins as script/server in early versions of Ruby on Rails (pre-Rails 3.0). This change unified many common development tasks under the single rails executable, improving consistency and ease of use. Initially, WEBrick was the default server. Over time, as Rails applications grew in complexity and performance demands, more robust and performant servers like Thin, Unicorn, and eventually Puma gained popularity. Puma became the default web server for new Rails applications starting with Rails 5. The command's core function has remained consistent: providing a convenient way to run the Rails application locally for development and testing purposes.

SEE ALSO

rails(1): The main Ruby on Rails command-line interface., rails console(1): Starts the Rails interactive console., rails generate(1): Generates new Rails application components., puma(1): The default web server often used by rails server.

Copied to clipboard