How do I setup my Rails Apps to Run through Phusion Passenger? - ruby-on-rails

I have the following doubt:
I have an application in Rails+MySQL and I want to run this with Apache + Passenger, I have both installed, but when I run ./script/server my app starts running with WebRick, how do I change it for work with Passenger and Apache?
P.S: I'm on Ubuntu 9.04 Jaunty Jackalope, please consider the fact I'm a newbie =)

If passenger is installed (and the module is in apache), all you need to do is point an Apache VHost's DocumentRoot at your public directory. Passenger should take care of the rest.
There is no separate server to start, the app runs direct from Apache (just like mod_php, but without the cooties).
A more detailed explanation is available here.

You need to put it wherever you told Apache to serve it from. script/server will no longer be involved.

Passenger is more of an always-on type of setup. You don't use script/server to start it, because it automatically shuts down if you're not actively using it, and reactivates when you start using it again. I personally have an /etc/hosts entry that maps myapp.local to 127.0.0.1, and then I use Apache's virtual hosts to identify which Rails app to start.
<VirtualHost *:80>
ServerName myapp.local
DocumentRoot /apps/myapp/public
RailsEnv development
</VirtualHost>

If you're running multiple apps on your own box (i.e. a development box) a friend of mine has got a gem that will help with the /etc/hosts writing called Ghost.
You can use it like:
sudo ghost add domain.local
And now in your browser typing domain.local will point to 127.0.0.1.

Related

Deploying Ruby on Rails App using Passenger & Apache

I am trying to deploy my Ruby on Rails application using Phusion Passenger and the Apache2 module on MacOS. I believe I have installed all dependencies and gems correctly and think a problem lies in my configuration files. I do not have a domain so I am trying to use localhost to deploy.
this first file is the configuration for the apache2 web server and is located in /private/etc/apache2/httpd.conf I think my issue is with the ServerName. I do not have a domain to publicly host this web application from yet. I am trying to locally deploy it first. I have been using localhost:3000 for my development and want to deploy it using that as well. Maybe I am not understanding something vital here with deploying and ServerName.
httpd.conf
<VirtualHost *:80>
ServerName localhost.depot
DocumentRoot /Users/rubythree/Documents/agile-web-development-with-rails-5.1/production/depot/public
SetEnv SECRET_KEY_BASE "secret_key_here"
<Directory /Users/rubythree/Documents/agile-web-development-with-rails-5.1/production/depot/public>
AllowOverride all
Options -MultiViews
Require all granted
</Directory>
</VirtualHost>
the next file where an issue might be is the hosts file located in /private/etc/hosts where I added the following line...
hosts
127.1.1.1 localhost.depot
I am new to Phusion Passenger and Apache and am pretty sure I am missing something in my configuration files here. When i try to access the ip 127.1.1.1 after a while of loading the browser will not be able to reach the server. running curl 127.1.1.1 will not return anything either, it will timeout. Any help or direction is appreciated :)
Even if ServerName does not match - apache will serve the first virtualhost as default.
"This machine" address is 127.0.0.1, also make sure apache is actually running and has no errors in logs

OS X: Development & Production Deployment for RoR with Apache and Passenger

