You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby on Rails + Passenger环境变量无法修改,始终启动开发模式求助

Fixing Stuck Development Environment Variables in Non-Standard Rails Projects

Hey George, let's work through this frustrating environment variable issue you're dealing with. Since your project doesn't follow the standard Rails structure (no environment.rb), the usual Rails-specific config files won't help here. Instead, focus on these key places to override RAILS_ENV, RACK_ENV, and related variables to force production mode:

1. Passenger Configuration Files

Since you mentioned PASSENGER_APP_ENV, Passenger's own config takes priority over system-level exports. Check these files:

  • Apache main config: /etc/httpd/httpd.conf or /etc/apache2/apache2.conf (depending on your Linux distro)
  • Virtual host config: /etc/httpd/conf.d/your_app.conf or /etc/apache2/sites-available/your_app.conf

Add or update these directives in the relevant block (usually inside <VirtualHost>):

PassengerAppEnv production
SetEnv RAILS_ENV production
SetEnv RACK_ENV production
SetEnv NODE_ENV production

After saving, restart Apache with service httpd restart (or systemctl restart apache2 for systemd-based distros).

2. Custom Application Startup Scripts

If your project uses a custom shell script to launch the app or Passenger, check for hardcoded environment variables. Look for lines like:

export RAILS_ENV=development

Change this to production and ensure the script is the one used when starting your server.

3. User-Specific Shell Profiles

If Apache/Passenger runs under a dedicated user (like apache, www-data, or a deploy-specific user), check their shell config files:

  • ~/.bashrc
  • ~/.bash_profile
  • ~/.profile

Look for any export RAILS_ENV=development lines and update them to production. You'll need to log in as that user and source the file (e.g., source ~/.bashrc) or restart the server for changes to take effect.

4. System-Level Environment Files

For system-wide overrides, edit these files:

  • /etc/environment: Add lines like RAILS_ENV="production" (no export keyword needed here)
  • /etc/profile.d/custom_env.sh: Create this file if it doesn't exist, then add:
    export RAILS_ENV=production
    export RACK_ENV=production
    export NODE_ENV=production
    export PASSENGER_APP_ENV=production
    

Run source /etc/environment or restart your entire system to apply these changes.

5. Dotenv Configuration (If Used)

If your project uses the dotenv gem (common in non-standard Rails setups), check for:

  • .env (root directory of your project)
  • .env.production

Add or update the variables in these files:

RAILS_ENV=production
RACK_ENV=production
NODE_ENV=production

Make sure dotenv is loaded early in your application's boot process to override any other conflicting settings.

Quick Debugging Tips

  • To confirm which environment Passenger is using, run passenger-status and look for the "Environment" field in the output.
  • Add a quick debug endpoint in your app to verify the current environment:
    get '/env-check' do
      "RAILS_ENV: #{ENV['RAILS_ENV']}\nRACK_ENV: #{ENV['RACK_ENV']}"
    end
    
  • Restart Apache and hit that endpoint to confirm your changes took effect.

内容的提问来源于stack exchange,提问作者George Morris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:37:30