Running more than Rails apps on a local Mac - ruby-on-rails

I would like to set up multiple Rails apps running simultaneously.
I'm using a basic installation with Rails and Mongrel. I'm pretty newbie when it comes to a server side thing.
What kind of options do I have to run multiple Rails app running at the same time on a local Mac?

The only thing that stops you from running multiple rails apps on one machine is the ports.
If you want to run several apps while developing just use script/server -p <port number> for each of the apps.
If you have a production machine set up, I would recommend you to use phusion passenger with apache or nginx, and set up different virtual machines (or ports)

If you end up using Phusion Passenger, the Passenger Preference Pane can automatically configure your Apache virtual hosts for you. It's a lot easier than editing the Apache configuration and your /etc/hosts file each time you want to set up a new application.

Generally you start rails server using webrick or mongrel like
script/server
and
mongrel_rails start
respectively, that starts your server on port 3000 by default, ie. you can access your app on localhost:3000
To have multiple rails apps running on same machine, just start the servers by going to different rails application root directories on different ports like so
script/server -p 3001
or
mongrel_rails start -p 3001
Just for info, if you want to start rails apps in different environments then just pass -e option when you start the server like so
script/server -e production
or
script/server -e test_or_anyotherenv
If you don't give -e option, then it will by default start the server in development environment.

I'm a Django (not Rails) coder, but I think you should launch the servers at different ports.

Initially, I used mongrel on different ports. Works just fine. But, as agregoire mentioned, Phusion Passenger and the Passenger PrefPane make your life so much easier. Checkout Ryan Bates's RailsCast, Passenger in Development, for a good tutorial on setting it up.

Related

Domain setting using ruby rails

I am planning to have a web application.
To do so, I studied about ruby and ruby on rails. I am using linux server from amazon clouding system.
I bought a domain from godday, and I put the IP address on DNS setting. When I run 'rails s' command, I can connect to the wep page through port 3000 in such a way that domain.com:3000. However, I cannot directly connect to domain.com. How can I my domain works without port 3000?
And Do I have to run 'rails s' every time to make the wep page work? Actually I tried to use 'rails s &' to make it run in background. But it fails. How can I make the server run even though I am not connected to the linux server?
Thank you!
usually you use rails s just in development. there are quite a few ruby web servers you can choose from for your production environment: puma, passenger or unicorn to name a few.
of course all of them have their own tutorials how to set them up. for starters, i'd go with with passenger because it's integrated with nginx and apache and easily set up.
You need to specify a port, if you don't see the port it can be either 80 (http) or 443 (https).
rails server -p 80
On linux you have to be root to bind to port less than 1000, so just append sudo in front.

rails scope root and multiple apps

I have a working rails app accessible directly from http://0.0.0.0:3000/ . The app is also in staging and production in heroku.
Today, I want to start working on a new rails app in the same computer. How can I start working on the new unrelated app under a different path without messing up my staging/production urls ?
How can I have something like this locally and switch between the two apps
http://0.0.0.0:3000/existingApp/
http://0.0.0.0:3000/newapp/
I tried scope "/existingApp" do in my routes.rb for / and I suppose I should do that for the new app as well... but how do I specify this only for my local environment? I would like my heroku urls to stay unchanged (ie stay at the root).
I wouldn't recommend doing what you're doing, but if you're gonna do it anyway, you could try writing an engine and mounting it.
This could help get you started.
Almost any server would be able to be configured to listen to a port other than 3000. Thin, for example, can be started as:
thin -R config.ru -a 127.0.0.1 -p 8080 start
And would then listen on port 8080. Rails server can be started similarly using:
rails server -e production -p 4000
You may want to also consider starting your database using a port different than the standard one, but that is probably not necessary.

Setting up multiple rails apps using nginx and Puma

I have a web server serving multiple Rails applications using a combination of nginx and Passenger. This is pretty straightforward, because the Passenger install sets-up pretty much everything you need to connect to nginx.
I found "Rails app with Puma" that seems to explain how to set up nginx and Puma together. How would this configuration need to be modified in order to serve a second Rails application on the same server?
Also, this guide doesn't say anything about restarting the application automatically if there is a system reboot or some other issue. Is there a way to do that? The nginx + Passenger combo seems to do it by default.

Rails 4 Change port number only for production environment

I found this question before:
How to change Rails 3 server default port in develoment?
However what I really want is to change my port number for the production environment only. I am using RoR 4. It would be very nice if I could type something on production.rbin config/environments. Is there a way to do that?
The answer in brief
The rails stack has your application and then the server that runs your application (aka the "application server"). This server could be webrick (not a good idea in production), thin, gunicorn, passenger etc etc.
You should be telling that server which port to run under. You'll (likely) need to specify this outside Rails - not in config/production.rb because by the time Rails boots it's already running inside some application server.
A deeper dive with an example:
Let's use Heroku for an example, because port numbers there are essentially randomized (at least from the view of us looking in).
Heroku will pick a random port for us, then tell us through the PORT environmental variable. With Heroku you need a Procfile to tell it what services to launch, and your Procfile may look something like this:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
See here, we use -p $PORT to tell unicorn - our application server in this example - to run on some port Heroku gave us.
Back to your question:
However you kick off your application serving process in your production environment, you should tell it to specify the port number to your web server. There's a bunch of ways to kick off your application serving process at a production level: from upstart (built into Ubuntu), to supervisord to god... all these methods run commands and make sure the process stays up (an important part of production level deployment ;) )

How practical is it to run a Rails server with Apache or Nginx even in development mode?

Since WEBrick or Mongrel might be limited to not having keep-alive etc, can't Apache or Nginx be used with Rails even in development mode?
For example, running Apache there always, and attach Rails to it, so that starting Rails meaning starting the Rails part, and no webserver such as WEBrick or Mongrel need to be started. Is that feasible? On some Mac, even Apache is running all the time when the system Settings has the "Web sharing" selected.
You can use Apache or nginx or any server of your choice for any environment you wish to run. But you can't just start it with rails server.
As described in this post, you just need to set the right option in your httpd.conf, in the section that serves your rails:
## Specify Rails Environment here, default value is "production"
RailsEnv development

Resources