Rails environment deployment issue? - ruby-on-rails

I'm trying to deploy my first Rails app here, and I've been stuck on something since last night. I'm encountering some weird behaviors I can't explain.
I'm running Rails, Apache, Phusion Passenger, and for the moment, SQLite 3. (I'll move that over to MySQL shortly.) Currently this is being hosted on a too-small EC2 slice running Ubuntu Server 11.04 (Natty).
When I visit the address of the EC2 slice in the browser, I get the default Rails 500 notice. Here's what's weird, though. When I tail /log/production.log, I see the following error:
ActionView::Template::Error (SQLite3::SQLException: no such table: offers: SELECT "offers".* FROM "offers" WHERE (code = '') ORDER BY created_at desc LIMIT 25 OFFSET 0):
So, I manually opened up the development database in SQLite3, and saw that table in there. The production database, however, does not have that table.
OK, so I'm getting errors with the production database logged in the production log. The application has to be running in production mode, right?
That is what's throwing me. First of all, it's running in development mode on my development machine, and I didn't change any of the files when I deployed it. Neither did I use any fancy deployment tools to deploy it (which may have switched something without my knowledge) - I just did a simple git push.
Furthermore, I added the following to my httdp.conf VirtualHost config:
RailsEnv development
Also, when I run rails console, I can get the following:
irb(main):002:0> Rails.env
=> "development"
So, the application really should be running in development mode, right? In fact, it seems to think (partially) that it is, right?
I'm really not sure what's happening here, and I'd really appreciate some expert advice.
Thanks everyone.
Edit - A few server reboots later, and now the thing just hangs when I try to view it in a browser. Also, Apache seems to hang when I try to restart it (hence the server reboots), related problem, or different problem altogether?

Well, this isn't a 100% satisfactory answer for me, but I did two things, and I think I got it working.
First, I re-installed the passenger Apache module. That may or may not have been necessary.
This was the big thing, though: after I had added the line to httpd.conf to pass the Rails Environment over to Passenger, I believe Apache restarted incorrectly. (Rather, I believe I've been restarting Apache incorrectly for my whole life!)
I was trying to restart Apache this way:
sudo /etc/init.d/apache2 restart
That has always worked for me (when programming PHP), but it simply wasn't working here. Apache would just stall on the restart.
This, however, works fine:
sudo apachectl restart
I'll have to ask Server Fault what the significant difference is between the two.
I hope that helps someone out.

Related

Err max clients reached Redis/Sidekiq/Rails

I have been stuck on this issue for the past 3 days and unsure where to look now.
I have a simple Sidekiq implementation into my rails app.
I am working on: Rails 4.2.0, Sidekiq 4.1.2, Redis 3.0.6
The production app is running live with heroku, and I have 1 worker dyno and 1 web dyno.
The issue is this, and I am unsure on how to approach it or what I did to make it do this.
When I run the redis-cli on heroku I can see the clients that I have running. At most I have 2 or 3 clients running at any given time. I can easily kill the clients with
CLIENT KILL TYPE normal
So that's all fine and dandy. The part when things get a little tricky is when I fire up my server locally, and I am working in development. All of a sudden my redic-cli shows that I have 19 clients running. This will result in me logging
Err max clients reached
My assumption is that somehow locally I am directing sidekiq to work off the redis production url. I have to admit what I know about Redis and Sidekiq is limited, but I do have a basic understanding of how it should be working.
Any help or guidance would be appreciated.
Try using sidekiq -c 3 to limit your concurrency.
This ended up being a configuration error. Just in case anyone stumbles upon this question hopefully this will help them not overlook something like I did.
This issue was happening only when I was firing up my local server, so I knew it had something to do with me locally. I noticed that on my production redis:cli I was seeing clients that had my local IP in the ADDR column.
This led me to believe that my local machine was pushing clients to my production Redis server. Looking at my logs when I fired up my Procfile I saw the Redis url there so that only confirmed it.
Finally after searching through my code, I discovered that I had actually added the url into my .env, so when I fired up my server it was using that production Redis url. So I changed it to the appropriate IP address for local development on my .env file redis://127.0.0.1:6379 and everything is now working as normal.

Passenger Hanging after Restart - Shared Hosting Environment

Last night I made a revision to [app-root]/config/initializers/refinery/core.rb and the touched [app-name]/tmp/restart.txt. Then I tried to reload the app to find it totally hanging. The other two RoR apps I'm running on the same server were also hanging. Per curl -v there was no response at all from the server.
Everything was working fine before I touched the restart.txt file. The following morning, seven or eight hours later, the sites are working again and my minor edit (the site name) is showing up.
Rails 3.2.16 on a hostmonster linux server.
The instance of Passenger is not running under my user so I wonder if my hang horned any other user's RoR sites?
I can't restart apache (of course) since this is a shared server. Any suggestions for how to deal with this if it happens again? I have no budget for Heroku or a VPS.
UPDATE: Did the same steps again this morning, revised the core.rb and asked for a restart by touching restart.txt and it restarted in less than a minute. The question remains valid I think - what can a person do to address a hanging Passenger or prevent it from happening?

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).

Rails application running through nginx forcing https despite contrary setting?

I have been working towards deploying my rails application, and while there have been some kinks I've been able to work through most of them. The latest is quite perplexing, however.
My setup is as follows:
Ruby 1.9/Rails 3.2 App w/Phusion Passenger --> nginx on CentOS 6.2 --> Varnish cache server
Despite config.force_ssl being set to false AND being commented out in the production.rb file, the server is still forcibly redirecting to https (which is not yet functional in my case). The server has been restarted repeatedly via 'killall nginx' and '/opt/nginx/sbin/nginx', so the setting should be taking effect.
My first thought was that perhaps the cache server was lagging behind, but this isn't the case.
What makes this particularly odd, though, is that the application works fine if not put into production mode. If I edit nginx.conf to instead use development mode, the site works flawlessly.
What could be the culprit? I am very much new to this facet of server maintenance and IT so I have no idea.
Turned out that it really was varnish being over-aggressive.

should I be using passenger in dev to match prod for a ruby on rails project?

Should I be using passenger in dev to match prod for a ruby on rails project? (as opposed Webrick)
Noting I'll be deploying to dreamhost which uses passenger.
(Also if yes, does it have it's own web-server or do I need to install one?)
Set up a passenger staging server on your production machine. Use same configuration as pro server.
This has just saved my bacon. A recent upgrade we recently pushed broke application and took me a week to get sorted. Fortunately production server continued to function which we found prob.
Use capistrano multistage to manage the same.
You don't have to (mostly). But there might be cases where things can go wrong due to the mismatch between production & dev environment. Using staging environment will catch such issues.
If you want to test things like SSL, then webrick wont work. You need apache/nginx(or other web servers) to use passenger. I also noticed that there is no significant difference in the performance between the two options

Resources