many instances of apache get spawned with Passenger/Rails - ruby-on-rails

I have a Debian Linux VPS server for my production website (512MB).
I'm using Phusion Passenger with Apache to service my Rails 2.3.4 application with Ruby 1.9. I'm limiting the pool of Phusion passenger instances to 3
Although the traffic is relatively low, the server crashes at times and I notice (when using 'top' command) that there are many instances of apache (/usr/sbin/apache2 -k start) maybe like 20 of them taking up all the memory I have and the website become un-responsive.
I'm not sure what to do about this, where to start digging for potential issues or how to spot or limit the number of apache instances.
Thanks,
Tam

If you want to limit the number of Apache processes, look at the documentation for the Multi-Processing Modules. But if you don't have that much traffic (it depends on what you call "relatively low"), it should work out of the box.
Have you tried asking your question over at Server Fault ? You might get better answers there.

Related

Which one of these is a better option to use alongside "latest rails" application? Mongrel, Thin, WEBrick and Passenger

I have been playing around and evaluating other options to rails' default WEBrick server and Thin was the most painless and clean thing which worked very well!!
which one of these Mongrel, Thin, WEBrick and Passenger would you recommend and why..?
are there any scalability perks(cluster friendly or configs which can handle some sort of scale) which come with any of these servers.. or should scalability even be a parameter while evaluating them?
Dipak already answered half of your question but let me clarify on things a little bit. (I am one of the Phusion Passenger authors.)
WEBrick is a toy web server. Nobody uses it in anything but development because it performs poorly and is said to leak memory.
You said Thin worked well. Have you already set it up in a reverse proxy configuration? Because that's what people do in production scenarios. It is not safe to expose Thin (or Mongrel, or Unicorn) directly to the Internet.
You may be interested in reading Ruby on Rails server options and the Phusion Passenger architectural overview for more etailed explanations.
When it comes to scalability, there's not much difference. They all perform very similar in production, they all scale in about the same way, and any problems you encounter will most likely to be caused by your app or by Rails. Well, except for WEBrick, which you really shouldn't use in production. You may see difference in hello world benchmarks, but that will be all. In production use most of the time will be spent in the app so any minor speed differences visible in hello world benchmarks will become completely invisible.
There are some subtleties to be aware of though.
Phusion Passenger provides a feature called global queuing. It solves a specific problem, explained in detail in the manual. By default Nginx and Apache proxies requests in a round-robin manner so they suffer from this problem, while Phusion Passenger does not. There are ways to get around this when not using Phusion Passenger but they require specific configuration or the installation of additional web server modules.
The I/O model may or may not be important depending on the nature of your application. Mongrel, Thin, Unicorn, they are all multi-process single-threaded. This works great for traditional web apps which looks up stuff in the local database and renders something, but sucks majorly for apps which perform a lot of HTTP API calls or otherwise have to wait a lot on I/O. Why Rails 4 Live Streaming is a Big Deal explains this in detail.
Phusion Passenger is also multi-process single-threaded, but Phusion Passenger Enterprise supports multithreading. Phusion Passenger Enterprise is a commercial variant of the open source Phusion Passenger, with a variety of features useful for large-scale production environments.
In large production environments, some features become important, e.g. rolling restarts, not showing anything wrong when a deployment failed, etc. Mongrel, Thin, Unicorn, Phusion Passenger, they all expose these features to some degree, but some require more admin effort than others. For example to implement rolling restarts in Mongrel and Thin, you need quite a lot of steps in your deployment scripts. Unicorn doesn't require as many steps, but still significantly. This is where Phusion Passenger Enterprise shines: it takes all these features and turn them into a single config option. Turn the option on and the software takes care of the rest.
So, pick whatever option you think is best for your scenario.
The easiest to set up for production will probably be Apache and mod_rails (passenger). If you want to be using the new hotness, you could give nginx and passenger a whirl.
For development mongrel is usually the easiest to work with. Most Windows IDE's (RadRails, Netbeans) give you the choice to use Webrick or Mongrel for development work and let you control the servers from the IDE itself.
Update
Four Choices
There are really four choices, well, plus WEBrick, but that would be an unusual choice for a production server. Approximately in order of increasing complexity...
nginx + Mongrel
nginx + Passenger
Apache + Mongrel
Apache + Passenger
(There is Phusion Passenger Standalone, but that's really an nginx + passenger compiled together, so I'm not counting it, although it may be a good option for some people.)
A larger site may then add specialized layer 7 hardware (NetScaler, F5, ...) in front of the servers.
Thin is as easy as gem 'thin' for development and production

