rails / passenger: using environment variable for memcache server address - ruby-on-rails

I am trying to set up our Rails app to pull the address of the memcache server from an environment variable (we have 2 'production' environments, one for testing and one for live, and hence can't have different environments files).
This is a Rails 2.3.11 app, not rails 3.
I have this in my environments/production.rb file:
config.cache_store = :mem_cache_store, ENV['MEMCACHE_SERVERS'] || 'localhost:11211'
If I fire up a Rails console and do:
Rails.cache.stats
I see that it is using the right one out of the environment variable (set in /etc/environments on our ubuntu servers).
But, if our actual running rails app fired up via passenger it is using localhost!
Does Passenger not use the environment or am I doing something else wrong?
If there is a better way of doing this of course I am open to suggestions!

Have you tried using the Apache directive SetEnv or the equivalent for your server? The environment is heavily modified for Apache sub-processes, of which Passenger is one.

Related

rails 3 system command not working

Here is the issue. When running the 'system' command in a rails controller in development it behaves as intended by running the command while in production it does not do anything.
For example the following command:
system 'rails g migration user_generated_migration'
or even:
system 'ls'
work locally (in development) but on the server (production environment) they don`t do anything.
Am i missing something in the configuration files, production.rb maybe? Or is there something that should be enabled on the server?
Update:
The production environment is the default set up maybe with a notable change to how caching is handled:
config.cache_store = :dalli_store, ENV['MEMCACHE_SERVERS']
It is running on apache server through passenger. I suspect it has something to do with what rights the apache user has? I don`t have a lot of experience in the server area so i wouldn`t know what exact details to give you.

Error message: Unknown database 'someweb_production' (Mysql2::Error)

I'm trying to run this web aplication of Ruby on Rails with Apache using Phusion Passenger. I already configured the httpd.conf file.
I also have another aplication which runs with 'rails server' commmand, and it's connecting to the development database. However, i don't understand why the aplication which runs with apache it's trying to connect to a production database that i didn't create yet instead development as it should be.
What i have to configure to make my Ruby on Rails application run as development?
Apache with Phusion provide a production level web server environment, rails server (WEBrick) is a simple web server that lets you test locally. You typically use one or the other on a given machine. But not always :-)
But to answer your question, which database is used by Rails is determined by the RAILS_ENV variable, which is by default, one of production, development or test.
When you create a new rails application, a default database configuration is created in the file app/config/database.yml -- there are separate sections that provide necessary parameters for connecting to your database(s). Other environment-specific configurations may be specified either in the environment.rb or in app/config/environments/<name>.rb.
In your Passenger config, you can set the RAILS_ENV variable as documented here http://www.modrails.com/documentation/Users%20guide%20Apache.html#rails_env.
I just realized also that it seems like Passenger is looking for production (expecting the db name to be someweb_production) -- chances are you have to run a bundle exec rake db:migrate in the production environment in order to (create and) initialize the database. You may need to pass the environment parameter in this case also.
For your local config (rails server) the server will look for a shell environment variable named RAILS_ENV, and you can also pass a specific environment on the command line e.g. rails server --environment=development. I think if neither is specified, rails server defaults to development.
The problem was that i didn't include the following lines in the Apache conf file:
RailsEnv development
RackEnv development

Running a Rails site: development vs production

I'm learning Ruby on Rails. At the moment I'm just running my site locally with rails server in the OS X Terminal. What changes when a Rails site is run on a production box?
Is the site still started with rails server?
Any differences with how the db is setup?
Note: I'm running Rails 3.
A rails app can be run in production calling rails server -e production, although 99% of the time you'll be serving on something like passenger or thin instead of WEBrick, which means there's a different command to start the server. (thin start -e production for instance)
This is a complicated question, but the best place to start learning about the differences would be to look at the specific environment.rb files. When rails boots up it starts with the environment file that matches the called environment, ie if you start it in development it begins by loading your development.rb file, or if you're in production it will load the production.rb file. The differences in environments are mostly the result of these differences in the various environment config files.
Basically if a Rails 3.1 app is in production mode, then by default it is not going to be compiling assets on the fly, and a lot of caching will be going on that isn't happening in development. Also, when you get error messages they will be logged but not rendered to the user, instead the static error page from your public directory will be used.
To get more insight into this, I would suggest reading the relevant rails guides:
Rails Initialization Guide: http://guides.rubyonrails.org/initialization.html
Rails Configuration Guide: http://guides.rubyonrails.org/configuring.html
There are two contexts you can use the word "production" here. One of them is running the server in production mode. You can do this locally by,
RAILS_ENV=production ./script/server
The configuration for this is picked up from config/environments/production.rb. Try comparing this file with config/environments/development.rb. There are only subtle differences like caching classes. Development mode makes it easier so that it will respond to any changes you make instantly. Plus there are two different databases (by default) will be used namely yourproject_development and yourproject_production if you choose to run your server in either of these modes.
On the other hand, rails deployment to a production box is something different. You will need to pick your server carefully. You may have to deal with a deployment script may be capistrano. You may also need a load balancer such as netgear. The database also may require a deep consideration like size expectation, master/slave clustering etc.,
Note: I have never used Rails 3. This answer is biased towards 2.3.x.

Ruby on Rails: How to set which development environment an app runs in?

I'm relatively new to Ruby on Rails and occasionally I find this convention-over-configuration stuff a little confusing as a lot of things seemed to be hidden from the developer, as in this case.
I'm using rails 2.3.8 and when I run my app locally through NetBeans 6.9/Mongrel on my system it runs using the development environment parameters.. when I deploy it to a Fedora box and run it there in Apache HTTPD it automatically runs using the production environment parameters.
How does my app know which environment to use? I haven't changed anything in my app to set the environment.. both versions locally and on my Fedora box are identical. I can't find anywhere in the code where it is setting the environment.. so how is this working?
Thanks.
In httpd.conf file, write the following in VirtualHost:-
## Specify Rails Environment here,
default value is "production"
RailsEnv development
Thanks...
The primary way to specify rails mode is RAILS_ENV environment variable (I assume development is default, when nothing is specified). You can check its value in bash, echo $RAILS_ENV.
You can also modify ENV['RAILS_ENV'] in your config file to change the mode:
ENV['RAILS_ENV'] = 'production'
edit
I've never used rails with apache, but I think passenger mod can also specify this variable somewhere, checking apache configs might help.

Rails 3 - set environment

I have a rails 3 app (which I upgraded). It runs on passenger and nginx but on my production server it also starts with the environment set to 'production'. I know I am missing something really basic, but I just can't figure out where to set the environment instead of in environment.rb.
Thanks for helping!
UPDATE: ok, I learned I can still do that with Rails.env = 'production'.
That seems kind of old school to me. Do you know an elegant way to configure this maybe in the Capfile or sth like that?
Rails 3 is a little bit different than Rails 2.x in that it has a config.ru file, like other Rack applications.
Passenger detects rails as a Rack app, so you'll have to use RackEnv instead of RailsEnv in the vhost. You can set the environment using RackEnv as per the documentation for Passenger/Nginx.
You can configure a different RAILS_ENV for each app in your vhost for nginx with passenger. I've never used nginx but in apache it's just a RailsEnv=development directive. That way each site just has it set, no worries with configuring a cap task or variable or anything. See the docs. Note that the default is production so this should already be set for you.

Resources