My head is about to explode from the mangled mess as a result of the following few days trying to setup a development environment for Rails, Apache and Passenger.
The questions I have are:
Do you NEED passenger for a development environment? Can I just develop with pow.cx instead? - I am 99.99% sure the answer is no (you don't use passenger for development), but I need confirmation since I am deeply confused now.
When I deploy, I only use Passenger for that, correct? I.e. I don't ever touch passenger until I deploy.
Is my development environment correct?
Production deployment is simply moving a rails application under the effects of Passenger coupled with an Apache VHOST?
Background (I suggest you read):
It seems that all the information on the web is concerned about explaining things for people who already know what they are doing, rather than explaining in detail how things work it's just a series of installation steps and that has left me extremely confused on the role of things, and how to setup a development environment and deploy a RoR application correctly - so please bear with this long question.
For the past 3 days I have been trying to setup a development environment on my Macbook Pro that isn't destroyed by Apple's rediculous limits on Apache installations. I installed a custom Apache install (from bitnami using their ruby stack, since I refuse to use Server.app) so that I can run Apache and upgrade things like PHP to 5.5 easily, and that works fine.
I am trying to get into RoR but so far it has been a struggle, and I am about ready to give up.
I understand you need Apache to serve Rails applications so that the server can handle requests concurrently rather than one at a time, and that various interfaces for this exist like Thin or whatever; Passenger was highly recommended.
I installed Passenger via their instructions and did some hackery to compile it for the Bitnami passenger installation, rather than the default Apache on Mac OS X - and it's working. When I start apache and run: passenger-memory-stats I get results expected from the installation guide, so that tells me passenger is running.
However, when I try and deploy a simple hello world Rails application I get a slew of "We're sorry…" or no result at all and just a blank page.
I am fairly sure my development environment is correct, everything works except this last bit. I can picture development taking place on a pow.cx server, and once deployment is ready you simply copy the Rails application and configure Apache's VHOST to point to your ready-to-deploy app while Passenger handles the rest, is that correct?
I am using PostgresSQL via the Postgress.app, the server works fine and I can connect to it.
I have gem 'pg' in my Gemfile.
I have already read, and tried every conceivable solution from the following SO questions, but I either get no result or empty logs which is… infuriating to say the least:
We're sorry, but something went wrong. - with Rails, Apache, Passenger
Ruby on Rails: How can i edit database.yml for postgresql?
How do I set up the database.yml file in Rails?
https://www.ruby-forum.com/topic/187128
So with all that said, I am trying to deploy this hello world application (which works on a standard rails server) using the following:
INVOKING APPLICATION VIA:
http://dmarket.local:8081/
VHOSTS:
<VirtualHost *:8081>
PassengerEnabled on
RailsEnv production
ErrorLog /Applications/rubystack/apache2/htdocs/helloworld/project_error.log
CustomLog /Applications/rubystack/apache2/htdocs/helloworld/project_error.log combined
ServerName dmarket.local:8081
ServerAlias www.dmarket.local:8081
DocumentRoot "/Applications/rubystack/apache2/htdocs/helloworld/public"
PassengerPreStart http://dmarket.local:8081
<Directory "/Applications/rubystack/apache2/htdocs/helloworld/public">
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
HOSTS FILE:
127.0.0.1 dmarket.local
127.0.0.1 www.dmarket.local
DATABASE.YML (same for development, test, and production):
adapter: postgresql
encoding: unicode
host: 127.0.0.1
port: 5432
database: tsujp
pool: 5
username: tsujp
password:
A summary of answers to your questions
You don't need Passenger in development. You can develop with Pow, and deploy with Passenger.
But you can use Passenger in development if you want to. It is a good idea to use Passenger in development because that way your development environment will match your production environment more, which reduces the risk of running into unexpected problems when you deploy.
Using Passenger in development is very easy. Use it's Standalone mode, and run passenger start instead of rails server.
Pow is strictly a development-only server. The authors recommend against using it in production.
When you deploy, you touch Passenger. You don't have to touch Passenger until deployment time, but you may.
Production deployment is indeed moving an application under the effects of Passenger, and setting up a virtual host. You will of course also need to install gems (bundle install) setup the database (editing config/database.yml), running database migrations (bundle exec rake db:migrate), etc.
I've also posted updates on the posts that you linked to, in order to make life easier for people who happened to have found those posts via search.
Apache vs Nginx
You will find a lot of people recommending Nginx (e.g. Sergio just did). I second that recommendation. Nginx is faster than Apache, handles slow clients better and is generally easier to use.
Passenger works great with Nginx. It has an Nginx integration mode that is just as easy as the Apache mode. Sergio suggested Nginx + Unicorn or Nginx + Puma, but Nginx + Passenger (which replaces Unicorn/Puma) is much easier to setup, performs great, uses less memory, works better and has more features. Nginx + Unicorn requires a lot of configuration, process management using init scripts, etc.
But this is just a recommendation. You don't have to use Nginx. Sticking with Apache + Passenger is fine. Apache works well enough for most people.
Regarding your Passenger problems
However, when I try and deploy a simple hello world Rails application I get a slew of "We're sorry…" or no result at all and just a blank page.
Whenever you get an unexpected error, the first thing you should do is to read the log files. There are two log files that are important to you:
The web server error log, typically /var/log/apache/error.log. This log file contains:
Phusion Passenger error messages.
Everything that the Rails application writes to STDERR. This typically consists of errors that Rails encounters during startup (but not errors that it encounters when it's handling requests).
The Rails development log (or production log, in case you're running in production), log/development.log (or log/production.log). When an error occurs during request handling, it is typically logged here. This file does not contain errors that Rails encounters during startup.
The error messages will often tell you what the problem is and how to solve it.
This tip can also be found in the Phusion Passenger manual, Troubleshooting section.
Capistrano
Sergio recommended Capistrano. I second that recommendation. You should remember that Capistrano complements Passenger; it does not replace Passenger. Capistrano is a tool for automating tasks. Do you currently create a tarball of your app and scp it to your server, and extract it there? Well, Capistrano automates this sort of thing for you.
For more information about how all the different pieces of the stack fit together (Apache, Passenger, Capistrano, HAProxy, Chef, etc), check out the section "The big picture" on the Phusion Passenger documentation page.
Recommendation summary
Use passenger start in development. It is by far the easiest to get started with. You don't have to edit any configuration files, it works immediately.
Use Phusion Passenger for Nginx in production.
You don't need Passenger in development. In fact, in development mode you don't need even apache. You can use built-in Webrick server ($ rails server) to run your app. And yes. Pow is a good tool, I use it all the time.
In production there are also multiple options. One of them is Apache+Passenger, yes. But you need to put Nginx in front of those (because Apache doesn't handle slow clients very well). If you have nginx, then you can replace apache+passenger with something else. For a long time I've been using Unicorn (ruby web server from github). Now my current favourite is Puma. It uses less resources than unicorn, but has more requirements to your code (it better be thread-safe, because puma is a threaded server).
Now, to the development-production discrepancy: it is known that development should resemble production as closely as possible, because it minimizes risks when deploying. So, my suggestion is: use unicorn everywhere (both development and production). Only on production put nginx in front of it.
Also,
for actually performing deploys, look into Capistrano. It became industry standard for deploying rails apps (but it can also deploy PHP, static files and what have you).

Configuring an EC2 instance as a space to serve up all my Rails\WordPress projects

How do I configure an Amazon EC2 instance as a personal space to serve up whatever Ruby on Rails or WordPress projects I want, with their own domains? As a developer, I've found surprisingly little information on how to create your own "bucket" for all your various web-based projects.
It's surprisingly easy to get started but there are dozens of different ways to go about it. For the sake of giving you relevant resources I'm going to make a few assumptions about your setup and try to paint with broad strokes:
You're running a Debian-based Linux distribution (Ubuntu is popular and has a great community)
You're running a LAMP (Linux, Apache, MySQL, and PHP) stack (see Installing LAMP Server in the Ubuntu documentation).
You'll be deploying your sites to the default server location of /var/www
The first thing you'll want to do is read up on virtual hosts (vhosts). A common pattern is to put virtual hosts' web roots in subdirectories of /var/www/vhosts/ (e.g. /var/www/vhosts/example.com/), though these can technically live anywhere on the server.
After creating your document roots you'll give each site each a virtual host file in /etc/apache2/sites-available that looks (at a very high/rudimentary level) something like this:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/vhosts/example.com/
</VirtualHost>
This tells Apache to listen for requests for www.example.com and route them to /var/www/vhosts/example.com. There are a whole mess of Apache vhost configuration options, I'd recommend a Google search for some more specific examples though looking at the 000-default vhost that comes bundled with Apache on Ubuntu is a good place to start.
When you're ready to activate the vhost you'll need to enable the virtual host and reload/restart Apache:
$: a2ensite {your vhost file name} # Creates a symlink to this vhost file in /etc/apache2/sites-enabled/
$: service apache2 restart # restart Apache
Since you want to point different domains to your projects you'll simply need to create a DNS record (A-records will be the easiest unless you're dealing with all sorts of shifting IP addresses) and point it to the IP address of your server. Apache will route the domain to the appropriate virtual host.
For Rails projects the web root should be the public/ directory, so your DocumentRoot directive will be DocumentRoot /var/www/vhosts/example.com/public. You'll also need to be running a Ruby-compatible server for Rails apps, Phusion Passenger, Thin, and Unicorn are all pretty popular.

Stable and fast configuration of Web Server to run Rails like apache + php on Windows

I'm looking for a good configuration of rails + a webserver. Actually I'm using webrick which has some problems (on 2 different machines): First, it crashes after XXX time (and it's not my application, it happens randomly on a brand new app). Second, it doesn't handle multiple requests, and my app uses a lot of small ajax calls making the website testing really slow for being on a local server with so small pages.
I thought I could install Rails + Apache but reading around I think it's not possible. What's the fastest configuration (that supports multi threaded requests) as a webserver to use with Rails?
I'm using wamp to handle my apache webserver, php, mysql and so on (yea I'm not an expert, but I can handle with configuration files). I would like to use rails through that apache if possible, I read I should use however apache + mongrel (can I handle multiple requests in this way?).
I'm ok with other configurations too, I just need someone to point me in the right direction and possibly on a tutorial.
So requirements are:
Handle parallel requests (I have a lot of small requests)
Stable (webrick crashes quite often)
Thanks a lot
Edit 1:
For those who like me uses virtual hosts, here is what I wrote:
<VirtualHost *:8080>
ServerAdmin admin#federtrek.org.localhost
DocumentRoot "D:/wamp/www/manage_federtrek_org"
ServerName federtrek.org.localhost
ServerAlias manage.federtrek.org.localhost
ErrorLog "logs/manage.federtrek.org-error.log"
CustomLog "logs/manage.federtrek.org-access.log" common
ProxyPass / http://manage.federtrek.org.localhost:3000/
ProxyPassReverse / http://manage.federtrek.org.localhost:3000/
</VirtualHost>
Where port 3000 is mongrel server and 8080 is apache port.
Follow the pdf document linked in the answer to make it works.
Webrick is not appropriate for a production environment; it should only be used for local development.
You're deploying Rails on Windows? Never heard of that being done before. If it were Linux or OS X and you insisted on using Apache, I would recommend Fusion Passenger (it's kind of like Ruby's mod_php). But I don't think they support windows.
I think your best bet is Mongrel...I think it runs under Windows. The idea is that you run one or more copies of your Rails app as Mongrel processes (services in Windows parlance?) You then setup Apache as a reverse-proxy to them, perhaps also doing some load balancing. While it's not actually multi-threading, it will have the same benefits. (In fact, most Rails deployments use this idea instead of true multi-threading.)
Should be lots of tutorials out there for this, on *nix at least. I think most of it will be the same for Windows. A quick Google search yielded this PDF specific to a Windows setup. http://www.napcsweb.com/howto/rails/deployment/RailsWithApacheAndMongrel.pdf

How to run\handle multiple Ruby on Rails applications on the same domain?

I am using Ruby on Rails 3.1 and I have an application running at the www.mywebsitename.com domain. For improvement reasons I would like to run my application at the www.uk.mywebsitename.com subdomain (BTW: at the www.mywebsitename.com domain I will run a RoR application to redirect users to the proper subdomain).
I do not need geocoding or similar. Simply, I would like to know how to run my application on the www.uk.mywebsitename.com Web address (I am planning to add as subdomain other/similar RoR applications like www.de.mywebsitename.com and www.it.mywebsitename.com, each working with a separate database): what I have to care/do? what do you advice about?
P.S.: My server is running Linux Ubuntu and Apache. I deploy with the Capistrano gem.
It seems like you're looking for how to make apache vhosts, since that's basically what they do.
I assume you're using phusion passenger, and in that case you should already have one vhost (or at least a default site in /etc/apache/sites-available (or something similar, it might be apache2, I'm not entirely sure).
What you basically need to do to get multiple rails applications working is to set up one vhost for each rails application and set the proper ServerName and DocumentRoot for each vhost.
It might look something like this for you uk site:
<VirtualHost *:80>
ServerName www.uk.mywebsitename.com
DocumentRoot /path/to/where/your/uk/site/is/deployed/current/public
<Directory /path/to/where/your/uk/site/is/deployed/current/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
How to setup vhosts for passenger is documented in the passenger documentation.

Resources