Can I run Python Django and Ruby on the same Apache server?

I'm runnig Python Django on Apache2 with ModWSGI and I would like to run Ruby on Rails on the same server.
Is it possible do this?
I read that Passenger for Ruby on Rails can support Django too.
Any help is welcome.
Thanks.
Yes, I've done it. I would not suggest doing it for a server that is busy or prone to load spikes because of the way these modules work, they embed code into the server process itself (though mod_wsgi is better than mod_python for this).
When I do HEAD http://my.dev.server the response headers include this:
Server: Apache/2.2.8 (Ubuntu) DAV/2 Phusion_Passenger/2.2.15 PHP/5.2.4-2ubuntu5.10 with Suhosin-Patch mod_wsgi/1.3 Python/2.5.2
For example, if you have 40 apache child processes adding mod_wsgi may add a couple megs of RAM to each, so used RAM may go up by 100MB. Adding phusion passenger made cause each child to use another 5 MB of RAM increasing it by 200MB more.
However, if you have 10 apache child processes this is not a huge deal.
If you are on a server that gets these busy periods then one way to achieve better results is to use nginx instead of Apache (or fastcgi with apache). It does not embed any of the application code into the http process.

Deploy Rails app to EC2

My setup: Rails 2.3.10, Ruby 1.8.7 on Windows
The last time I deployed a Rails app from Windows to Linux on Slicehost, I used Capistrano, Nginx, Mongrel, and SVN. That was 3 years ago, fast forward to now, I'm still on Windows for development and is now looking to deploy to EC2. A quick search turns up tools like Rubber and Chef which aren't easy to grasp with a quick read. It seems like Rubber and Chef are designed for multi-EC2 instances deployment which will be useful when I need to scale.
I'm also new to Passenger but it seems to be the default way to deploy Rails app nowadays, one thing that isn't so clear to me is whether Passenger is a replacement for Mongrel? In my old setup, I configured Nginx to forward the Rails requests to a cluster of Mongrel processes but I don't see anything like that for Passenger.
Any insights are much appreciated.
We use something like what you're describing for our production server: EC2 + Apache + Passenger. We haven't had any need to use the fancy deployment tools you describe - plain old Capistrano (plus capistrano-ext so we can use it for multiple environments) does the job just fine. I've looked at Rubber (not Chef), but deemed it needlessly automagical and too poorly documented, and I'm really not sure what it offers that can't be done just as well with roles in Capistrano.
Passenger has been great. It's an "overseer" that manages a collection of Mongrel-like workers (I had thought that the workers were Mongrels, but upon further reading, I don't think they are. The Passenger comparisons page even compares its RPS to a Mongrel cluster, so...), starting them up as needed, culling them under low loads, restarting them if they crash, etc. It's actually very similar to the Server + Mongrel Cluster you described, but probably a bit better, as Passenger has an understanding of the underlying workers that Nginx / Apache don't. And you'll have to make a few minor tweaks to get Capistrano playing nicely with Passenger.
And if possible, pair Passenger with Ruby Enterprise Edition (from the same guys who made Passenger). It's a much faster version of Ruby, mostly due to a rewritten, configurable garbage collector. You'll have to tune your GC settings to get the most out of it.
Hope this helps!
Both might help:
http://ginzametrics.com/deploy-rails-app-to-ec2-with-rubber.html
Hosting rails on ec2
Rubystack allows you to have the same Rails environment for development on Windows and for deployment on Linux. We also have EC2 images (scroll to the bottom) and it is completely free, so you may want to give it a try.
Also, this may not work for you, but depending on your requirements, you may want to go for a PaaS solution like Heroku

