Specify which server a rails project will use - ruby-on-rails

Just wondering if this can be done. You can specify you want a new rails project to use the postgresql server ike this:
rails new my-new-rails-project -d postgresql
and that takes care of the database yaml file.
Can an option be passed in here to specify puma as the development and production server so the relevant puma.rb configuration file is created?
Something like this:
rails new my-new-rails-project -d postgresql -s puma

By default Rails are using Webrick, but you can include different gem using Gemfile.
For example, you can use Thin (or puma, unicorn, whatever...) gem to your Gemfile and install it with bundler.
gem 'thin', group :development
When you start local server, rails will boot with custom webserver
rails server
=> Booting Thin
=> Rails 4.0.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Thin web server (v1.6.1 codename Death Proof)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop

There is no option for server configuration in Rails. You can check using below command.
rails new --help

Related

Error Rails 4 console on staging or production

Join in my office the following command:
RAILS_ENV=production rails s production
And I get the following error:
/usr/local/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/handler.rb:78:in `require': cannot load such file -- rack/handler/production (LoadError)
Please, help!!
Run this command to run the rails server in production mode
rails s -e production
guessing:
RAILS_ENV=production rails s production
to:
RAILS_ENV=production rails s
Sure this is the problem of your command
RAILS_ENV=production rails s production
change it to:
RAILS_ENV=production rails s
usage of rails s command is :
Usage: rails server [mongrel, thin, etc] [options]
-p, --port=port Runs Rails on the specified port.
Default: 3000
-b, --binding=ip Binds Rails to the specified ip.
Default: 0.0.0.0
-c, --config=file Use custom rackup configuration file
-d, --daemon Make server run as a Daemon.
-u, --debugger Enable ruby-debugging for the server.
-e, --environment=name Specifies the environment to run this server under (test/development/production).
Default: development
-P, --pid=pid Specifies the PID file.
Default: tmp/pids/server.pid
-h, --help Show this help message.
so the word following rails s is expected to be server you want to run your application with i.e. (Thin, WEBrick, FastCGI, CGI, SCGI and LiteSpeed.)
rails s thin
=> Booting Thin
=> Rails 3.2.16 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
rails s WEBrick
=> Booting WEBrick
=> Rails 3.2.16 application starting in development on http://0.0.0.0:3000
=> Ctrl-C to shutdown server
It runs as local, and uses WEBrick 1.3.1 I want to run using apache + passenger + rails

My server is starting Webrick instead of Unicorm/Nginx on production

I've followed these steps in order to have a Unicorn/Nginx app running:
https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-14-04
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04
I've done everything indicated there with exception of the databases instructions (because I'm going to use Mongo). However when I execute (ip edited):
RAILS_ENV=production rails server --binding=123.456.789.01
a Webrick server is started:
/home/username/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.0.2/lib/active_support/values/time_zone.rb:282: warning: circular argument reference - now
=> Booting WEBrick
=> Rails 4.0.2 application starting in production on http://123.456.789.01:3000
=> Run `rails server -h` for more startup options
Is there something I need to check to get the production setup working fine? I don't know what I need to verify.
I believe you have skipped some steps in the tutorial you have mentioned... After you install unicorn and nginx you should be able to start them:
sudo service unicorn_appname start
sudo service nginx restart

Rails running in development thinks it's in production mode

BIt of a weird one this.
rails s
=> Booting Thin
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
But when I call
puts Rails.env
in a controller, it says
production
In rails console it correctly says development
Rails.env
=> "development"
Any ideas.
I want to run in dev locally and production on my server. Particularly important as I'm testing functionality where it costs me money to run certain api calls and I have them faked if Rails.env.prodcution? is false.
Running rails 3.2.13

`rails server puma` vs. `puma`

Some guides (example) recommend this to start one's webserver
bundle exec rails server puma
But I've always just started the server with puma directly
bundle exec puma
Does something special happening when firing up puma (or any other server) via rails server?
When you use rails s <server>, the server is launched from the Rails command and is aware of the Rails environment.
This makes possible, for example, to use any of the features and flags offered by the rails server command.
rails s --help
Usage: rails server [mongrel, thin, etc] [options]
-p, --port=port Runs Rails on the specified port.
Default: 3000
-b, --binding=ip Binds Rails to the specified ip.
Default: 0.0.0.0
-c, --config=file Use custom rackup configuration file
-d, --daemon Make server run as a Daemon.
-u, --debugger Enable ruby-debugging for the server.
-e, --environment=name Specifies the environment to run this server under (test/development/production).
Default: development
-P, --pid=pid Specifies the PID file.
Default: tmp/pids/server.pid
-h, --help Show this help message.
For instance, you can attach a debugger to the session passing --debugger or daemonize the server.
The second advantage is that you can version the Puma instance, since you will have to list the gem in the Gemfile. This is already true if you start it with bundle exec like you are doing.
Conversely, when you simply run $ puma (or $ bundle exec puma) you're not passing through the Rails system. Puma will try to find a rack bootstrap file and will use it (it works because Rails provides a config.ru script in the application root.
Generally speaking, there is no real difference if you don't need to pass specific options to the server. I like puma and I tend to use it in some projects even when on production we use Unicorn, thus running $ puma as a standalone command is handy because I don't need to add it to the Gemfile.
However, I would probably go with $ rails s puma if my entire stack uses Puma. This is also the command suggested in the documentation.

How define his own server with Rails 3

I want use always thin when I start my rails server in development mode.
By default it webrick to use. So I add thin in my Gemfile
gem 'thin', :group => 'development'
Now If I want use it to launch my server in development mode I mandatory define it.
bundle exec rails s thin
If I don't define it, it's always use webrick. So How define using thin by default ?
Instead of rails s just type :
>> thin start -p 3000
Where 3000 is the number of your port.
You can also specify an enviornment :
>> thin start -e production
Assuming you are using bundler 1.0.x and your gems is vendorized:
bundle exec vendor/ruby/1.9.1/bin/thin start

Resources