What can I use to host a Rails site on Windows?

Okay, before you guys go nuts -- this is just a small site, temporary setup. Right now I'm having some internal folks remote into the server and use the site through webrick via the dev command: ruby script/server. Not exactly ideal.
I'm just starting Rails dev and I want to know a better way to handle hosting on a Windows Pro box. Again, just temporary so please be gentle :)
As far as I know, mod_rails isn't an alternative.
Mongrel plays very nice on windows, though, so you can set up a few mongrels and have IIS or Apache proxy to them. Or just use Mongrel directly. Before mod_rails, mongrel was the de facto way to deploy on any platform, so it's a very viable choice.
The one time I was forced to deploy on Windows, however, I installed Ubuntu via virtualbox (could also use VMWare or whatever, of course) and deployed on that. Works like a dream, and I got to work with a sensible OS. Phew. SSH and stuff. Can't live without it. Remote desktop isn't exactly my kind of thing.
Your best bet is to setup a Mongrel cluster. Mongrel is an application server which can serve a Rails application on HTTP. But a single Mongrel instance can only handle 1 request at the same time, so typically people run a cluster of Mongrels, i.e. multiple Mongrel instances. These Mongrel instances do not talk to the Internet directly. Instead, they are put behind a load balancer or a web server, which proxies requests to this cluster of Mongrels. If you use Apache on Windows then you can:
1. Setup and start a cluster of Mongrels, each listening on its own port.
2. Setup a virtual host with some mod_proxy_balancer directives, with which you tell mod_proxy_balancer to proxy all requests to the Mongrel cluster. mod_proxy_balancer will automatically distribute the load between the Mongrels.
If usage is really low i.e. likely to be mostly 1 person at a time or your response time is really low then you can get away with a single mongrel and having your users point there browser to the relevent IP address and port.
For some time before I finally got my apps migrated to our corporate Linux/apache "cloud" (which was anything but straightforward, for mostly internal IT-related reasons) I ran two apps on a workstation, using a separate mongrel (different ports) for each. It worked well enough to be useful for almost a year.
These days (well, about three weeks now) I've substituted the immediacy, control - and vulnerability - of local (under my desk) access for the stability of five servers, each with multiple mongrels, staging areas, and deployment annoyances. Swings and roundabouts.

When to use Mongrel as web server for Rails application?

When I started my first bigger Rails application, I chose to use Apache (with Passenger), because I already had some experience with it with PHP, but I don't have any deep knowledge of deploying Rails applications.
How would one choose web server for a Rails app?
Are there any main differences between Apache and Mongrel?
Or is it just a matter of taste?
As you know, two popular setups are:
Apache + passenger
Apache + mongrel (Apache sending requests to your mongrel cluster)
Yes, It is a matter of taste.
You made the good choice with Passenger for your first app as it is far easier to setup.
It is the #1 recommended option on ROR official Website.
Performance is also very good.
Using mongrel is still a good option but it is harder to setup and administrate.
You will need to make sure you are running your rails app against a cluster of mongrel and not only one mongrel instance (for performance/concurrent request reason). Meaning adminstration of your apache + administration of your mongrels.
There is no reason to use mongrel.
The reason you see lots of sites/tutorials using mongrel is.. it was a good choice before Passenger.
Passenger is better on the performance side, dead easy to install and no (for the 90% cases) managing is required.
You can also do Nginx + Mongrel cluster. (I don't recommend it though.) Go with Phusion Passenger. It's pretty easy to setup and it's fast. No reason not to use it.
I use Passenger with Nginx. Besides just the web server, also look into using Ruby Enterprise Edition. It's going to save you a good amount of memory. ~30%. Ruby Enterprise Edition.

Resources